sl-optimizer


Namesl-optimizer JSON
Version 0.0.4 PyPI version JSON
download
home_page
SummaryOptimizing the arrangement of data in storage layout.
upload_time2024-02-23 02:43:05
maintainer
docs_urlNone
author
requires_python>=3.10
licenseThe MIT License (MIT) Copyright (c) 2024 to present Vladyslav Novotnyi and individual contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords evm solidity storage-layout
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Storage Layout Optimizer

[![Tests](https://github.com/fabelx/storage-layout-optimizer/actions/workflows/tests.yml/badge.svg)](https://github.com/fabelx/storage-layout-optimizer/actions/workflows/tests.yml)
___
> [!IMPORTANT]
> ## Currently, in development
___

## About
`sl_optimizer` is a Python library designed to optimize the storage layout for [Solidity](https://soliditylang.org/) 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.

<details>
<summary>Problem Statement</summary>

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](https://en.wikipedia.org/wiki/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.

```mermaid
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.

</details>

<details>
<summary>Additional</summary>

- **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](tests/fixtures/sample_contract_1_storage.json).
- **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](https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html#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](https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html).

</details>

___

## Installation
```bash
pip install sl_optimizer
```
From source:
```bash
make install
```
or
```bash
pip install .
```
___

## Usage
See [documentation](src/README.md) for more details.

___

### To-do
- [x] rewrite tests (improve the use of utilities in code)
- [x] add new samples ([fixtures](tests/fixtures)) for tests
- [ ] add diagnostic or metric information related to optimization
- [x] add CLI
- [x] add benchmarks (to cover optimize functionality, for local usage, not included to package)
- [x] 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](LICENSE.txt) file for license information.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "sl-optimizer",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "Vladyslav Novotnyi <psejjkuczo@proxiedmail.com>",
    "keywords": "evm,solidity,storage-layout",
    "author": "",
    "author_email": "Vladyslav Novotnyi <psejjkuczo@proxiedmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/73/83/3ec4a8b3f004b5a56406fdc9a834d1dcfef514fa2a62a9b3aad0ee1da994/sl_optimizer-0.0.4.tar.gz",
    "platform": null,
    "description": "# Storage Layout Optimizer\n\n[![Tests](https://github.com/fabelx/storage-layout-optimizer/actions/workflows/tests.yml/badge.svg)](https://github.com/fabelx/storage-layout-optimizer/actions/workflows/tests.yml)\n___\n> [!IMPORTANT]\n> ## Currently, in development\n___\n\n## About\n`sl_optimizer` is a Python library designed to optimize the storage layout for [Solidity](https://soliditylang.org/) smart contracts.\nEfficient storage layout can reduce the number of storage slots needed, leading to lower gas costs for both storage\nand computational operations. It's important to align variables to these slots\nefficiently to avoid wasted space and save some money.\n\n<details>\n<summary>Problem Statement</summary>\n\nIn Solidity smart contract development, the efficient allocation of storage is a critical concern for optimizing gas costs\nand overall performance. The current challenge lies in the need to carefully manage the storage layout to reduce the number\nof required storage slots. The inefficient allocation of variables to these slots can result in increased gas costs for both\nstorage and computational operations.\n\nWasted space due to suboptimal storage layout not only incurs unnecessary expenses but also diminishes the overall efficiency\nof smart contracts. To address this issue, developers must align variables to storage slots in an optimized manner.\nHowever, manually achieving this level of efficiency can be time-consuming and error-prone.\n\nTo streamline this process and enhance the cost-effectiveness of Solidity smart contracts, the `sl_optimizer` Python library has been designed.\nThis library aims to automate and optimize the storage layout.\n\n### Mathematical Complexity\nThe mathematical complexity of storage layout optimization involves determining the most efficient way to pack variables\ninto storage slots *(aka [Bin packing problem](https://en.wikipedia.org/wiki/Bin_packing_problem))*. This problem can be\napproached with various algorithms and optimization techniques.\n\n#### First Fit Decreasing Method\n**The First Fit Decreasing (FFD)** method is a heuristic algorithm commonly used in bin packing problems, and it was\nadapted for storage layout optimization in Solidity. The goal is to efficiently pack variables into 32-byte storage slots,\nminimizing wasted space and optimizing gas costs.\n\n```mermaid\ngraph TD\n  A[Sort variables in decreasing order of size] -->|Sorted List| B(Empty set of storage slots)\n  B -->|Available Storage Slots| C{For each variable in the Sorted List}\n  C -->|Iterate through slots| D(Try to find a suitable slot)\n  D -->|Slot found| E{Assign variable to slot}\n  D -->|No suitable slot| F[Create a new storage slot]\n  E -->|Assign variable| C\n  F -->|Assign variable| C\n  C -->|All variables assigned| G{End}\n\n```\n\n#### Challenges:\n - Dependencies between variables might constrain the packing possibilities.\n - Arrays and mappings can complicate storage layout due to their dynamic nature.\n - Optimizing for storage efficiency must also consider the gas costs associated with reading and writing to storage.\n - Functions that use a delegate call to interact with the implementation contract.\n\n</details>\n\n<details>\n<summary>Additional</summary>\n\n- **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](tests/fixtures/sample_contract_1_storage.json).\n- **Storage** - refers to the `storage` field in the storage layout json file and contains information about the layout of variables (storage).\n- **Type(s)** - refers to the `types` field in the storage layout json file and contains information about the types used in the smart contact.\n- **[Gas](https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html#gas)** - is the unit used to measure computational effort in the EVM.\n- Solidity stores data in 32-byte chunks, also known as storage **slots**.\n\nMore information [here](https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html).\n\n</details>\n\n___\n\n## Installation\n```bash\npip install sl_optimizer\n```\nFrom source:\n```bash\nmake install\n```\nor\n```bash\npip install .\n```\n___\n\n## Usage\nSee [documentation](src/README.md) for more details.\n\n___\n\n### To-do\n- [x] rewrite tests (improve the use of utilities in code)\n- [x] add new samples ([fixtures](tests/fixtures)) for tests\n- [ ] add diagnostic or metric information related to optimization\n- [x] add CLI\n- [x] add benchmarks (to cover optimize functionality, for local usage, not included to package)\n- [x] generate 3 fixtures (large nested structures & huge number of variables & cross nested structures)\n- [ ] change algorithm First Fit Decreasing -> consideration in progress ...\n- [ ] restructuring storage data for optimization\n___\n\n## License\n`sl_optimizer` is released under the MIT License.\nSee the [LICENSE](LICENSE.txt) file for license information.\n",
    "bugtrack_url": null,
    "license": "The MIT License (MIT)  Copyright (c) 2024 to present Vladyslav Novotnyi and individual contributors.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Optimizing the arrangement of data in storage layout.",
    "version": "0.0.4",
    "project_urls": {
        "Bug Reports": "https://github.com/fabelx/storage-layout-optimizer/issues",
        "Homepage": "https://github.com/fabelx/storage-layout-optimizer",
        "Say Thanks!": "https://saythanks.io/to/daprostovseeto",
        "Source": "https://github.com/fabelx/storage-layout-optimizer"
    },
    "split_keywords": [
        "evm",
        "solidity",
        "storage-layout"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "25ed1993a53d2fad8ce7b9783bdd6f9e69b4ca42c3c4a187592f0e88c91efe35",
                "md5": "e63c8fa2dbe7ff9411356bb1d50753ac",
                "sha256": "d3c342d9248fd6d04ed0a9600c44ff20aed48af57ab8797d0157142639048aeb"
            },
            "downloads": -1,
            "filename": "sl_optimizer-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e63c8fa2dbe7ff9411356bb1d50753ac",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 12396,
            "upload_time": "2024-02-23T02:43:03",
            "upload_time_iso_8601": "2024-02-23T02:43:03.811210Z",
            "url": "https://files.pythonhosted.org/packages/25/ed/1993a53d2fad8ce7b9783bdd6f9e69b4ca42c3c4a187592f0e88c91efe35/sl_optimizer-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "73833ec4a8b3f004b5a56406fdc9a834d1dcfef514fa2a62a9b3aad0ee1da994",
                "md5": "5a78e678fd442646437b5b1f013f286d",
                "sha256": "4c23933e5feeaf3b0bdfbc94d29d71cd64116964ae8961b62bbc54e6c0df59b0"
            },
            "downloads": -1,
            "filename": "sl_optimizer-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "5a78e678fd442646437b5b1f013f286d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 13602,
            "upload_time": "2024-02-23T02:43:05",
            "upload_time_iso_8601": "2024-02-23T02:43:05.613114Z",
            "url": "https://files.pythonhosted.org/packages/73/83/3ec4a8b3f004b5a56406fdc9a834d1dcfef514fa2a62a9b3aad0ee1da994/sl_optimizer-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-23 02:43:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fabelx",
    "github_project": "storage-layout-optimizer",
    "github_not_found": true,
    "lcname": "sl-optimizer"
}
        
Elapsed time: 0.18601s