From 9e2ba99dfcf5f145fd68c5eea42d899836d9571b Mon Sep 17 00:00:00 2001 From: Carson Date: Thu, 9 May 2024 15:30:01 -0700 Subject: [PATCH] fix gas loop optimization in MerkleProof.sol | caching length (#5028) --- contracts/utils/cryptography/MerkleProof.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/contracts/utils/cryptography/MerkleProof.sol b/contracts/utils/cryptography/MerkleProof.sol index 7b9678e32b..a9f7f37f78 100644 --- a/contracts/utils/cryptography/MerkleProof.sol +++ b/contracts/utils/cryptography/MerkleProof.sol @@ -50,7 +50,8 @@ library MerkleProof { */ function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) { bytes32 computedHash = leaf; - for (uint256 i; i < proof.length; ) { + uint256 len = proof.length; + for (uint256 i; i < len; ) { computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]); unchecked { ++i; @@ -64,7 +65,8 @@ library MerkleProof { */ function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) { bytes32 computedHash = leaf; - for (uint256 i; i < proof.length; ) { + uint256 len = proof.length; + for (uint256 i; i < len; ) { computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]); unchecked { ++i;