Optimizing the arrangement of data in storage layout.
Project description
Storage Layout Optimizer
[!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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sl_optimizer-0.0.4.tar.gz
.
File metadata
- Download URL: sl_optimizer-0.0.4.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c23933e5feeaf3b0bdfbc94d29d71cd64116964ae8961b62bbc54e6c0df59b0 |
|
MD5 | 5a78e678fd442646437b5b1f013f286d |
|
BLAKE2b-256 | 73833ec4a8b3f004b5a56406fdc9a834d1dcfef514fa2a62a9b3aad0ee1da994 |
File details
Details for the file sl_optimizer-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: sl_optimizer-0.0.4-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3c342d9248fd6d04ed0a9600c44ff20aed48af57ab8797d0157142639048aeb |
|
MD5 | e63c8fa2dbe7ff9411356bb1d50753ac |
|
BLAKE2b-256 | 25ed1993a53d2fad8ce7b9783bdd6f9e69b4ca42c3c4a187592f0e88c91efe35 |