Skip to main content

Optimizing the arrangement of data in storage layout.

Project description

Storage Layout Optimizer

Tests


[!IMPORTANT]

Currently, in development


About

sl_optimizer is a Python library designed to optimize the storage layout for Solidity smart contracts. Efficient storage layout can reduce the number of storage slots needed, leading to lower gas costs for both storage and computational operations. It's important to align variables to these slots efficiently to avoid wasted space and save some money.

Problem Statement

In Solidity smart contract development, the efficient allocation of storage is a critical concern for optimizing gas costs and overall performance. The current challenge lies in the need to carefully manage the storage layout to reduce the number of required storage slots. The inefficient allocation of variables to these slots can result in increased gas costs for both storage and computational operations.

Wasted space due to suboptimal storage layout not only incurs unnecessary expenses but also diminishes the overall efficiency of smart contracts. To address this issue, developers must align variables to storage slots in an optimized manner. However, manually achieving this level of efficiency can be time-consuming and error-prone.

To streamline this process and enhance the cost-effectiveness of Solidity smart contracts, the sl_optimizer Python library has been designed. This library aims to automate and optimize the storage layout.

Mathematical Complexity

The mathematical complexity of storage layout optimization involves determining the most efficient way to pack variables into storage slots (aka Bin packing problem). This problem can be approached with various algorithms and optimization techniques.

First Fit Decreasing Method

The First Fit Decreasing (FFD) method is a heuristic algorithm commonly used in bin packing problems, and it was adapted for storage layout optimization in Solidity. The goal is to efficiently pack variables into 32-byte storage slots, minimizing wasted space and optimizing gas costs.

graph TD
  A[Sort variables in decreasing order of size] -->|Sorted List| B(Empty set of storage slots)
  B -->|Available Storage Slots| C{For each variable in the Sorted List}
  C -->|Iterate through slots| D(Try to find a suitable slot)
  D -->|Slot found| E{Assign variable to slot}
  D -->|No suitable slot| F[Create a new storage slot]
  E -->|Assign variable| C
  F -->|Assign variable| C
  C -->|All variables assigned| G{End}

Challenges:

  • Dependencies between variables might constrain the packing possibilities.
  • Arrays and mappings can complicate storage layout due to their dynamic nature.
  • Optimizing for storage efficiency must also consider the gas costs associated with reading and writing to storage.
  • Functions that use a delegate call to interact with the implementation contract.
Additional
  • Layout (Storage Layout) - in code you can often find references to these names; they mean a data storage scheme that is presented in json format and can be obtained using this command solc --storage-layout -o output Contract.sol, an example of a smart contract storage json file here.
  • Storage - refers to the storage field in the storage layout json file and contains information about the layout of variables (storage).
  • Type(s) - refers to the types field in the storage layout json file and contains information about the types used in the smart contact.
  • Gas - is the unit used to measure computational effort in the EVM.
  • Solidity stores data in 32-byte chunks, also known as storage slots.

More information here.


Installation

pip install sl_optimizer

From source:

make install

or

pip install .

Usage

See documentation for more details.


To-do

  • rewrite tests (improve the use of utilities in code)
  • add new samples (fixtures) for tests
  • add diagnostic or metric information related to optimization
  • add CLI
  • add benchmarks (to cover optimize functionality, for local usage, not included to package)
  • generate 3 fixtures (large nested structures & huge number of variables & cross nested structures)
  • change algorithm First Fit Decreasing -> consideration in progress ...
  • restructuring storage data for optimization

License

sl_optimizer is released under the MIT License. See the LICENSE file for license information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sl_optimizer-0.0.4.tar.gz (13.6 kB view hashes)

Uploaded Source

Built Distribution

sl_optimizer-0.0.4-py3-none-any.whl (12.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page