Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: remove unchecked in for loop #253

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
Claim_Integration_Test:test_Claim() (gas: 278831)
Claim_Integration_Test:test_RevertGiven_AlreadyClaimed() (gas: 266771)
Claim_Integration_Test:test_RevertGiven_CampaignExpired() (gas: 17765)
Claim_Integration_Test:test_RevertGiven_ProtocolFeeNotZero() (gas: 84689)
Clawback_Integration_Test:testFuzz_Clawback(address) (runs: 20, μ: 316927, ~: 316927)
Clawback_Integration_Test:testFuzz_Clawback_CampaignNotExpired(address) (runs: 20, μ: 88771, ~: 88771)
Clawback_Integration_Test:test_Clawback() (gas: 272109)
Clawback_Integration_Test:test_RevertGiven_CampaignNotExpired() (gas: 30440)
Constructor_MerkleStreamerLL_Integration_Test:test_Constructor() (gas: 1219550)
CreateMerkleStreamerLL_Integration_Test:testFuzz_CreateMerkleStreamerLL(address,uint40) (runs: 20, μ: 1139893, ~: 1139893)
CreateMerkleStreamerLL_Integration_Test:test_RevertGiven_AlreadyDeployed() (gas: 8937393460516730625)
CreateWithDeltas_Integration_Test:test_BatchCreateWithDeltas() (gas: 2070549)
CreateWithDurations_Integration_Test:test_BatchCreateWithDurations() (gas: 1334021)
CreateWithMilestones_Integration_Test:test_BatchCreateWithMilestones() (gas: 2063959)
CreateWithRange_Integration_Test:test_CreateWithRange() (gas: 1336190)
HasClaimed_Integration_Test:test_HasClaimed() (gas: 251894)
HasClaimed_Integration_Test:test_HasClaimed_IndexNotInTree() (gas: 8011)
HasClaimed_Integration_Test:test_HasClaimed_NotClaimed() (gas: 13268)
HasExpired_Integration_Test:test_HasExpired_ExpirationEqualToCurrentTime() (gas: 13971)
HasExpired_Integration_Test:test_HasExpired_ExpirationGreaterThanCurrentTime() (gas: 14065)
HasExpired_Integration_Test:test_HasExpired_ExpirationLessThanCurrentTime() (gas: 5760)
HasExpired_Integration_Test:test_HasExpired_ExpirationZero() (gas: 1081876)
Claim_Integration_Test:test_Claim() (gas: 278641)
Claim_Integration_Test:test_RevertGiven_AlreadyClaimed() (gas: 266327)
Claim_Integration_Test:test_RevertGiven_CampaignExpired() (gas: 17745)
Claim_Integration_Test:test_RevertGiven_ProtocolFeeNotZero() (gas: 84510)
Clawback_Integration_Test:testFuzz_Clawback(address) (runs: 20, μ: 316725, ~: 316725)
Clawback_Integration_Test:testFuzz_Clawback_CampaignNotExpired(address) (runs: 20, μ: 88746, ~: 88746)
Clawback_Integration_Test:test_Clawback() (gas: 271907)
Clawback_Integration_Test:test_RevertGiven_CampaignNotExpired() (gas: 30429)
Constructor_MerkleStreamerLL_Integration_Test:test_Constructor() (gas: 1198502)
CreateMerkleStreamerLL_Integration_Test:testFuzz_CreateMerkleStreamerLL(address,uint40) (runs: 20, μ: 1126334, ~: 1126334)
CreateMerkleStreamerLL_Integration_Test:test_RevertGiven_AlreadyDeployed() (gas: 8937393460516730624)
CreateWithDeltas_Integration_Test:test_BatchCreateWithDeltas() (gas: 2070068)
CreateWithDurations_Integration_Test:test_BatchCreateWithDurations() (gas: 1333914)
CreateWithMilestones_Integration_Test:test_BatchCreateWithMilestones() (gas: 2063258)
CreateWithRange_Integration_Test:test_CreateWithRange() (gas: 1336083)
HasClaimed_Integration_Test:test_HasClaimed() (gas: 251704)
HasClaimed_Integration_Test:test_HasClaimed_IndexNotInTree() (gas: 7997)
HasClaimed_Integration_Test:test_HasClaimed_NotClaimed() (gas: 13254)
HasExpired_Integration_Test:test_HasExpired_ExpirationEqualToCurrentTime() (gas: 13957)
HasExpired_Integration_Test:test_HasExpired_ExpirationGreaterThanCurrentTime() (gas: 14051)
HasExpired_Integration_Test:test_HasExpired_ExpirationLessThanCurrentTime() (gas: 5746)
HasExpired_Integration_Test:test_HasExpired_ExpirationZero() (gas: 1068623)
MerkleBuilder_Test:testFuzz_ComputeLeaf(uint256,address,uint128) (runs: 20, μ: 1176, ~: 1176)
MerkleBuilder_Test:testFuzz_ComputeLeaves((uint256,address,uint128)[]) (runs: 20, μ: 353773, ~: 397124)
Precompiles_Test:test_DeployBatch() (gas: 2794484)
Precompiles_Test:test_DeployMerkleStreamerFactory() (gas: 3124723)
Precompiles_Test:test_DeployPeriphery() (gas: 5913539)
USDC_CreateWithMilestones_Batch_Fork_Test:testForkFuzz_CreateWithMilestones((uint128,address,address,uint128,uint40,(uint128,uint64,uint40)[])) (runs: 20, μ: 34894028, ~: 21953046)
USDC_CreateWithRange_Batch_Fork_Test:testForkFuzz_CreateWithRange((uint128,(uint40,uint40,uint40),address,address,uint128)) (runs: 20, μ: 1223264, ~: 1038250)
USDC_MerkleStreamerLL_Fork_Test:testForkFuzz_MerkleStreamerLL((address,uint40,(uint256,uint256,uint128)[],uint256)) (runs: 20, μ: 4359652, ~: 3909187)
USDT_CreateWithMilestones_Batch_Fork_Test:testForkFuzz_CreateWithMilestones((uint128,address,address,uint128,uint40,(uint128,uint64,uint40)[])) (runs: 20, μ: 34894028, ~: 21953046)
USDT_CreateWithRange_Batch_Fork_Test:testForkFuzz_CreateWithRange((uint128,(uint40,uint40,uint40),address,address,uint128)) (runs: 20, μ: 1223264, ~: 1038250)
USDT_MerkleStreamerLL_Fork_Test:testForkFuzz_MerkleStreamerLL((address,uint40,(uint256,uint256,uint128)[],uint256)) (runs: 20, μ: 4359652, ~: 3909187)
MerkleBuilder_Test:testFuzz_ComputeLeaves((uint256,address,uint128)[]) (runs: 20, μ: 328702, ~: 368679)
Precompiles_Test:test_DeployBatch() (gas: 2794084)
Precompiles_Test:test_DeployMerkleStreamerFactory() (gas: 3098051)
Precompiles_Test:test_DeployPeriphery() (gas: 5886439)
USDC_CreateWithMilestones_Batch_Fork_Test:testForkFuzz_CreateWithMilestones((uint128,address,address,uint128,uint40,(uint128,uint64,uint40)[])) (runs: 20, μ: 35758280, ~: 21718385)
USDC_CreateWithRange_Batch_Fork_Test:testForkFuzz_CreateWithRange((uint128,(uint40,uint40,uint40),address,address,uint128)) (runs: 20, μ: 1353143, ~: 1508161)
USDC_MerkleStreamerLL_Fork_Test:testForkFuzz_MerkleStreamerLL((address,uint40,(uint256,uint256,uint128)[],uint256)) (runs: 20, μ: 4504409, ~: 3860802)
USDT_CreateWithMilestones_Batch_Fork_Test:testForkFuzz_CreateWithMilestones((uint128,address,address,uint128,uint40,(uint128,uint64,uint40)[])) (runs: 20, μ: 35758280, ~: 21718385)
USDT_CreateWithRange_Batch_Fork_Test:testForkFuzz_CreateWithRange((uint128,(uint40,uint40,uint40),address,address,uint128)) (runs: 20, μ: 1353143, ~: 1508161)
USDT_MerkleStreamerLL_Fork_Test:testForkFuzz_MerkleStreamerLL((address,uint40,(uint256,uint256,uint128)[],uint256)) (runs: 20, μ: 4504409, ~: 3860802)
40 changes: 8 additions & 32 deletions src/SablierV2Batch.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ contract SablierV2Batch is ISablierV2Batch {
// transactions will revert if there is overflow.
uint256 i;
uint256 transferAmount;
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
unchecked {
transferAmount += batch[i].totalAmount;
i += 1;
}
}

