Skip to content

⚡️ Optimize ERC20 slot computation with bitwise operations#1507

Open
seinmyung25 wants to merge 1 commit intoVectorized:mainfrom
seinmyung25:perf/erc20-optimize-balance-slot
Open

⚡️ Optimize ERC20 slot computation with bitwise operations#1507
seinmyung25 wants to merge 1 commit intoVectorized:mainfrom
seinmyung25:perf/erc20-optimize-balance-slot

Conversation

@seinmyung25
Copy link
Copy Markdown

Description

Replaces mstore + keccak256 with bitwise operations for balance slot computation in ERC20.

Pattern change:

// Before
mstore(0x0c, _BALANCE_SLOT_SEED)
mstore(0x00, owner)
result := sload(keccak256(0x0c, 0x20))

// After
result := sload(or(shl(224, _BALANCE_SLOT_SEED), shr(96, shl(96, owner))))

Functions optimized: balanceOf, transfer, transferFrom, _mint, _burn, _transfer

Impact: Reduced gas cost through fewer memory operations and direct bitwise slot computation.

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant