B003: Maximal Gas Optimizations

Transaction Fees

Each transaction on Ethereum and any EVM-based blockchain consumes gas for its execution. While simple transactions between two accounts have a set gas cost, complex smart-contract-based transactions can consume an arbitrary amount of gas which is dictated by the EVM statements the transaction executes.

Compilation Optimizer

This is a widely misunderstood tool that most people fail to utilize properly. Although two optimizers are currently available in the latest version of Solidity, we will focus on the “old” optimizer that operates on the opcode level.

Costly Notions

Our primary focus will be on how to rigorously optimize the gas cost consumed by smart contract transactions. Before moving forward to short, tangible examples, we will showcase how a seemingly simple code example can be optimized in multiple ways.

Looks simple, right?
  • Contains three mapping lookups
  • Reads a full storage slot three times
  • Upcasts a bytes8 three times

Mapping Lookups

Let’s deal with the first issue first. Within Solidity, a mapping is accessed by computing the keccak256 hash of its key. This means that, regardless of the key type, a keccak256 operation will be performed each time a particular entry is requested. To avoid this, a “pointer” should be stored in the code to ensure that the lookup occurs only once.

Redundant Lookups Eliminated

Storage Access

By far, storage reads and writes are the most costly notion within Solidity. A programmer’s primary goal to reduce gas costs is to minimize the total storage reads and writes performed by a particular function block. This can be done by avoiding data redundancy, such as the struct containing the key used for the mapping, or by applying data optimization techniques.

Variable Tight Packing
Struct Stored in Memory

Type Upcasting

The final point here to optimize is data upcasting. The EVM is built to handle 32-byte data, meaning that any type less than that has been artificially created using upcasting and downcasting techniques that clean up the extraneous bits.

Optimized Data Types

Comparison

To illustrate how simple statements can significantly affect the gas cost of a function, we will compare the finalized code above with the initial bullet list:

  • Contains three mapping lookups — Contains a single mapping lookup
  • Reads a full storage slot three times — Reads a full storage slot once
  • Upcasts a bytes8 three times — Contains no upcasting

Conclusion

A lot more optimizations are available within Solidity that can be applied yet remain relatively unknown or at least not widely applied, most likely due to the low gas costs that Ethereum possessed when the first Solidity developers hopped on the ecosystem.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store