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

[Memtrie] Optimize NibbleSlice during the memtrie construction code path #11342

Merged
merged 1 commit into from
May 20, 2024

Conversation

robin-near
Copy link
Contributor

This contains two top optimizations that speed up the memtrie construction code:

  • Rather than allocating a new Vec every time we encode nibbles, we use a freelist so that previously allocated Vec can be reused. This reduces allocations from O(millions) to O(tens).
    • As a side effect, InputMemTrieNode has to be changed to contain borrowed slices, as we can no longer give owned Box<[u8]> to it. This is totally fine though because anyway the InputMemTrieNode's data needs to be copied into the constructed memtrie nodes in the arena.
  • Optimize the nibble slice encoding function so that rather than encoding nibble by nibble, we encode the first nibble and then just copy the remaining bytes directly. Also optimize the at(i) function, removing branching entirely (this code now gets compiled to a bunch of arithmetic operations).

On shard 5 these optimizations reduce load time from 146s to 122s.

@robin-near robin-near requested a review from a team as a code owner May 17, 2024 20:45
Copy link

codecov bot commented May 17, 2024

Codecov Report

Attention: Patch coverage is 97.33333% with 6 lines in your changes are missing coverage. Please review.

Project coverage is 71.09%. Comparing base (c7ef040) to head (f746fd5).
Report is 2 commits behind head on master.

Files Patch % Lines
core/store/src/trie/mem/construction.rs 96.96% 2 Missing and 1 partial ⚠️
core/store/src/trie/mem/freelist.rs 91.66% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11342      +/-   ##
==========================================
+ Coverage   71.08%   71.09%   +0.01%     
==========================================
  Files         783      784       +1     
  Lines      156875   156945      +70     
  Branches   156875   156945      +70     
==========================================
+ Hits       111508   111580      +72     
- Misses      40528    40539      +11     
+ Partials     4839     4826      -13     
Flag Coverage Δ
backward-compatibility 0.24% <0.00%> (-0.01%) ⬇️
db-migration 0.24% <0.00%> (-0.01%) ⬇️
genesis-check 1.38% <0.00%> (-0.01%) ⬇️
integration-tests 37.11% <80.88%> (+0.02%) ⬆️
linux 68.80% <97.33%> (+0.03%) ⬆️
linux-nightly 70.51% <97.33%> (+<0.01%) ⬆️
macos 52.26% <97.33%> (+1.66%) ⬆️
pytests 1.60% <0.00%> (-0.01%) ⬇️
sanity-checks 1.40% <0.00%> (-0.01%) ⬇️
unittests 65.53% <97.33%> (+0.03%) ⬆️
upgradability 0.29% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bowenwang1996 bowenwang1996 requested a review from nagisa May 20, 2024 17:45
Copy link
Contributor

@tayfunelmas tayfunelmas left a comment

Choose a reason for hiding this comment

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

I did not get a detailed view of the nibble_slice.rs but the other changes LGTM. (I assume nibble_slice.rs is well tested anyways).

@robin-near robin-near added this pull request to the merge queue May 20, 2024
Merged via the queue into near:master with commit 141e7e5 May 20, 2024
29 checks passed
@robin-near robin-near deleted the memopt5 branch May 20, 2024 22:15
use super::node::MemTrieNodeId;
use crate::trie::mem::node::InputMemTrieNode;
use crate::NibbleSlice;
use elastic_array::ElasticArray4;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like an iffy crate to use compared to the much better established smallvec: the documentation is pretty light, the maintenance status is unclear and its just plain a less popular crate.

Though I guess this is a preexisting dependency, so I'll file an issue to address this instead...

marcelo-gonzalez pushed a commit to marcelo-gonzalez/nearcore that referenced this pull request May 23, 2024
…ath (near#11342)

This contains two top optimizations that speed up the memtrie
construction code:

* Rather than allocating a new Vec<u8> every time we encode nibbles, we
use a freelist so that previously allocated Vec<u8> can be reused. This
reduces allocations from O(millions) to O(tens).
* As a side effect, InputMemTrieNode has to be changed to contain
borrowed slices, as we can no longer give owned Box<[u8]> to it. This is
totally fine though because anyway the InputMemTrieNode's data needs to
be copied into the constructed memtrie nodes in the arena.
* Optimize the nibble slice encoding function so that rather than
encoding nibble by nibble, we encode the first nibble and then just copy
the remaining bytes directly. Also optimize the at(i) function, removing
branching entirely (this code now gets compiled to a bunch of arithmetic
operations).

On shard 5 these optimizations reduce load time from 146s to 122s.
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.

None yet

3 participants