Skip to content

Commit c07b8d2

Browse files
committed
chore: add changeset
1 parent d18634a commit c07b8d2

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

.changeset/thick-goats-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': patch
3+
---
4+
5+
Optimize ReentrancyGuardTransient by pre-computing BooleanSlot wrapper to save 30 gas per call

contracts/utils/ReentrancyGuardTransient.sol

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ abstract contract ReentrancyGuardTransient {
2323
* @dev Pre-computed BooleanSlot type wrapper for the reentrancy guard storage slot.
2424
* This optimization avoids the overhead of repeatedly calling asBoolean() which performs
2525
* a bytes32.wrap() operation on each invocation. By pre-computing the wrapped type at compile-time,
26-
* this eliminates redundant type casting operations in _nonReentrantBefore(), _nonReentrantAfter(),
27-
* and _reentrancyGuardEntered(), reducing gas consumption by 10 gas per call
28-
* to asBoolean() (30 gas total per nonReentrant modifier execution).
26+
* this eliminates 3 redundant type casting operations per nonReentrant modifier execution,
27+
* reducing gas consumption by 19 gas.
2928
*/
3029
TransientSlot.BooleanSlot private constant REENTRANCY_SLOT =
3130
TransientSlot.BooleanSlot.wrap(REENTRANCY_GUARD_STORAGE);
@@ -48,11 +47,28 @@ abstract contract ReentrancyGuardTransient {
4847
_nonReentrantAfter();
4948
}
5049

51-
function _nonReentrantBefore() private {
52-
// On the first call to nonReentrant, REENTRANCY_SLOT.tload() will be false
50+
/**
51+
* @dev A `view` only version of {nonReentrant}. Use to block view functions
52+
* from being called, preventing reading from inconsistent contract state.
53+
*
54+
* CAUTION: This is a "view" modifier and does not change the reentrancy
55+
* status. Use it only on view functions. For payable or non-payable functions,
56+
* use the standard {nonReentrant} modifier instead.
57+
*/
58+
modifier nonReentrantView() {
59+
_nonReentrantBeforeView();
60+
_;
61+
}
62+
63+
function _nonReentrantBeforeView() private view {
5364
if (_reentrancyGuardEntered()) {
5465
revert ReentrancyGuardReentrantCall();
5566
}
67+
}
68+
69+
function _nonReentrantBefore() private {
70+
// On the first call to nonReentrant, REENTRANCY_SLOT.tload() will be false
71+
_nonReentrantBeforeView();
5672

5773
// Any calls to nonReentrant after this point will fail
5874
REENTRANCY_SLOT.tstore(true);

0 commit comments

Comments
 (0)