Feature Request: Consider adding packed keyword to pack struct fields #13175
Labels
closed due inactivity
The issue/PR was automatically closed due to inactivity.
feature
stale
The issue/PR was marked as stale because it has been open for too long.
Abstract
Consider following struct:
When abiencoded value Foo(true, true, false, true) translates into
0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
, while it could be just1101
.Motivation
Since gas usage is a big topic almost all big ethereum projects use tight structs packing to save caller funds. You can see it in the wild everywhere, e.g. 1Inch limit orders encodes order Id and timestamp into
Order.info
or Uniswap V3 swaps encodes zeroForOne and oneForZero fees intofeeProtocol
field. There are other examples in the wild. Since it's a repeatable pattern which is kinda error-prone and dull I think it just might be worthwhile to move this to compiler and code generation.Specification
Introduce new keyword
packed
that changes ABI representation and field access. It might look like:This struct should be only 4 bytes long. When abiencoded it might look like
00000000000000000000000000000000000000000000000000000000000001101
or11010000000000000000000000000000000000000000000000000000000000000
depending on if we wantbytes
oruint
zeroing pattern. Compiler is responsible for generating appropriate bit shifts and masks when reading and writing fields:Note that unlike current behavior this should affect calldata representation as well as inner storage.
Backwards Compatibility
Since this proposal introduces a new keyword it doesn't change meaning for any existing code.
Related: #11691
Additional considerations
It may be also worthwhile to consider packed arrays like:
function bar(uint8[] packed calldata foo) external { }
so it doesn't put padding zeroes between elements, but unaligned access from those might be tricky and come with surprise costs.
The text was updated successfully, but these errors were encountered: