Skip to content

refactor: effective balance increments#213

Merged
wemeetagain merged 3 commits intomainfrom
bing/refactor-effective-balance-increments
Feb 23, 2026
Merged

refactor: effective balance increments#213
wemeetagain merged 3 commits intomainfrom
bing/refactor-effective-balance-increments

Conversation

@spiral-ladder
Copy link
Copy Markdown
Collaborator

@spiral-ladder spiral-ladder commented Feb 11, 2026

@spiral-ladder spiral-ladder self-assigned this Feb 11, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @spiral-ladder, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request streamlines the management of effective balance increments within the state transition system. It standardizes naming conventions and refactors the underlying initialization and resizing mechanisms for these increments, leading to more consistent and potentially more performant memory handling.

Highlights

  • Variable Renaming: The variable effective_balance_increment has been consistently renamed to effective_balance_increments across multiple files for improved clarity and pluralization.
  • Refactored Initialization Logic: The effective_balance_increments.zig file was refactored to consolidate multiple initialization functions into a single effectiveBalanceIncrementsInit function, which now handles capacity pre-allocation and zeroing more efficiently.
  • EpochCache Updates: The EpochCache struct and its methods were updated to reflect the variable rename and to utilize the new effectiveBalanceIncrementsInit function for managing effective balance increments, including improved logic for extending the array while preserving existing data.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/state_transition/block/process_attestation_altair.zig
    • Renamed effective_balance_increment to effective_balance_increments.
  • src/state_transition/block/slash_validator.zig
    • Renamed effective_balance_increment to effective_balance_increments.
  • src/state_transition/cache/effective_balance_increments.zig
    • Removed getEffectiveBalanceIncrementsZeroed, getEffectiveBalanceIncrementsWithLen, and getEffectiveBalanceIncrements functions.
    • Introduced effectiveBalanceIncrementsInit to handle allocation, capacity setting, and zeroing of increments.
    • Added a descriptive comment for the new effectiveBalanceIncrementsInit function.
  • src/state_transition/cache/epoch_cache.zig
    • Updated import statement to use effectiveBalanceIncrementsInit.
    • Renamed the effective_balance_increment field to effective_balance_increments within the EpochCache struct.
    • Modified initialization, cloning, and release logic to use the new effective_balance_increments field name.
    • Updated the getEffectiveBalanceIncrements utility method to reference the new field name.
    • Refactored effectiveBalanceIncrementsSet to use effectiveBalanceIncrementsInit and memcpy for extending the array while preserving existing data.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the handling of effective_balance_increments by renaming the field for clarity and improving its initialization logic. The changes are consistent and also fix a bug in effectiveBalanceIncrementsSet where existing data was not preserved during resizing. My feedback includes a couple of suggestions for minor improvements to make the code more idiomatic and efficient.

Comment on lines +530 to +531
var effective_balance_increments = try EffectiveBalanceIncrements.initCapacity(self.allocator, self.effective_balance_increments.get().items.len);
try effective_balance_increments.appendSlice(self.effective_balance_increments.get().items);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This manual clone of the ArrayList can be simplified by using the built-in clone() method, which is a clearer and more idiomatic abstraction provided by the standard library.

        var effective_balance_increments = try self.effective_balance_increments.get().clone();
References
  1. The style guide recommends using a minimum of excellent abstractions that make the best sense of the domain. Using clone() is a good example of leveraging a standard library abstraction for clarity and correctness. (link)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Comment on lines +768 to +772
const old = self.effective_balance_increments.get();
var new_increments = try effectiveBalanceIncrementsInit(self.allocator, index + 1);
@memcpy(new_increments.items[0..old.items.len], old.items);
self.effective_balance_increments.release();
self.effective_balance_increments = try EffectiveBalanceIncrementsRc.init(allocator, new_increments);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation is functionally correct but can be made more efficient. It first zeroes out the entire new array via effectiveBalanceIncrementsInit and then overwrites a portion of it with memcpy. A more optimal approach would be to allocate an uninitialized array, copy the old data, and then zero-fill only the newly added portion. This avoids the redundant work of zeroing data that is immediately overwritten. This aligns with the style guide's emphasis on performance.

            const old = self.effective_balance_increments.get();
            const new_len = index + 1;
            const capacity = 1024 * @divFloor(new_len + 1024, 1024);
            var new_increments = try EffectiveBalanceIncrements.initCapacity(self.allocator, capacity);
            errdefer new_increments.deinit();

            new_increments.items.len = new_len;
            @memcpy(new_increments.items[0..old.items.len], old.items);
            @memset(new_increments.items[old.items.len..new_len], 0);

            self.effective_balance_increments.release();
            self.effective_balance_increments = try EffectiveBalanceIncrementsRc.init(allocator, new_increments);
References
  1. The style guide encourages thinking about performance from the design phase. This suggestion improves performance by avoiding redundant memory operations. (link)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

88a19f2

I'm a bit conflicted about this and the usage of effectiveBalanceIncrementsInit but I think this makes more sense even if we have to duplicate some code

@spiral-ladder spiral-ladder marked this pull request as ready for review February 11, 2026 15:07
@spiral-ladder spiral-ladder requested a review from a team as a code owner February 11, 2026 15:07
@spiral-ladder spiral-ladder force-pushed the bing/refactor-effective-balance-increments branch from 88a19f2 to c309acb Compare February 23, 2026 06:53
@wemeetagain wemeetagain merged commit 413c1e8 into main Feb 23, 2026
10 checks passed
twoeths pushed a commit that referenced this pull request Mar 3, 2026
- pluralize the term
- fix init issues found in
#173 leading to spec tests
failure
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.

2 participants