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

Add Triekey to reduce allocations #8007

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

Add Triekey to reduce allocations #8007

wants to merge 10 commits into from

Conversation

benaadams
Copy link
Member

@benaadams benaadams commented Jan 5, 2025

Changes

  • Add Triekey which can represent a key from two arrays rather than allocating a new one using concatenation
  • Horrible

Types of changes

What types of changes does your code introduce?

  • Optimization

Testing

Requires testing

  • Yes

If yes, did you write tests?

  • Yes

@benaadams benaadams mentioned this pull request Jan 5, 2025
2 tasks
@LukaszRozmej LukaszRozmej requested a review from asdacap January 7, 2025 14:06
Copy link
Member

@LukaszRozmej LukaszRozmej left a comment

Choose a reason for hiding this comment

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

Looks ok for me, but would like to hear @asdacap opinion

Comment on lines +44 to +50
for (int i = 0; i < path.Length - 1; i += 2)
{
output[i / 2 + 1] =
path.Length % 2 == 0
? (byte)(16 * path[i] + path[i + 1])
: (byte)(16 * path[i + 1] + path[i + 2]);
}
Copy link
Member

Choose a reason for hiding this comment

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

Can this be SIMD'ed?

Comment on lines +168 to +172
for (int i = 0; i < byteLength; i++)
{
pathSpan[i + pathSpanStart] = Nibbles.ToByte(nibbles[offset + i * 2], nibbles[offset + i * 2 + 1]);
Length += 2;
}
Copy link
Member

Choose a reason for hiding this comment

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

SIMD?

@@ -55,15 +55,15 @@ public void VisitBranch(TrieNode node, TrieVisitContext trieVisitContext)

public void VisitExtension(TrieNode node, TrieVisitContext trieVisitContext)
{
_builder.AppendLine($"{GetPrefix(trieVisitContext)}EXTENSION {Nibbles.FromBytes(node.Key).ToPackedByteArray().ToHexString(false)} -> {KeccakOrRlpStringOfNode(node)}");
_builder.AppendLine($"{GetPrefix(trieVisitContext)}EXTENSION {Nibbles.FromBytes(node.Key.ToArray()).ToPackedByteArray().ToHexString(false)} -> {KeccakOrRlpStringOfNode(node)}");
Copy link
Member

Choose a reason for hiding this comment

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

should Nibbles support FromKey?

@asdacap
Copy link
Contributor

asdacap commented Jan 7, 2025

There should be a patricia tree benchmark somewhere. What is the result?
Personally, I'd just switch to TreePath for the key. Two array pointer is already 16 byte on its own. I'm sure concatenating arbitrary maximum 32 byte struct at 4 bit boundary can be made fast somehow.
If I'm not mistaken, extension is not common so the main issue is leaf but that also mean that the keys are mostly long most of the time, so its likely that the main concat operation is the byte + key or the remaining update path thing.

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.

4 participants