This guide is based on Sei’s recommendations to reduce gas usage and improve parallel execution. For the engine design, see the parallelization engine page.
Principles for parallel-friendly contracts
- Minimize shared writes: The scheduler can parallelize transactions that do not write to the same storage keys. Avoid hot globals (for example, a single counter that every call updates).
- Partition state: Shard storage by user, asset, or ID so that independent transactions touch disjoint keys.
- Prefer pull over push: Let users claim funds instead of paying many recipients in a loop.
- Avoid unbounded loops: This applies especially to loops that write to storage or iterate over dynamic arrays or mappings.
- Batch internal work and isolate external effects: Do heavy computation in memory and commit a minimal set of storage writes.
- Use precompiles when available: Precompiles are highly optimized and cost less than the same logic written in Solidity.
Storage design patterns
Partition state by key
Isolate per-user and per-asset data instead of centralizing writes.- Writing a global
totalVolume += amount;in every transaction - Maintaining a single on-chain queue or registry that most calls update
Prefer pull payments
Avoid writing to many recipients in a single transaction. Emit events, and let recipients callwithdraw() when they need to.
Avoid large storage writes in loops
If you must process many items, keep the work in memory and commit a compact result. Alternatively, split the work across multiple transactions keyed by different IDs.Gas-efficient Solidity practices
- Use
externalfor externally called functions. Mark pure and read-only functions withpureandview. - Pack variables to share storage slots (for example, multiple
uint8values in one slot). - Prefer
bytes32overstringwhen applicable. - Cache array lengths in loops and short-circuit cheap conditions first.
- Use
uncheckedfor arithmetic when overflow is impossible. - Prefer memory over storage for temporary data, and commit only final values.
Leverage Sei precompiles
Sei has precompiled contracts for common functionality. They are cheaper and simpler than custom Solidity equivalents: See precompile examples for quick starts.Testing and analysis
- Use Foundry or Hardhat gas reporters to track changes per function.
- Benchmark under concurrent load to detect hot keys (addresses or IDs) that serialize execution.
- Inspect execution with JavaScript tracers and runtime logs.