Skip to content

Commit

Permalink
⚡️ LibClone argsOn reduce copied length (#1227)
Browse files Browse the repository at this point in the history
Co-authored-by: philogy <[email protected]>
  • Loading branch information
Vectorized and Philogy authored Dec 17, 2024
1 parent c312283 commit ccaed15
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/utils/LibClone.sol
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,8 @@ library LibClone {
assembly {
args := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(instance), 0x2d))
extcodecopy(instance, add(args, 0x20), add(start, 0x2d), add(n, 0x20))
let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
extcodecopy(instance, add(args, 0x20), add(start, 0x2d), add(l, 0x20))
mstore(args, mul(sub(n, start), lt(start, n))) // Store the length.
mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory.
}
Expand Down Expand Up @@ -1144,7 +1145,8 @@ library LibClone {
assembly {
args := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(instance), 0x3d))
extcodecopy(instance, add(args, 0x20), add(start, 0x3d), add(n, 0x20))
let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
extcodecopy(instance, add(args, 0x20), add(start, 0x3d), add(l, 0x20))
mstore(args, mul(sub(n, start), lt(start, n))) // Store the length.
mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory.
}
Expand Down Expand Up @@ -1644,7 +1646,8 @@ library LibClone {
assembly {
args := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(instance), 0x52))
extcodecopy(instance, add(args, 0x20), add(start, 0x52), add(n, 0x20))
let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
extcodecopy(instance, add(args, 0x20), add(start, 0x52), add(l, 0x20))
mstore(args, mul(sub(n, start), lt(start, n))) // Store the length.
mstore(0x40, add(mload(args), add(args, 0x40))) // Allocate memory.
}
Expand Down Expand Up @@ -2239,7 +2242,8 @@ library LibClone {
assembly {
args := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(instance), 0x52))
extcodecopy(instance, add(args, 0x20), add(start, 0x52), add(n, 0x20))
let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
extcodecopy(instance, add(args, 0x20), add(start, 0x52), add(l, 0x20))
mstore(args, mul(sub(n, start), lt(start, n))) // Store the length.
mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory.
}
Expand Down Expand Up @@ -2741,7 +2745,8 @@ library LibClone {
assembly {
args := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(instance), 0x57))
extcodecopy(instance, add(args, 0x20), add(start, 0x57), add(n, 0x20))
let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
extcodecopy(instance, add(args, 0x20), add(start, 0x57), add(l, 0x20))
mstore(args, mul(sub(n, start), lt(start, n))) // Store the length.
mstore(0x40, add(args, add(0x40, mload(args)))) // Allocate memory.
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/SSTORE2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ library SSTORE2 {
assembly {
data := mload(0x40)
let n := and(0xffffffffff, sub(extcodesize(pointer), 0x01))
extcodecopy(pointer, add(data, 0x1f), start, add(n, 0x21))
let l := sub(n, and(0xffffff, mul(lt(start, n), start)))
extcodecopy(pointer, add(data, 0x1f), start, add(l, 0x21))
mstore(data, mul(sub(n, start), lt(start, n))) // Store the length.
mstore(0x40, add(data, add(0x40, mload(data)))) // Allocate memory.
}
Expand Down
2 changes: 2 additions & 0 deletions test/DeploylessPredeployQueryer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ contract DeploylessPredeployQueryerTest is SoladyTest {
}

function testTargetGenerate() public {
vm.pauseGasMetering();
Target target = new Target();
for (uint256 i; i < 16; ++i) {
bytes memory seed = _randomBytes();
assertEq(target.generate(seed), RandomBytesGeneratorLib.generate(seed));
}
vm.resumeGasMetering();
}

function testPredeployQueryer(bytes32 salt) public {
Expand Down

0 comments on commit ccaed15

Please sign in to comment.