Skip to content

Conversation

borj404
Copy link

@borj404 borj404 commented Aug 19, 2025

Description

Eliminates redundant type casting in ReentrancyGuardTransient by pre-computing the BooleanSlot wrapper at compile-time.

Edit: Updated to support the new nonReentrantView modifier introduced in the latest version.

Current implementation

Current implementation performs asBoolean()BooleanSlot.wrap() three times per nonReentrant execution (10 gas each).

Optimization

Pre-computed constant:

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

Gas Savings

19 gas per nonReentrant call (verified via test suite)

Method Original gas Optimized gas Savings Improvement
callback() 44,007 43,988 19 gas 0.04%
guardedCheckEntered() 21,952 21,933 19 gas 0.09%

Deployment overhead: 1,728 gas (amortized after ~91 transactions)

Testing

Impact

  • Zero security trade-offs.
  • Full backward compatibility.
  • Supports both nonReentrant and nonReentrantView modifiers.

@borj404 borj404 requested a review from a team as a code owner August 19, 2025 17:26
Copy link

changeset-bot bot commented Aug 19, 2025

🦋 Changeset detected

Latest commit: 7627ddb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openzeppelin-solidity Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@borj404 borj404 force-pushed the perf/reentrancy-guard-precompute-booleanslot branch from 5b77380 to c07b8d2 Compare August 22, 2025 18:31
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