-
Notifications
You must be signed in to change notification settings - Fork 11
perf: use saturating arithmetic in balance updates for SIMD vectorization #282
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,14 +31,19 @@ pub fn processRewardsAndPenalties( | |
| const balances = try state.balancesSlice(allocator); | ||
| defer allocator.free(balances); | ||
|
|
||
| // Use saturating arithmetic instead of checked (`std.math.add`): | ||
| // - Overflow is impossible: max ETH supply ~120M ETH = ~1.2e17 Gwei, | ||
| // u64 max = ~1.8e19, giving 154x headroom even with all rewards summed. | ||
| // - Saturating ops are branchless, enabling SIMD auto-vectorization of the | ||
| // balance loop (~3.4x speedup on the inner loop for 500k+ validators). | ||
| if (slashing_penalties) |slashings| { | ||
| for (rewards, penalties, balances, 0..) |reward, penalty, *balance, i| { | ||
| const slashing: u64 = if (i < slashings.len) slashings[i] else 0; | ||
| balance.* = (try std.math.add(u64, balance.*, reward)) -| penalty -| slashing; | ||
| balance.* = ((balance.* +| reward) -| penalty) -| slashing; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To uphold the style guide's emphasis on safety ( From the style guide:
I've also removed a redundant set of parentheses for cleaner code. References
|
||
| } | ||
| } else { | ||
| for (rewards, penalties, balances) |reward, penalty, *balance| { | ||
| balance.* = (try std.math.add(u64, balance.*, reward)) -| penalty; | ||
| balance.* = (balance.* +| reward) -| penalty; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, please add a References
|
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is very helpful for explaining the rationale. To better align with the style guide's preference for prose in comments, consider rephrasing it as a paragraph.
From the style guide:
References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 29d60ec. Added
std.debug.assertfor the invariant and rewrote the comment as prose.