near / borsh-rs

Rust implementation of Binary Object Representation Serializer for Hashing
https://borsh.io/
Apache License 2.0
299 stars 65 forks source link

feat: add `borsh::object_length` helper #236

Closed dj8yfo closed 11 months ago

dj8yfo commented 11 months ago

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 commented 11 months ago
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