Giter VIP home page Giter VIP logo

storage-layout-optimizer's Introduction

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}

Loading

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.