Optimizing for gas costs is an essential part of deploying Move code to the Aptos blockchain, because transaction costs for unoptimized programs are ultimately passed on to end users. Move developers want to write programs that are inexpensive for anyone who uses them, and users want developers to write code that is as optimized as possible.
Now, after several months of fast-paced development, Move developers have a stable foundation to start optimizing on top of, because just last week, Aptos Labs finalized a precision-engineered gas schedule built for web-scale performance.
Days later, Econia Labs dove deep into the open-source code that runs the new gas schedule, searching for ways to build Econia into the most gas-efficient settlement engine it can be, and hoping to support other developers who want to keep their gas costs low too. After reverse-engineering a formal specification on the gas schedule directly from source code, Econia Labs authored two contributions to the Aptos Core open-source repository, laying out the gas schedule and how to optimize around it:
- Commit 202c6f8: DocGen additions to storage_gas.move, enabling a comprehensive and internally-linked module documentation file, auto-built at storage_gas.md.
- Commit e852029: A new topic page on the official Aptos documentation website, “How Base Gas Works,” including an “Optimization Principles” section.
Both contributions have been accepted, and now everyone can benefit from enhanced gas documentation!
Base Gas Principles
Aptos transactions charge a default base gas fee based on three conditions:
Storage gas on a per-item basis is by far the most expensive, with an “item” defined as either a resource having the key attribute, or a table entry. Here, there are three possible kinds of operations:
- Per-item reads.
- Per-item writes.
- Per-item creations.
Reads from global storage do not alter data, writes into global storage alter existing data, and creations allocate completely new memory, then write to it. With writes costing five times as much as reads and creations costing five times as much as writes, the most effective strategy for minimizing gas costs thus involves four simple principles:
- Minimize per-item creations.
- Track unused items and overwrite them, rather than creating new items, when possible.
- Contain per-item writes to as few items as possible.
- Read, rather than write, whenever possible.
Econia v4 and Beyond
With mainnet just around the corner, Econia Labs will continue iterating with the Aptos developer community, incorporating the latest optimization principles into its Econia v4 mainnet release, and supporting integrators who want to provide low transaction fees for their end users. Working together to build out the next generation of DeFi on Aptos, there is no limit to the kinds of gas-optimized programs that can be built on top of Econia!