In the world of Ethereum development, gas is money. While Solidity provides a high-level, developer-friendly syntax, it often abstracts away the inner workings of the Ethereum Virtual Machine (EVM). For developers looking to optimize their smart contracts to the absolute limit, there is Yul.
Yul is an intermediate language that can be used within Solidity as “Inline Assembly.” It allows you to bypass some of the high-level safety features of Solidity to interact directly with the EVM’s stack, memory, and storage.
Why Use Yul?
Using Yul isn’t about making code look “cool”—it’s about surgical precision. Here are the three main reasons developers drop down to assembly:
- Gas Optimization: Solidity’s compiler (solc) is getting better, but it still generates overhead. Yul allows you to write the exact opcodes you need, often saving thousands of gas in complex loops or data manipulations.
- Access to Low-Level Opcodes: Some EVM instructions, like
mstore8,staticcall, ordelegatecallnuances, are more flexibly handled via assembly. - Memory Management: Solidity’s memory management can be restrictive. Yul gives you full control over the memory pointer, allowing for manual allocation and layout.
The Anatomy of an Assembly Block
In Solidity, you enter the world of Yul using the assembly { ... } block. Inside this block, you are no longer writing standard Solidity; you are writing a functional-style language that maps closely to EVM opcodes.
function addNumbers(uint256 a, uint256 b) public pure returns (uint256 result) {
assembly {
// We use the 'add' opcode
result := add(a, b)
}
}
Interacting with Storage and Memory
This is where Yul truly shines. In Solidity, accessing a mapping or a large array involves several hidden calculations. In Yul, you see everything.
1. Loading and Storing
The primary opcodes for data movement are:
sload(p): Load a 32-byte word from storage slotp.sstore(p, v): Store a 32-byte wordvinto storage slotp.mload(p): Read 32 bytes from memory at offsetp.mstore(p, v): Write 32 bytesvinto memory at offsetp.
Example: Bypassing Solidity’s Overflow Checks
Since Solidity 0.8.0, arithmetic is checked by default. If you know a value won’t overflow and want to save the gas cost of the check, Yul is your friend:
function fastIncrement(uint256 x) public pure returns (uint256) {
assembly {
x := add(x, 1)
}
return x;
}
A Practical Use Case: Efficient Hashing
When computing hashes of multiple variables, Solidity often copies data to memory first. With Yul, you can manipulate the memory pointer (0x40) directly to perform operations more efficiently.
function efficientHash(uint256 a, uint256 b) public pure returns (bytes32 result) {
assembly {
// Store 'a' at memory offset 0
mstore(0x00, a)
// Store 'b' at memory offset 32 (0x20)
mstore(0x20, b)
// Hash 64 bytes starting from offset 0
result := keccak256(0x00, 0x40)
}
}
When Should You Use Yul?
The rule of thumb for Yul is: Only use it when necessary.
- Profile your gas usage. If a function is called thousands of times and is gas-heavy, investigate Yul.
- Use it for library functions (like custom string manipulation or highly optimized math).
- Avoid it for business logic where readability and security audits are the priority.
Conclusion
Yul is the “God Mode” of Ethereum development. It allows you to move beyond the constraints of the high-level syntax and speak the language of the EVM directly. While the learning curve is steep and the risks are real, mastering Yul is the hallmark of a senior smart contract engineer. By squeezing every drop of efficiency out of your code, you ensure your protocols are leaner, faster, and more competitive.
--optimize flag does a better job than manual assembly!