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

Feat: optimizations with smallvec #65

Merged
merged 2 commits into from
Oct 15, 2024
Merged

Feat: optimizations with smallvec #65

merged 2 commits into from
Oct 15, 2024

Conversation

mrLSD
Copy link
Member

@mrLSD mrLSD commented Oct 14, 2024

Description

Added optimizations with smallvec crate. The intention is to reduce memory allocation when it possible.

One of the most significant problems for memory allocation and re-allocation is unknown size of data. To mitigate it, we can allocate on the stack for arrays, when size is known. And reallocate memory on the heap for program and algorithm needs. smallvec solves it good enough.

Simulation results

It was tested through aurora-engine tests. It shows insignificant NEAR gas consumption improvement.

⚠️ Notes

As NEAR gas consumption improved insignificantly, I don't mind if there are some arguments against this solution.

@mrLSD mrLSD self-assigned this Oct 14, 2024
@mrLSD mrLSD added the enhancement New feature or request label Oct 14, 2024
@mrLSD mrLSD requested review from aleksuss and birchmd October 14, 2024 22:15
Cargo.toml Outdated Show resolved Hide resolved
src/executor/stack/executor.rs Outdated Show resolved Hide resolved
@mrLSD mrLSD requested a review from aleksuss October 15, 2024 09:41
Copy link
Member

@birchmd birchmd left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this potential optimization. I think it is fine to merge it even if the impact is low.

I also think it makes sense that this optimization does not save much gas because it always saves exactly one memory allocation per call to Aurora Engine (the first one where the call stack is initialized). SmallVec allows us to skip the first allocation but if the call stack gets deeper than DEFAULT_CALL_STACK_CAPACITY then a new allocation is required (both in the old code and with this optimization). I think SmallVec usually is more important when there is a lot of churn on the data being allocated (e.g. a small Vec was being allocated in a function that is called many times or as part of a loop).

@mrLSD mrLSD merged commit dc4e443 into master Oct 15, 2024
6 checks passed
@mrLSD mrLSD deleted the feat/smallvec branch October 15, 2024 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants