Skip to content

reuse VertexRef instance on update #3444

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

Merged
merged 3 commits into from
Jul 4, 2025
Merged

reuse VertexRef instance on update #3444

merged 3 commits into from
Jul 4, 2025

Conversation

arnetheduck
Copy link
Member

@arnetheduck arnetheduck commented Jul 3, 2025

When updates to the MPT happen, a new VertexRef is allocated every time - this keeps the code simple but has the significant downside that updates cause unnecessary allocations.

Instead of allocating a new VertexRef on every update, we can update the existing one provided that it is not shared. We can prevent it from being shared by copying it eagerly when it's added to the layer. A downside of this approach is that we also have to make a copy when invalidating hash keys, which affects branch nodes mainly.

The tradeoff seems well worth it though, specially for imports that clock a nice perf boost, like in this little test:

(21005462, 21008193]  14.46  15.50  2,479.35  2,656.98  9m26s  8m48s   7.16%   7.16%   -6.69%
(21013654, 21016385]  15.28  16.14  2,523.74  2,665.83  8m56s  8m27s   5.63%   5.63%   -5.33%
(21021846, 21024577]  15.52  17.66  2,539.25  2,889.61  8m47s  7m43s  13.80%  13.80%  -12.12%

blocks: 16384, baseline: 27m10s, contender: 24m59s
Time (total): -2m10s, -8.00%

When updates to the MPT happen, a new VertexRef is allocated every time - this
keeps the code simple but has the significant downside that updates cause
unnecessary allocations.

Instead of allocating a new `VertexRef` on every update, we can update the
existing one provided that it is not shared. We can prevent it from being shared
by copying it eagerly when it's added to the layer. A downside of this approach
is that we also have to make a copy when invalidating hash keys, which affects
branch and account nodes mainly.

The tradeoff seems well worth it though, specially for imports that clock a nice
perf boost, like in this little test:

```
(21005462, 21008193]  14.46  15.50  2,479.35  2,656.98  9m26s  8m48s   7.16%   7.16%   -6.69%
(21013654, 21016385]  15.28  16.14  2,523.74  2,665.83  8m56s  8m27s   5.63%   5.63%   -5.33%
(21021846, 21024577]  15.52  17.66  2,539.25  2,889.61  8m47s  7m43s  13.80%  13.80%  -12.12%

blocks: 16384, baseline: 27m10s, contender: 24m59s
Time (total): -2m10s, -8.00%
```
arnetheduck added a commit that referenced this pull request Jul 3, 2025
Deferred GC seemed like a good idea to reduce the amount of work done
during block processing, but a side effect of this is that more memory
ends up being allocated in certain workloads which in turn causes an
overall slowdown, with a long test showing a net performance effect that
hovers around 0% and more memory usage.

In particular, the troublesome range around 2M sees a 10-15% slowdown
and an ugly memory usage spike.

Reverting for now - it might be worth revisiting in the future under
different memory allocation patters, but as usual, it's better to not do
work at all (like in #3444) than to do work faster.

This reverts commit 3a00915.
arnetheduck added a commit that referenced this pull request Jul 4, 2025
Deferred GC seemed like a good idea to reduce the amount of work done
during block processing, but a side effect of this is that more memory
ends up being allocated in certain workloads which in turn causes an
overall slowdown, with a long test showing a net performance effect that
hovers around 0% and more memory usage.

In particular, the troublesome range around 2M sees a 10-15% slowdown
and an ugly memory usage spike.

Reverting for now - it might be worth revisiting in the future under
different memory allocation patters, but as usual, it's better to not do
work at all (like in #3444) than to do work faster.

This reverts commit 3a00915.
@arnetheduck arnetheduck merged commit 00d2ad4 into master Jul 4, 2025
23 checks passed
@arnetheduck arnetheduck deleted the reuse-vtx branch July 4, 2025 10:28
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.

1 participant