From 023a9fb5b45cf0bb07815549f4f605316388a198 Mon Sep 17 00:00:00 2001 From: Aryan Malik <88618913+Aryan9592@users.noreply.github.com> Date: Thu, 12 Oct 2023 00:09:23 +0530 Subject: [PATCH] Update AttestationRegistry.sol: Optimized 'For' loops --- contracts/src/AttestationRegistry.sol | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/contracts/src/AttestationRegistry.sol b/contracts/src/AttestationRegistry.sol index 472aa558..863d8b3d 100644 --- a/contracts/src/AttestationRegistry.sol +++ b/contracts/src/AttestationRegistry.sol @@ -121,13 +121,16 @@ contract AttestationRegistry is OwnableUpgradeable { * @param attestationsPayloads the attestations payloads to create attestations and register them */ function bulkAttest(AttestationPayload[] calldata attestationsPayloads, address attester) public { - for (uint256 i = 0; i < attestationsPayloads.length; i++) { + for (uint256 i; i < attestationsPayloads.length;) { attest(attestationsPayloads[i], attester); + unchecked { + ++i; + } } } function massImport(AttestationPayload[] calldata attestationsPayloads, address portal) public onlyOwner { - for (uint256 i = 0; i < attestationsPayloads.length; i++) { + for (uint256 i; i < attestationsPayloads.length;) { // Auto increment attestation counter attestationIdCounter++; bytes32 id = bytes32(abi.encode(attestationIdCounter)); @@ -147,6 +150,10 @@ contract AttestationRegistry is OwnableUpgradeable { attestationsPayloads[i].attestationData ); emit AttestationRegistered(id); + + unchecked { + ++i; + } } } @@ -203,8 +210,11 @@ contract AttestationRegistry is OwnableUpgradeable { * @param attestationIds the IDs of the attestations to revoke */ function bulkRevoke(bytes32[] memory attestationIds) external { - for (uint256 i = 0; i < attestationIds.length; i++) { + for (uint256 i; i < attestationIds.length;) { revoke(attestationIds[i]); + unchecked { + ++i; + } } } @@ -287,8 +297,11 @@ contract AttestationRegistry is OwnableUpgradeable { function balanceOfBatch(address[] memory accounts, uint256[] memory ids) public view returns (uint256[] memory) { if (accounts.length != ids.length) revert ArrayLengthMismatch(); uint256[] memory result = new uint256[](accounts.length); - for (uint256 i = 0; i < accounts.length; i++) { + for (uint256 i; i < accounts.length;) { result[i] = balanceOf(accounts[i], ids[i]); + unchecked { + ++i; + } } return result; }