Skip to content

Gas optimization: Cache BooleanSlot in ReentrancyGuardTransient #5837

@borj404

Description

@borj404

Small gas optimization in ReentrancyGuardTransient.sol by pre-computing the BooleanSlot to avoid repeated type conversions.

Current:

REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true);

Proposed:

TransientSlot.BooleanSlot private constant REENTRANCY_SLOT =
    TransientSlot.BooleanSlot.wrap(REENTRANCY_GUARD_STORAGE);

// then use: REENTRANCY_SLOT.tstore(true);

Benefits

  • Eliminates redundant .asBoolean() calls (currently called 3 times per nonReentrant execution).
  • No functional changes. Same storage slot, same behavior.
  • All existing tests pass without modification.
  • Testing shows contracts save 1.3K gas at deployment plus 30 gas per nonReentrant modifier execution.

Would maintainers be interested in this micro-optimization? Happy to submit a PR if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions