Skip to content

Commit

Permalink
perf(Bundler): reduce gas usage w/ unchecked increments in batch fn l…
Browse files Browse the repository at this point in the history
…oops and cached user value (#305)

perf: add uncheck for increment and cache user

Co-authored-by: e6f4e37l <[email protected]>
  • Loading branch information
horsefacts and fiveoutofnine authored Aug 1, 2023
1 parent 3d36634 commit e7c0389
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BundleRegistryGasUsageTest:testGasRegisterWithSig() (gas: 826601)
BundleRegistryGasUsageTest:testGasTrustedBatchRegister() (gas: 6742022)
BundleRegistryGasUsageTest:testGasTrustedBatchRegister() (gas: 6644222)
BundleRegistryGasUsageTest:testGasTrustedRegister() (gas: 852171)
IdRegistryGasUsageTest:testGasRegister() (gas: 734576)
IdRegistryGasUsageTest:testGasRegisterForAndRecover() (gas: 1704136)
Expand Down
21 changes: 16 additions & 5 deletions src/Bundler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ contract Bundler is TrustedCaller {
uint256 fid =
idRegistry.registerFor(registration.to, registration.recovery, registration.deadline, registration.sig);

for (uint256 i; i < signers.length; i++) {
for (uint256 i; i < signers.length;) {
SignerParams calldata signer = signers[i];
keyRegistry.addFor(registration.to, signer.scheme, signer.key, signer.metadata, signer.deadline, signer.sig);

// We know this will not overflow because it's less than the length of the array, which is a `uint256`.
unchecked {
++i;
}
}

uint256 overpayment = storageRegistry.rent{value: msg.value}(fid, storageUnits);
Expand Down Expand Up @@ -152,10 +157,16 @@ contract Bundler is TrustedCaller {
*/
function trustedBatchRegister(UserData[] calldata users) external onlyTrustedCaller {
// Safety: calls inside a loop are safe since caller is trusted
for (uint256 i = 0; i < users.length; i++) {
uint256 fid = idRegistry.trustedRegister(users[i].to, users[i].recovery);
keyRegistry.trustedAdd(users[i].to, users[i].scheme, users[i].key, users[i].metadata);
storageRegistry.credit(fid, users[i].units);
for (uint256 i; i < users.length;) {
UserData calldata user = users[i];
uint256 fid = idRegistry.trustedRegister(user.to, user.recovery);
keyRegistry.trustedAdd(user.to, user.scheme, user.key, user.metadata);
storageRegistry.credit(fid, user.units);

// We know this will not overflow because it's less than the length of the array, which is a `uint256`.
unchecked {
++i;
}
}
}

Expand Down

0 comments on commit e7c0389

Please sign in to comment.