Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Size limited containers; Refactored Buffer and BufferView #1373

Merged
merged 22 commits into from
Oct 28, 2022

Conversation

xDimon
Copy link
Member

@xDimon xDimon commented Oct 21, 2022

Signed-off-by: Dmitriy Khaustov aka xDimon [email protected]

Referenced issues

Resolves #1364

Description of the Change

Implemented size limited containers.
Refactored Buffer and BufferView: inheritance instead composition, hold mover span by BufferView

Benefits

Ability to limit size of containers (i.e. can be useful during scale-decode).
Fixed some using memory of just destroyed temporary buffers.

Usage Examples or Tests

Appended

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
@codecov
Copy link

codecov bot commented Oct 21, 2022

Codecov Report

Merging #1373 (3fae415) into master (88bfa95) will increase coverage by 0.38%.
The diff coverage is 70.14%.

@@            Coverage Diff             @@
##           master    #1373      +/-   ##
==========================================
+ Coverage   24.19%   24.57%   +0.38%     
==========================================
  Files         628      629       +1     
  Lines       23520    23613      +93     
  Branches    12287    12288       +1     
==========================================
+ Hits         5691     5804     +113     
- Misses      12540    12543       +3     
+ Partials     5289     5266      -23     
Impacted Files Coverage Δ
.../service/child_state/impl/child_state_api_impl.cpp 22.09% <0.00%> (+2.32%) ⬆️
core/blockchain/impl/block_storage_impl.cpp 22.80% <0.00%> (ø)
core/blockchain/impl/storage_util.cpp 41.86% <0.00%> (ø)
core/common/blob.hpp 63.26% <ø> (ø)
core/host_api/impl/child_storage_extension.cpp 26.50% <0.00%> (-0.45%) ⬇️
core/host_api/impl/storage_extension.cpp 15.21% <0.00%> (ø)
core/injector/application_injector.cpp 32.85% <0.00%> (+0.05%) ⬆️
core/offchain/impl/offchain_local_storage.cpp 0.00% <0.00%> (ø)
core/offchain/impl/offchain_persistent_storage.cpp 8.57% <0.00%> (ø)
core/primitives/authority.hpp 84.61% <ø> (ø)
... and 23 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
…nditional compiling

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
@xDimon xDimon marked this pull request as ready for review October 25, 2022 09:02
@xDimon xDimon requested review from iceseer and kamilsa October 25, 2022 09:03
@xDimon xDimon changed the title Feature: size limited containers Size limited containers; Refactored Buffer and BufferView Oct 25, 2022
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Comment on lines 421 to 445
bool operator==(const Base &other) const noexcept {
return Base::size() == other.size()
and std::equal(
Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
}

template <size_t Size>
bool operator==(const std::array<typename Base::value_type, Size> &other)
const noexcept {
return Base::size() == Size
and std::equal(
Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
}

bool operator<(const Base &other) const noexcept {
return std::lexicographical_compare(
Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
}

template <size_t Size>
bool operator<(const std::array<typename Base::value_type, Size> &other)
const noexcept {
return std::lexicographical_compare(
Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
}
Copy link
Contributor

@turuslan turuslan Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want bidirectional compare like Self < Base and Base < Self.
Maybe boost::equality_comparable, or friend functions.


SizeLimitedContainer() = default;

SizeLimitedContainer(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы оставил только мув-/копи-конструкторы, инит лист, и через итераторы.

xDimon added 10 commits October 27, 2022 15:53
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

template <size_t MaxSize>
using SLBufer = BufferT<MaxSize>;
typedef SLBuffer<std::numeric_limits<size_t>::max()> Buffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using?


back_insert_iterator<Buffer> &operator=(uint8_t value) {
back_insert_iterator<Container> &operator=(uint8_t value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May shorten

Suggested change
back_insert_iterator<Container> &operator=(uint8_t value) {
using Self = back_insert_iterator<Container>;
Self &operator=(uint8_t value) {

or

Suggested change
back_insert_iterator<Container> &operator=(uint8_t value) {
auto &operator=(uint8_t value) {

return Base(size);
}()) {}

SizeLimitedContainer(size_t size, const typename Base::value_type &value)
Copy link
Contributor

@turuslan turuslan Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify typename Base::value_type to
value_type (because we are already inherited that container)
or using?


std::ostream &operator<<(std::ostream &os, const Buffer &buffer);
std::ostream &operator<<(std::ostream &os, BufferView view);
using BufferMutRef = std::reference_wrapper<Buffer>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused

Suggested change
using BufferMutRef = std::reference_wrapper<Buffer>;

@@ -41,7 +40,8 @@ namespace kagome::blockchain {
}
auto root = trie.getRoot();
if (root == nullptr) {
return codec.hash256(common::Buffer{0});
static const auto zero_hash = codec.hash256(common::Buffer{0});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing mocks may conflict

@@ -371,7 +371,7 @@ namespace kagome::host_api {
storage::trie::PolkadotCodec codec;
if (pv.empty()) {
static const auto empty_root =
common::Buffer{}.put(codec.hash256(common::Buffer{0}));
common::Buffer(codec.hash256(common::Buffer{0}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be moved to global const?

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
@xDimon xDimon requested review from turuslan and iceseer October 27, 2022 18:09
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
@xDimon xDimon merged commit db19edf into master Oct 28, 2022
@xDimon xDimon deleted the feature/size_limited_containers branch October 28, 2022 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restrict size of scale buffer
4 participants