Skip to content

Commit

Permalink
add abiEncodeTightlyPacked, partially refactor tightcoder
Browse files Browse the repository at this point in the history
dk1a committed Jul 21, 2023
1 parent 1f9093e commit 80bad46
Showing 13 changed files with 4,408 additions and 497 deletions.
12 changes: 12 additions & 0 deletions packages/common/src/codegen/render-solidity/common.ts
Original file line number Diff line number Diff line change
@@ -180,6 +180,18 @@ export function renderValueTypeToBytes32(name: string, { typeUnwrap, internalTyp
}
}

export function isLeftAligned(field: Pick<RenderType, "internalTypeId">): boolean {
return field.internalTypeId.match(/^bytes\d{1,2}$/) !== null;
}

export function shiftLeftBits(field: Pick<RenderType, "internalTypeId" | "staticByteLength">): number {
if (isLeftAligned(field)) {
return 0;
} else {
return 256 - field.staticByteLength * 8;
}
}

function internalRenderList<T>(
lineTerminator: string,
list: T[],
16 changes: 8 additions & 8 deletions packages/store/gas-report.json
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@
"file": "test/Gas.t.sol",
"test": "testCompareAbiEncodeVsCustom",
"name": "custom encode",
"gasUsed": 1812
"gasUsed": 1379
},
{
"file": "test/Gas.t.sol",
@@ -117,7 +117,7 @@
"file": "test/Mixed.t.sol",
"test": "testSetAndGet",
"name": "set record in Mixed",
"gasUsed": 111108
"gasUsed": 110678
},
{
"file": "test/Mixed.t.sol",
@@ -645,7 +645,7 @@
"file": "test/tables/Callbacks.t.sol",
"test": "testSetAndGet",
"name": "set field in Callbacks",
"gasUsed": 62243
"gasUsed": 61982
},
{
"file": "test/tables/Callbacks.t.sol",
@@ -663,7 +663,7 @@
"file": "test/tables/Hooks.t.sol",
"test": "testSetAndGet",
"name": "set field in Hooks",
"gasUsed": 64400
"gasUsed": 63973
},
{
"file": "test/tables/Hooks.t.sol",
@@ -693,19 +693,19 @@
"file": "test/tightcoder/EncodeArray.t.sol",
"test": "testEncodeUint16Array",
"name": "encode packed uint16[]",
"gasUsed": 1143
"gasUsed": 710
},
{
"file": "test/tightcoder/EncodeArray.t.sol",
"test": "testEncodeUint32Array",
"name": "encode packed uint32[]",
"gasUsed": 1049
"gasUsed": 619
},
{
"file": "test/tightcoder/EncodeArray.t.sol",
"test": "testEncodeUint8Array",
"name": "encode packed uint8[]",
"gasUsed": 1038
"gasUsed": 608
},
{
"file": "test/tightcoder/TightCoder.t.sol",
@@ -717,7 +717,7 @@
"file": "test/tightcoder/TightCoder.t.sol",
"test": "testToAndFromBytes24Array",
"name": "encode packed bytes24[]",
"gasUsed": 880
"gasUsed": 608
},
{
"file": "test/tightcoder/TightCoder.t.sol",
19 changes: 19 additions & 0 deletions packages/store/src/Memory.sol
Original file line number Diff line number Diff line change
@@ -44,4 +44,23 @@ library Memory {
)
}
}

/**
* mstore n bytes (left-aligned) of given data
*/
function mstoreN(bytes32 data, uint256 toPointer, uint256 n) internal pure {
uint256 mask = leftMask(n);
/// @solidity memory-safe-assembly
assembly {
mstore(
toPointer,
or(
// store the left part
and(data, mask),
// preserve the right part
and(mload(toPointer), not(mask))
)
)
}
}
}
Loading

0 comments on commit 80bad46

Please sign in to comment.