Expand All @@ -52,7 +51,7 @@ contract SablierV2Batch is ISablierV2Batch {

// Create a stream for each element in the parameter array.
streamIds = new uint256[](batchSize);
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
// Create the stream.
streamIds[i] = lockupLinear.createWithDurations(
LockupLinear.CreateWithDurations({
Expand All @@ -66,11 +65,6 @@ contract SablierV2Batch is ISablierV2Batch {
transferable: batch[i].transferable
})
);

// Increment the for loop iterator.
unchecked {
i += 1;
}
}
}

Expand All @@ -94,10 +88,9 @@ contract SablierV2Batch is ISablierV2Batch {
// transactions will revert if there is overflow.
uint256 i;
uint256 transferAmount;
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
unchecked {
transferAmount += batch[i].totalAmount;
i += 1;
}
}

Expand All @@ -106,7 +99,7 @@ contract SablierV2Batch is ISablierV2Batch {

// Create a stream for each element in the parameter array.
streamIds = new uint256[](batchSize);
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
// Create the stream.
streamIds[i] = lockupLinear.createWithRange(
LockupLinear.CreateWithRange({
Expand All @@ -120,11 +113,6 @@ contract SablierV2Batch is ISablierV2Batch {
transferable: batch[i].transferable
})
);

// Increment the for loop iterator.
unchecked {
i += 1;
}
}
}

