Skip to content

feat: optimize allocations in writable CoW accounts#4846

Closed
bmuddha wants to merge 3 commits intoanza-xyz:masterfrom
magicblock-labs:realloc-optimization
Closed

feat: optimize allocations in writable CoW accounts#4846
bmuddha wants to merge 3 commits intoanza-xyz:masterfrom
magicblock-labs:realloc-optimization

Conversation

@bmuddha
Copy link
Copy Markdown

@bmuddha bmuddha commented Feb 7, 2025

Problem

Unnecessary allocations in CoW accounts' data field, during the first write access from within the SVM. Potentially, instruction might just write to an account without reallocating, and thus premature call to Vec::reserve introduces an overhead which could be avoided.

Summary of Changes

This performance optimization removes the unnecessary calls to Vec::reserve (on AccountSharedData.data field). Instead when realloc is detected during deserialization of SVM buffer back into account, extra allocation happens on demand. This should decrease the pressure on allocator when instructions writing to the account do not reallocate, by avoiding allocation where possible.

This feature removes the unnecessary calls to Vec::reserve
(AccountSharedData.data field) Instead when realloc is detected during
deserialization of SVM buffer back into account, extra allocation
happens on demand. This should decrease the pressure on allocator when
transactions writing to the account do not reallocate, by avoiding
allocation where possible.
@bmuddha bmuddha requested a review from a team as a code owner February 7, 2025 09:11
@mergify
Copy link
Copy Markdown

mergify Bot commented Feb 7, 2025

The Firedancer team maintains a line-for-line reimplementation of the
native programs, and until native programs are moved to BPF, those
implementations must exactly match their Agave counterparts.
If this PR represents a change to a native program implementation (not
tests), please include a reviewer from the Firedancer team. And please
keep refactors to a minimum.

@Lichtso Lichtso requested a review from seanyoung February 7, 2025 13:21
@LucasSte
Copy link
Copy Markdown

LucasSte commented Feb 7, 2025

I believe some benchmarks could be run to make the case for this change. Maybe @seanyoung has some suggestions.

@alessandrod alessandrod self-requested a review February 7, 2025 14:10
@seanyoung
Copy link
Copy Markdown

Needs a careful review. I'll go over it a few times.

Comment on lines +979 to +982
if additional > MAX_PERMITTED_DATA_INCREASE {
self.account
.reserve(additional.saturating_sub(MAX_PERMITTED_DATA_INCREASE));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I know it was previously already the case and this condition just makes it better but the double reallocation does feel like a big waste. Especially in such case where the data size is know to be pretty big already.
I wonder if the introduction of a "make mut with additional size" or something of the sort would be acceptable.

Also i'm unsure if it is actually possible for additional to be > MAX_PERMITTED_DATA_INCREASE but since its been renamed from MAX_PERMITTED_DATA_LENGTH it may now/soon be the case.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

But overall feels like a very positive change - everything that can speed up execution is more than welcomed...

@bmuddha
Copy link
Copy Markdown
Author

bmuddha commented Feb 28, 2025

@seanyoung just a quick reminder on this PR, any progress in reviewing it?

@bmuddha
Copy link
Copy Markdown
Author

bmuddha commented Mar 10, 2025

Hey guys, do you think this can be merged any time soon? Let me know if there're some issues that need to be addressed first

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 4, 2026

This pull request is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions Bot added the stale label Feb 4, 2026
@Lichtso
Copy link
Copy Markdown

Lichtso commented Feb 4, 2026

I think this has been implicitly addressed in #5871. See

account.resize(new_len, 0);

In short: If the program wants to grow the account we grow it by the max +10 KiB in one go as doing it in multiple increments would be slower for large accounts, which have to copy all the data to a new allocation.

@github-actions github-actions Bot removed the stale label Feb 5, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 6, 2026

This pull request is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions Bot added the stale label Apr 6, 2026
@Lichtso Lichtso closed this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants