Skip to content

statement-store: make encode/hash faster#10882

Merged
alexggh merged 7 commits intomasterfrom
alexggh/hash-faster
Jan 28, 2026
Merged

statement-store: make encode/hash faster#10882
alexggh merged 7 commits intomasterfrom
alexggh/hash-faster

Conversation

@alexggh
Copy link
Copy Markdown
Contributor

@alexggh alexggh commented Jan 22, 2026

By reserving the memory in advance we halve the encoding speed which ultimately speeds up the statement.hash() function which gets called in a lot of places.

More importantly, when we start being connected to more nodes the hash function gets called a lot for the same statement because we might receive the same statement from all peers we are connected to.

For example on versi on_statements ate a lot of time when running with 15 nodes, see #10814 (comment).

Modified the statement_network benchmark to also be parameterizable by the number of times we might receive a statement and if we receive it from 16 peers, we notice a speed up with this PR of ~16%, which I consider not negligible, so I consider this an worthy improvement.

on_statements/statements_2000/peers_16/threads_8/blocking
                        time:   [22.099 ms 22.641 ms 23.175 ms]
                        change: [-18.841% -16.637% -14.429%] (p = 0.00 < 0.05)

Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
@alexggh alexggh added the T8-polkadot This PR/Issue is related to/affects the Polkadot network. label Jan 23, 2026
Copy link
Copy Markdown
Contributor

@AndreiEres AndreiEres left a comment

Choose a reason for hiding this comment

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

Should we reduce calls of hash() then and pass it into arguments where it's possible?

alexggh and others added 3 commits January 23, 2026 16:54
Co-authored-by: Andrei Eres <eresav@me.com>
Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
@alexggh alexggh enabled auto-merge January 27, 2026 14:02
Comment on lines +498 to +515
// Calculate capacity for preallocation as a close approximation of the SCALE-encoded
// size without actually performing the encoding. Uses size_of for type sizes:
// - Compact length prefix: 1-5 bytes (assume 5 for safety)
// - Proof field: 1 (tag) + 1 (enum discriminant) + size_of::<Proof>()
// - DecryptionKey: 1 (tag) + size_of::<DecryptionKey>()
// - Priority: 1 (tag) + size_of::<u32>()
// - Channel: 1 (tag) + size_of::<Channel>()
// - Each topic: 1 (tag) + size_of::<Topic>()
// - Data: 1 (tag) + 5 (compact len) + data.len()
let proof_size =
if !for_signing && self.proof.is_some() { 1 + 1 + size_of::<Proof>() } else { 0 };
let decryption_key_size =
if self.decryption_key.is_some() { 1 + size_of::<DecryptionKey>() } else { 0 };
let priority_size = if self.priority.is_some() { 1 + size_of::<u32>() } else { 0 };
let channel_size = if self.channel.is_some() { 1 + size_of::<Channel>() } else { 0 };
let topics_size = self.num_topics as usize * (1 + size_of::<Topic>());
let data_size = self.data.as_ref().map_or(0, |d| 1 + 5 + d.len());
let compact_prefix_size = if !for_signing { 5 } else { 0 };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should either use max_encoded_len (instead of size_of) or you just come up with some worst case max encoded len for the entire struct. In the end, even if this is a little bit above, it will not hurt that much too always over allocate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Modified it to use max_encoded_len, also added a test to make sure estimated is always slightly higher than the actual encoded size.

@alexggh alexggh disabled auto-merge January 28, 2026 08:20
Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
@alexggh alexggh requested review from AndreiEres and P1sar January 28, 2026 09:22
@alexggh alexggh added this pull request to the merge queue Jan 28, 2026
Merged via the queue into master with commit 21df44e Jan 28, 2026
238 of 240 checks passed
@alexggh alexggh deleted the alexggh/hash-faster branch January 28, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T8-polkadot This PR/Issue is related to/affects the Polkadot network.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants