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

feat: add borsh::object_length helper #236

Merged
merged 3 commits into from
Sep 28, 2023
Merged

feat: add borsh::object_length helper #236

merged 3 commits into from
Sep 28, 2023

Conversation

dj8yfo
Copy link
Collaborator

@dj8yfo dj8yfo commented Sep 28, 2023

resolves #23

in scope of work on nearcore update it was noticed that
pattern borsh_obj.try_to_vec().unwrap().len() was used quite a few times,
thus it may be a slight optimization to replace this pattern with computing length without memory allocation:

 nearcore
 ├──tools
 │  └──amend-genesis
 │     └──src
 │        └──lib.rs
 │           └──+ access_key.try_to_vec().unwrap().len() as u64
 ├──runtime
 │  └──runtime
 │     └──src
 │        ├──actions.rs
 │        │  ├──+ access_key.try_to_vec().unwrap().len() as u64
 │        │  ├──delete_key.public_key.try_to_vec().unwrap().len() as u64
 │        │  ├──+ access_key.try_to_vec().unwrap().len() as u64
 │        │  ├──delete_key.public_key.try_to_vec().unwrap().len() as u64
 │        │  ├──+ Some(access_key).try_to_vec().unwrap().len() as u64
 │        │  ├──add_key.public_key.try_to_vec().unwrap().len() as u64
 │        │  └──+ add_key.access_key.try_to_vec().unwrap().len() as u64
 │        └──verifier.rs
 │           ├──public_key.try_to_vec().unwrap().len() as u64
 │           └──+ access_key.try_to_vec().unwrap().len() as u64
 ├──core
 │  └──store
 │     ├──benches
 │     │  └──finalize_bench.rs
 │     │     └──let num_chunks = memory_range / chunk.try_to_vec().unwrap().len();
 │     └──src
 │        ├──trie
 │        │  └──split_state.rs
 │        │     ├──.fold(0_u64, |sum, receipt| sum + receipt.try_to_vec().unwrap().len() as u64);
 │        │     └──.fold(0_u64, |sum, receipt| sum + receipt.try_to_vec().unwrap().len() as u64);
 │        └──genesis
 │           └──state_applier.rs
 │              ├──+ public_key.try_to_vec().unwrap().len() as u64
 │              └──+ access_key.try_to_vec().unwrap().len() as u64;
 └──integration-tests
    └──src
       └──tests
          └──client
             ├──features
             │  └──zero_balance_account.rs
             │     ├──assert_eq!(PUBLIC_KEY_STORAGE_USAGE, edwards_public_key.try_to_vec().unwrap().len());
             │     ├──assert_eq!(FULL_ACCESS_PERMISSION_STORAGE_USAGE, full_access_key.try_to_vec().unwrap().len());
             │     └──assert_eq!(FUNCTION_ACCESS_PERMISSION_STORAGE_USAGE, fn_access_key.try_to_vec().unwrap().len());
             └──benchmarks.rs
                └──let size = chunk.try_to_vec().unwrap().len();

@dj8yfo
Copy link
Collaborator Author

dj8yfo commented Sep 28, 2023

group                                                             base                                   new
-----                                                             ----                                   ---
ser_account/borsh::object_length(obj).unwrap()/idx=0; size=191    1.00      0.4±0.01ns   415.3 GB/sec    1.00      0.4±0.01ns   415.3 GB/sec
ser_account/borsh::object_length(obj).unwrap()/idx=1; size=168    1.00      0.4±0.01ns   363.5 GB/sec    1.00      0.4±0.01ns   363.5 GB/sec
ser_account/borsh::object_length(obj).unwrap()/idx=2; size=90     1.00      0.4±0.01ns   199.3 GB/sec    1.00      0.4±0.01ns   199.3 GB/sec
ser_account/borsh::to_vec(obj).unwrap().len()/idx=0; size=191     1.00     19.1±0.33ns     9.3 GB/sec    1.00     19.1±0.33ns     9.3 GB/sec
ser_account/borsh::to_vec(obj).unwrap().len()/idx=1; size=168     1.00     18.2±0.23ns     8.6 GB/sec    1.00     18.2±0.23ns     8.6 GB/sec
ser_account/borsh::to_vec(obj).unwrap().len()/idx=2; size=90      1.00     19.2±1.26ns     4.4 GB/sec    1.00     19.2±1.26ns     4.4 GB/sec

@dj8yfo dj8yfo force-pushed the borsh_object_length branch 2 times, most recently from c29a813 to b2408cb Compare September 28, 2023 13:56
@dj8yfo dj8yfo force-pushed the borsh_object_length branch 3 times, most recently from f5c5be3 to fc803e5 Compare September 28, 2023 17:47
self.len = match res {
Some(res) => res,
None => {
return Err(ErrorKind::OutOfMemory.into());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

replacing this line with

              return Err(Error::new(ErrorKind::InvalidData, "overflow"));

results in much slower bench

time:   [+1707.9% +1716.3% +1726.3%] (p = 0.00 < 0.05)
thrpt:  [-94.524% -94.494% -94.469%]
Performance has regressed.

@dj8yfo dj8yfo force-pushed the borsh_object_length branch from fc803e5 to e5e1bf6 Compare September 28, 2023 17:57
@dj8yfo dj8yfo marked this pull request as ready for review September 28, 2023 18:08
@dj8yfo dj8yfo requested a review from frol as a code owner September 28, 2023 18:08
Copy link
Collaborator

@frol frol left a comment

Choose a reason for hiding this comment

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

Thank you for baking it into borsh!

@dj8yfo dj8yfo merged commit bb5248e into master Sep 28, 2023
7 checks passed
@dj8yfo dj8yfo deleted the borsh_object_length branch September 28, 2023 19:58
This was referenced Sep 28, 2023
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.

Add BorshSize trait
2 participants