Resources
Oct 4, 2022

Optimizing Gas Costs on Aptos

Econia Labs’ Latest Open-Source Contributions to Aptos Core

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:

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:

  1. Instructions.
  2. Storage.
  3. Payload.

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:

  1. Per-item reads.
  2. Per-item writes.
  3. 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:

  1. Minimize per-item creations.
  2. Track unused items and overwrite them, rather than creating new items, when possible.
  3. Contain per-item writes to as few items as possible.
  4. Read, rather than write, whenever possible.

For more detail and additional insights, check out the new “How Base Gas Works” article on the official Aptos documentation website, especially the “Optimization Principles” section!

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!

Author
Econia Labs

Econia is a hyper-parallelized order book protocol that runs on the Aptos blockchain. Engineered for web-scale performance and built to provide equal global access to markets, Econia leverages Aptos’ optimistic concurrency to scale up trading across trading pairs.