Skip to content

Commit 8b2f29c

Browse files
authored
Optimize Clone.sol yul (#4927)
1 parent 7417c59 commit 8b2f29c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

contracts/proxy/Clones.sol

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ library Clones {
2828
function clone(address implementation) internal returns (address instance) {
2929
/// @solidity memory-safe-assembly
3030
assembly {
31-
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
32-
// of the `implementation` address with the bytecode before the address.
33-
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
34-
// Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
35-
mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
31+
// Stores the bytecode after address
32+
mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3)
33+
// implementation address
34+
mstore(0x11, implementation)
35+
// Packs the first 3 bytes of the `implementation` address with the bytecode before the address.
36+
mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
3637
instance := create(0, 0x09, 0x37)
3738
}
3839
if (instance == address(0)) {
@@ -50,11 +51,12 @@ library Clones {
5051
function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
5152
/// @solidity memory-safe-assembly
5253
assembly {
53-
// Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
54-
// of the `implementation` address with the bytecode before the address.
55-
mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
56-
// Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
57-
mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
54+
// Stores the bytecode after address
55+
mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3)
56+
// implementation address
57+
mstore(0x11, implementation)
58+
// Packs the first 3 bytes of the `implementation` address with the bytecode before the address.
59+
mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
5860
instance := create2(0, 0x09, 0x37, salt)
5961
}
6062
if (instance == address(0)) {

0 commit comments

Comments
 (0)