From 8b2f29ceb0db64957a49deb63af7792772bd7a5d Mon Sep 17 00:00:00 2001 From: Renan Souza Date: Wed, 6 Mar 2024 05:37:20 -0300 Subject: [PATCH] Optimize Clone.sol yul (#4927) --- contracts/proxy/Clones.sol | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/contracts/proxy/Clones.sol b/contracts/proxy/Clones.sol index 92ed339a7..ffbc8f10c 100644 --- a/contracts/proxy/Clones.sol +++ b/contracts/proxy/Clones.sol @@ -28,11 +28,12 @@ library Clones { function clone(address implementation) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes - // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) - // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + // Stores the bytecode after address + mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3) + // implementation address + mstore(0x11, implementation) + // Packs the first 3 bytes of the `implementation` address with the bytecode before the address. + mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) instance := create(0, 0x09, 0x37) } if (instance == address(0)) { @@ -50,11 +51,12 @@ library Clones { function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) { /// @solidity memory-safe-assembly assembly { - // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes - // of the `implementation` address with the bytecode before the address. - mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) - // Packs the remaining 17 bytes of `implementation` with the bytecode after the address. - mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3)) + // Stores the bytecode after address + mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3) + // implementation address + mstore(0x11, implementation) + // Packs the first 3 bytes of the `implementation` address with the bytecode before the address. + mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000)) instance := create2(0, 0x09, 0x37, salt) } if (instance == address(0)) {