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.
- 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
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);
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% |
$ forge test
The code is tested but not audited. Use it at your own risk.
Contributions to the library are welcome. Please submit pull requests for any enhancements.
- Support for Variable Data Sizes: Future updates will include the ability to handle data sizes that do not fit snugly into 32 bytes.
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.