I want to execute a function like the following:
function calculateUserVotingPower(address _user) public view returns (uint256 userVotingPower) { IVoteEscrow.NFTInfo[] memory nfts = voteEscrow.getUserAllNFTs(_user); uint256 length = nfts.length; if (length > MAX_NFTS_CONSIDERED) revert TooManyNFTs(); for (uint256 i = 0; i < length; ) { if (nfts[i].locked_end > block.timestamp) { userVotingPower += nfts[i].balanceOf; } unchecked { ++i; } } } So, in such functions, does it save gas to cache block.timestamp variable and use it for comparisons? for eg.
function calculateUserVotingPower(address _user) public view returns (uint256 userVotingPower) { IVoteEscrow.NFTInfo[] memory nfts = voteEscrow.getUserAllNFTs(_user); uint256 length = nfts.length; if (length > MAX_NFTS_CONSIDERED) revert TooManyNFTs(); uint256 timestamp = block.timestamp; for (uint256 i = 0; i < length; ) { if (nfts[i].locked_end > timestamp) { userVotingPower += nfts[i].balanceOf; } unchecked { ++i; } } }
Other than loops, if block.timestamp, msg.sender or address(this) if used multiple times, should they be cached in memory and then used to save gas?