Expand Down Expand Up @@ -152,10 +140,9 @@ contract SablierV2Batch is ISablierV2Batch {
// transactions will revert if there is overflow.
uint256 i;
uint256 transferAmount;
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
unchecked {
transferAmount += batch[i].totalAmount;
i += 1;
}
}

Expand All @@ -164,7 +151,7 @@ contract SablierV2Batch is ISablierV2Batch {

// Create a stream for each element in the parameter array.
streamIds = new uint256[](batchSize);
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
// Create the stream.
streamIds[i] = lockupDynamic.createWithDeltas(
LockupDynamic.CreateWithDeltas({
Expand All @@ -178,11 +165,6 @@ contract SablierV2Batch is ISablierV2Batch {
transferable: batch[i].transferable
})
);

// Increment the for loop iterator.
unchecked {
i += 1;
}
}
}

Expand All @@ -206,10 +188,9 @@ contract SablierV2Batch is ISablierV2Batch {
// transactions will revert if there is overflow.
uint256 i;
uint256 transferAmount;
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
unchecked {
transferAmount += batch[i].totalAmount;
i += 1;
}
}

Expand All @@ -218,7 +199,7 @@ contract SablierV2Batch is ISablierV2Batch {

// Create a stream for each element in the parameter array.
streamIds = new uint256[](batchSize);
for (i = 0; i < batchSize;) {
for (i = 0; i < batchSize; ++i) {
// Create the stream.
streamIds[i] = lockupDynamic.createWithMilestones(
LockupDynamic.CreateWithMilestones({
Expand All @@ -233,11 +214,6 @@ contract SablierV2Batch is ISablierV2Batch {
transferable: batch[i].transferable
})
);

// Increment the for loop iterator.
unchecked {
i += 1;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/utils/ArrayBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ library ArrayBuilder {
returns (uint256[] memory streamIds)
{
streamIds = new uint256[](batchSize);
unchecked {
for (uint256 i = 0; i < batchSize; ++i) {
for (uint256 i = 0; i < batchSize; ++i) {
unchecked {
smol-ninja marked this conversation as resolved.
Show resolved Hide resolved
streamIds[i] = firstStreamId + i;
}
}
Expand Down
16 changes: 8 additions & 8 deletions test/utils/BatchBuilder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ library BatchBuilder {
returns (Batch.CreateWithDeltas[] memory batch)
{
batch = new Batch.CreateWithDeltas[](batchSize);
unchecked {
for (uint256 i = 0; i < batchSize; ++i) {
for (uint256 i = 0; i < batchSize; ++i) {
unchecked {
smol-ninja marked this conversation as resolved.
Show resolved Hide resolved
batch[i] = batchSingle;
}
}
Expand Down Expand Up @@ -55,8 +55,8 @@ library BatchBuilder {
returns (Batch.CreateWithDurations[] memory batch)
{
batch = new Batch.CreateWithDurations[](batchSize);
unchecked {
for (uint256 i = 0; i < batchSize; ++i) {
for (uint256 i = 0; i < batchSize; ++i) {
unchecked {
smol-ninja marked this conversation as resolved.
Show resolved Hide resolved
batch[i] = batchSingle;
}
}
Expand Down Expand Up @@ -94,8 +94,8 @@ library BatchBuilder {
returns (Batch.CreateWithMilestones[] memory batch)
{
batch = new Batch.CreateWithMilestones[](batchSize);
unchecked {
for (uint256 i = 0; i < batchSize; ++i) {
for (uint256 i = 0; i < batchSize; ++i) {
unchecked {
smol-ninja marked this conversation as resolved.
Show resolved Hide resolved
batch[i] = batchSingle;
}
}
Expand Down Expand Up @@ -134,8 +134,8 @@ library BatchBuilder {
returns (Batch.CreateWithRange[] memory batch)
{
batch = new Batch.CreateWithRange[](batchSize);
unchecked {
for (uint256 i = 0; i < batchSize; ++i) {
for (uint256 i = 0; i < batchSize; ++i) {
unchecked {
smol-ninja marked this conversation as resolved.
Show resolved Hide resolved
batch[i] = batchSingle;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/utils/Murky.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ abstract contract MurkyBase {
// proof length must be less than max array size
bytes32 rollingHash = valueToProve;
uint256 length = proof.length;
unchecked {
for (uint256 i = 0; i < length; ++i) {
for (uint256 i = 0; i < length; ++i) {
unchecked {
smol-ninja marked this conversation as resolved.
Show resolved Hide resolved
rollingHash = hashLeafPairs(rollingHash, proof[i]);
}
}
Expand Down