The _balanceOf function is designed to be a gas-efficient implementation for retrieving token balances using inline assembly to minimize overhead
The function contains two gas inefficiencies: (1) an unnecessary memory write of zero to add(ptr, 0x20) immediately before return, and (2) an unnecessary intermediate variable amount that adds extra stack operations when the result could be written directly to memory
These gas inefficiencies occur whenever _balanceOf is called within state-changing transactions such as transfer, transferFrom, mint, or burn operations
The wasteful operations execute unconditionally in 100% of internal balance queries during paid transactions
High-frequency DeFi operations that check balances multiple times per transaction will multiply this overhead
Additional ~8 gas wasted per internal balance query:
~6 gas from unnecessary memory write (3 gas ADD + 3 gas MSTORE)
~1-2 gas from unnecessary stack operations with intermediate variable
Contradicts the contract's stated goal of being "maximally gas efficient"
Cumulative gas waste across thousands of transactions increases costs for all users
Reduces competitiveness compared to other gas-optimized token implementations in the ecosystem
The following test demonstrates the gas waste by comparing execution costs with and without the unnecessary operations:
When _balanceOf is invoked during state-changing operations like transfers, the unnecessary operations add 7-8 gas overhead. The intermediate amount variable requires additional stack manipulation (POP to store, then load back for mstore), and the zero memory write serves no purpose. While this may seem minor for a single call, token contracts process millions of transactions, making even small optimizations significant for a "maximally gas efficient" implementation.
Remove both gas inefficiencies by eliminating the unnecessary memory write and the intermediate variable:
Code change:
Issue 1 - Unnecessary memory write:
In the EVM, memory exists only for the duration of the current call context and is automatically discarded after execution completes. Writing zero to add(ptr, 0x20) serves no purpose because:
The function immediately returns after this operation, terminating the call context
No subsequent code reads from this memory location
Memory cleanup provides no gas refund (unlike storage SSTORE operations)
We only return the first 32 bytes (0x20) starting from ptr, so the second slot is never used
Issue 2 - Unnecessary intermediate variable:
The amount variable creates unnecessary stack operations:
let amount := sload(dataSlot) - POPs the SLOAD result from stack into a variable
mstore(ptr, amount) - Loads the variable back to stack, then executes MSTORE
By writing mstore(ptr, sload(dataSlot)) directly:
SLOAD result stays on the stack
MSTORE consumes it directly from stack
Eliminates redundant stack manipulation operations
Combined benefit:
These optimizations save approximately 7-8 gas per internal balance query:
~6 gas from removing unnecessary ADD and MSTORE opcodes
~1-2 gas from eliminating redundant stack operations
Zero functional changes - identical behavior with better efficiency
This change better aligns with the stated goal of maximum gas efficiency while maintaining code clarity and correctness.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.
The contest is complete and the rewards are being distributed.