StorePacking
, the
compiler automatically uses StoreUsingPacking
to handle storage operations. As such, a type
cannot implement both Store
and StorePacking
.
Signature
Storage Optimization
Each storage slot in Starknet is afelt252
, and storage operations are expensive. By packing
multiple values into fewer slots, you can significantly reduce gas costs. For example:
- Multiple small integers can be packed into a single
felt252
- Structs with several fields can be compressed into a single storage slot
Implementation Requirements
To implementStorePacking
, ensure that the PackedT
type implements Store
. The packed
representation must preserve all necessary information to allow unpacking back to the original
type. Additionally, the pack
and unpack
operations must be reversible, meaning that packing
followed by unpacking should return the original value.
Example
Packing multiple integer fields into a single storage slot:StorePacking
for Sizes
, the Sizes
will be stored in its packed form,
using a single storage slot instead of 3. When retrieved, it will automatically be unpacked back
into the original type.