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

Optimize ABI API to have less allocation #7591

Open
LukaszRozmej opened this issue Oct 11, 2024 · 10 comments
Open

Optimize ABI API to have less allocation #7591

LukaszRozmej opened this issue Oct 11, 2024 · 10 comments

Comments

@LukaszRozmej
Copy link
Member

LukaszRozmej commented Oct 11, 2024

Encoding and Decoding ABI allocates a lot, rewrite it to a version that will allocate a lot less (stream-like or spans?) and update usages.

@ssonthal
Copy link
Contributor

ssonthal commented Oct 13, 2024

Hi @LukaszRozmej can I work on this?

I read a bit about streams and spans. Streams are likely to work better when we have large chunk of data of variable sizes while spans work better with structured fixed-size data and the latter offers better performance enhancement, maintains concurrency in a better way.

What would you suggest is a better approach? I am currently not aware about the data that Abi encoding deals with.

@LukaszRozmej
Copy link
Member Author

Hi @LukaszRozmej can I work on this?

Sure, take a look at: https://github.com/NethermindEth/nethermind/blob/master/src/Nethermind/Nethermind.Abi/AbiEncoder.cs and everything under it (AbiTypes etc). This now allocates a ton of objects when you Decode and ton of byte arrays when you Encode.

Think how can you allocate as little as possible. For inspiration you can check how our Rlp Encoding/Decoding works with NettyRlpStream that is based on pooled memory.

@NethermindEth NethermindEth deleted a comment Oct 26, 2024
@ssonthal
Copy link
Contributor

ssonthal commented Nov 18, 2024

@LukaszRozmej can we try to optimize this? -

In AbiBytes class's Encode method PadRight method is used like this - ,
image

The PadRight method create new byte array allocations like this and this method is used at multiple places.

image

@ssonthal
Copy link
Contributor

The Slice method in the PadRight method also create new allocations and used heavily ->

image

@ssonthal
Copy link
Contributor

Hey @LukaszRozmej , gentle nudge for your advise on this.

@LukaszRozmej
Copy link
Member Author

We definitely should be able to, but this is just tip of the iceberg and I think there are way more ways to optimise ABI

@ssonthal
Copy link
Contributor

Maybe we can break it down to subtasks for incremental progress?

@ssonthal
Copy link
Contributor

Hey @LukaszRozmej so for PadLeft and Slice methods, we can start changing the concerned files by using object pools instead of new allocations.

Can I take his as the first subtask for this issue?

@LukaszRozmej
Copy link
Member Author

Hey @LukaszRozmej so for PadLeft and Slice methods, we can start changing the concerned files by using object pools instead of new allocations.

Can I take his as the first subtask for this issue?

I think using object pools might be hard as a lot of those arrays are then later returned upstream, so the upstream would have to track and eventually return them.

But if we try changing to Span<> or maybe better here to Memory<> in a lot of places we might be able to still reduce allocations substantially.

@ssonthal
Copy link
Contributor

Got it! So I will try to use Span/Memory instead of creating new byte arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants