Skip to content

Pack address types to utilize 100% of storage space in Solidity

License

Notifications You must be signed in to change notification settings

soltheon/packed_arrays

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solidity Packed Arrays

Overview

Solidity Packed Arrays is a library designed to optimize storage and gas efficiency in Solidity by bit-packing address values in an array. Traditional storage of addresses in Ethereum wastes 37% of storage space, leading to unnecessary costs. This library aims to utilize 100% of the storage space by splitting addresses across storage slots.

Features

  • Gas Cost Reduction: Reduces gas costs significantly, especially beneficial for operations involving multiple addresses.
  • Efficient Storage: Maximizes storage efficiency by fitting addresses snugly within 32-byte storage slots.
  • Caching Mechanism: Minimizes SLOAD/SSTORE operations when fetching multiple addresses

Usage

Import the library

    using PackedArray for PackedArray.Addresses;

Declare the type in storage

    PackedArray.Addresses public array;

Push: Push an address to the array

    array.push(address(0xCAFE));

Set: Change the address at a particular index

    array.set(0, address(0xBEEF));

Get: Fetch an address in the array

    address beef = array.get(0);

Pop: Remove an item

    array.pop();

Append: Save multiple addresses from memory to storage

    address[] memory addresses;

    array.append(addresses);

Slice: Fetch multiple addresses into memory

    address[] memory addrs = array.slice(0, addresses.length);

Gas Comparison

The following operations are tested against using a regular address array in solidity:

Operation Normal Array Packed Array Percentage
Batch append 50 1,151,753 739,635 -35.78%
Batch slice 50 25,853 13,312 -48.51%
Set, Get, Edit 50 1,179,545 823,333 -30.19%
Set, Get, Edit 5 138,275 120,563 -12.81%
Set, Get, Edit 1 45,626 46,232 +1.33%

Test

$ forge test

Security

The code is tested but not audited. Use it at your own risk.

Contributing

Contributions to the library are welcome. Please submit pull requests for any enhancements.

Planned Future Features

  • Support for Variable Data Sizes: Future updates will include the ability to handle data sizes that do not fit snugly into 32 bytes.

License

This project is licensed under the GNU General Public License v3.0 or later (GPL-3.0-or-later).

For more details visit GNU General Public License v3.0 or later.

About

Pack address types to utilize 100% of storage space in Solidity

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published