Skip to content

Conversation

@jrick
Copy link
Member

@jrick jrick commented Dec 11, 2025

This adds two additional type cases to the optimized writes for BLAKE-256 (used by transactions and the original PoW algorithm) and BLAKE-3 (used by the current PoW algorithm). It also updates both the block header and transaction code to use these hashers during the calculation of block and transaction hashes.

Requires #3589.

@davecgh davecgh added this to the 2.2.0 milestone Dec 11, 2025
@jrick jrick force-pushed the optimize_hashing branch 4 times, most recently from 84a4633 to 27b36d8 Compare December 12, 2025 08:25
This special cases writes to bytes.Buffer, which is always the writer type
written to by WriteMessageN.  There are several optimizations that can be
implemented by special casing this type:

First, pulling temporary short buffers from binary freelist can be skipped
entirely, and instead the binary encoding of integers can be appended directly
to its existing capacity.  This avoids the synchronization cost to add and
remove buffers from the free list, and for applications which only ever write
wire messages with WriteMessageN, the allocation and ongoing garbage
collection scanning cost to for these buffers can be completely skipped.

Second, special casing the buffer type in WriteVarString saves us from
creating a temporary heap copy of a string, as the buffer's WriteString method
can be used instead.

Third, special casing the buffer allows WriteMessageN to calculate the
serialize size and grow its buffer so all remaining appends for writing block
and transactions will not have to reallocate the buffer's backing allocation.
This same optimization can be applied to other messages in the future.
This adds two additional type cases to the optimized writes for BLAKE-256
(used by transactions and the original PoW algorithm) and BLAKE-3 (used by the
current PoW algorithm).  It also updates both the block header and transaction
code to use these hashers during the calculation of block and transaction
hashes.
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.

2 participants