From 3fd5be68bbd7a0276ffc69a87279494a0aa489be Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 08:21:13 +0300 Subject: [PATCH 01/12] add isLeftAligned, shiftLeftBits common methods --- .../common/src/codegen/render-solidity/common.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/common/src/codegen/render-solidity/common.ts b/packages/common/src/codegen/render-solidity/common.ts index d8ea6dc0ef..54399134f2 100644 --- a/packages/common/src/codegen/render-solidity/common.ts +++ b/packages/common/src/codegen/render-solidity/common.ts @@ -180,6 +180,18 @@ export function renderValueTypeToBytes32(name: string, { typeUnwrap, internalTyp } } +export function isLeftAligned(field: Pick): boolean { + return field.internalTypeId.match(/^bytes\d{1,2}$/) !== null; +} + +export function shiftLeftBits(field: Pick): number { + if (isLeftAligned(field)) { + return 0; + } else { + return 256 - field.staticByteLength * 8; + } +} + function internalRenderList( lineTerminator: string, list: T[], From 5422dc6e18a8308ad14f2793a49f839f11e130d1 Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 08:27:43 +0300 Subject: [PATCH 02/12] refactor TightCoder codegen --- packages/store/package.json | 4 +- packages/store/src/tightcoder/DecodeSlice.sol | 624 ++++++++--------- packages/store/src/tightcoder/EncodeArray.sol | 635 ++++++++---------- packages/store/src/tightcoder/TightCoder.sol | 50 +- .../store/test/tightcoder/EncodeArray.t.sol | 18 - .../store/test/tightcoder/TightCoder.t.sol | 54 +- .../test/tightcoder/TightCoderAuto.t.sol | 55 +- packages/store/ts/codegen/tightcoder/index.ts | 4 + .../codegen/tightcoder/renderDecodeSlice.ts | 22 + .../codegen/tightcoder/renderEncodeArray.ts | 21 + .../ts/codegen/tightcoder/renderFunctions.ts | 36 + .../tightcoder/renderTightCoderAutoTest.ts | 47 ++ .../store/ts/scripts/generate-tightcoder.ts | 25 +- 13 files changed, 780 insertions(+), 815 deletions(-) create mode 100644 packages/store/ts/codegen/tightcoder/index.ts create mode 100644 packages/store/ts/codegen/tightcoder/renderDecodeSlice.ts create mode 100644 packages/store/ts/codegen/tightcoder/renderEncodeArray.ts create mode 100644 packages/store/ts/codegen/tightcoder/renderFunctions.ts create mode 100644 packages/store/ts/codegen/tightcoder/renderTightCoderAutoTest.ts diff --git a/packages/store/package.json b/packages/store/package.json index 8c160dc9a4..d7014f0f03 100644 --- a/packages/store/package.json +++ b/packages/store/package.json @@ -34,7 +34,7 @@ } }, "scripts": { - "build": "pnpm run build:mud && pnpm run build:abi && pnpm run build:typechain && pnpm run build:js", + "build": "pnpm run generate-tightcoder && pnpm run build:mud && pnpm run build:abi && pnpm run build:typechain && pnpm run build:js", "build:abi": "forge build --extra-output-files abi --out abi --skip test script MudTest.sol", "build:js": "tsup", "build:mud": "tsx ./ts/scripts/tablegen.ts", @@ -46,7 +46,7 @@ "clean:typechain": "rimraf types", "dev": "tsup --watch", "gas-report": "mud-gas-report --save gas-report.json", - "generate-tightcoder": "tsx ./scripts/generate-tightcoder.ts && prettier --write '**/tightcoder/*.sol'", + "generate-tightcoder": "tsx ./ts/scripts/generate-tightcoder.ts", "lint": "solhint --config ./.solhint.json 'src/**/*.sol'", "test": "vitest typecheck --run && vitest --run --passWithNoTests && forge test" }, diff --git a/packages/store/src/tightcoder/DecodeSlice.sol b/packages/store/src/tightcoder/DecodeSlice.sol index ecee49c4fb..19d0e24010 100644 --- a/packages/store/src/tightcoder/DecodeSlice.sol +++ b/packages/store/src/tightcoder/DecodeSlice.sol @@ -1,728 +1,694 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.0; -/* Autogenerated via `pnpm codegen`. Do not edit the `.sol` file manually. */ - -import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; +/* Autogenerated file. Do not edit manually. */ import { TightCoder } from "./TightCoder.sol"; import { Slice } from "../Slice.sol"; library DecodeSlice { - /************************************************************************ - * - * uint8 - uint256 - * - ************************************************************************/ - - function decodeArray_uint8(Slice self) internal pure returns (uint8[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 1, false); + function decodeArray_uint8(Slice _input) internal pure returns (uint8[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint16(Slice self) internal pure returns (uint16[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 2, false); + function decodeArray_uint16(Slice _input) internal pure returns (uint16[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 2, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint24(Slice self) internal pure returns (uint24[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 3, false); + function decodeArray_uint24(Slice _input) internal pure returns (uint24[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 3, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint32(Slice self) internal pure returns (uint32[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 4, false); + function decodeArray_uint32(Slice _input) internal pure returns (uint32[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 4, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint40(Slice self) internal pure returns (uint40[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 5, false); + function decodeArray_uint40(Slice _input) internal pure returns (uint40[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 5, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint48(Slice self) internal pure returns (uint48[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 6, false); + function decodeArray_uint48(Slice _input) internal pure returns (uint48[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 6, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint56(Slice self) internal pure returns (uint56[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 7, false); + function decodeArray_uint56(Slice _input) internal pure returns (uint56[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 7, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint64(Slice self) internal pure returns (uint64[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 8, false); + function decodeArray_uint64(Slice _input) internal pure returns (uint64[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 8, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint72(Slice self) internal pure returns (uint72[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 9, false); + function decodeArray_uint72(Slice _input) internal pure returns (uint72[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 9, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint80(Slice self) internal pure returns (uint80[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 10, false); + function decodeArray_uint80(Slice _input) internal pure returns (uint80[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 10, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint88(Slice self) internal pure returns (uint88[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 11, false); + function decodeArray_uint88(Slice _input) internal pure returns (uint88[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 11, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint96(Slice self) internal pure returns (uint96[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 12, false); + function decodeArray_uint96(Slice _input) internal pure returns (uint96[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 12, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint104(Slice self) internal pure returns (uint104[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 13, false); + function decodeArray_uint104(Slice _input) internal pure returns (uint104[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 13, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint112(Slice self) internal pure returns (uint112[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 14, false); + function decodeArray_uint112(Slice _input) internal pure returns (uint112[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 14, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint120(Slice self) internal pure returns (uint120[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 15, false); + function decodeArray_uint120(Slice _input) internal pure returns (uint120[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 15, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint128(Slice self) internal pure returns (uint128[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 16, false); + function decodeArray_uint128(Slice _input) internal pure returns (uint128[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 16, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint136(Slice self) internal pure returns (uint136[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 17, false); + function decodeArray_uint136(Slice _input) internal pure returns (uint136[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 17, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint144(Slice self) internal pure returns (uint144[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 18, false); + function decodeArray_uint144(Slice _input) internal pure returns (uint144[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 18, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint152(Slice self) internal pure returns (uint152[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 19, false); + function decodeArray_uint152(Slice _input) internal pure returns (uint152[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 19, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint160(Slice self) internal pure returns (uint160[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 20, false); + function decodeArray_uint160(Slice _input) internal pure returns (uint160[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint168(Slice self) internal pure returns (uint168[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 21, false); + function decodeArray_uint168(Slice _input) internal pure returns (uint168[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 21, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint176(Slice self) internal pure returns (uint176[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 22, false); + function decodeArray_uint176(Slice _input) internal pure returns (uint176[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 22, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint184(Slice self) internal pure returns (uint184[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 23, false); + function decodeArray_uint184(Slice _input) internal pure returns (uint184[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 23, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint192(Slice self) internal pure returns (uint192[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 24, false); + function decodeArray_uint192(Slice _input) internal pure returns (uint192[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 24, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint200(Slice self) internal pure returns (uint200[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 25, false); + function decodeArray_uint200(Slice _input) internal pure returns (uint200[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 25, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint208(Slice self) internal pure returns (uint208[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 26, false); + function decodeArray_uint208(Slice _input) internal pure returns (uint208[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 26, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint216(Slice self) internal pure returns (uint216[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 27, false); + function decodeArray_uint216(Slice _input) internal pure returns (uint216[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 27, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint224(Slice self) internal pure returns (uint224[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 28, false); + function decodeArray_uint224(Slice _input) internal pure returns (uint224[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 28, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint232(Slice self) internal pure returns (uint232[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 29, false); + function decodeArray_uint232(Slice _input) internal pure returns (uint232[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 29, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint240(Slice self) internal pure returns (uint240[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 30, false); + function decodeArray_uint240(Slice _input) internal pure returns (uint240[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 30, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint248(Slice self) internal pure returns (uint248[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 31, false); + function decodeArray_uint248(Slice _input) internal pure returns (uint248[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 31, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_uint256(Slice self) internal pure returns (uint256[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 32, false); + function decodeArray_uint256(Slice _input) internal pure returns (uint256[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 32, false); assembly { - output := genericArray + _output := _genericArray } } - /************************************************************************ - * - * int8 - int256 - * - ************************************************************************/ - - function decodeArray_int8(Slice self) internal pure returns (int8[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 1, false); + function decodeArray_int8(Slice _input) internal pure returns (int8[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int16(Slice self) internal pure returns (int16[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 2, false); + function decodeArray_int16(Slice _input) internal pure returns (int16[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 2, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int24(Slice self) internal pure returns (int24[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 3, false); + function decodeArray_int24(Slice _input) internal pure returns (int24[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 3, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int32(Slice self) internal pure returns (int32[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 4, false); + function decodeArray_int32(Slice _input) internal pure returns (int32[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 4, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int40(Slice self) internal pure returns (int40[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 5, false); + function decodeArray_int40(Slice _input) internal pure returns (int40[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 5, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int48(Slice self) internal pure returns (int48[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 6, false); + function decodeArray_int48(Slice _input) internal pure returns (int48[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 6, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int56(Slice self) internal pure returns (int56[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 7, false); + function decodeArray_int56(Slice _input) internal pure returns (int56[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 7, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int64(Slice self) internal pure returns (int64[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 8, false); + function decodeArray_int64(Slice _input) internal pure returns (int64[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 8, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int72(Slice self) internal pure returns (int72[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 9, false); + function decodeArray_int72(Slice _input) internal pure returns (int72[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 9, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int80(Slice self) internal pure returns (int80[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 10, false); + function decodeArray_int80(Slice _input) internal pure returns (int80[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 10, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int88(Slice self) internal pure returns (int88[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 11, false); + function decodeArray_int88(Slice _input) internal pure returns (int88[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 11, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int96(Slice self) internal pure returns (int96[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 12, false); + function decodeArray_int96(Slice _input) internal pure returns (int96[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 12, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int104(Slice self) internal pure returns (int104[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 13, false); + function decodeArray_int104(Slice _input) internal pure returns (int104[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 13, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int112(Slice self) internal pure returns (int112[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 14, false); + function decodeArray_int112(Slice _input) internal pure returns (int112[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 14, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int120(Slice self) internal pure returns (int120[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 15, false); + function decodeArray_int120(Slice _input) internal pure returns (int120[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 15, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int128(Slice self) internal pure returns (int128[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 16, false); + function decodeArray_int128(Slice _input) internal pure returns (int128[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 16, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int136(Slice self) internal pure returns (int136[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 17, false); + function decodeArray_int136(Slice _input) internal pure returns (int136[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 17, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int144(Slice self) internal pure returns (int144[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 18, false); + function decodeArray_int144(Slice _input) internal pure returns (int144[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 18, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int152(Slice self) internal pure returns (int152[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 19, false); + function decodeArray_int152(Slice _input) internal pure returns (int152[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 19, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int160(Slice self) internal pure returns (int160[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 20, false); + function decodeArray_int160(Slice _input) internal pure returns (int160[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int168(Slice self) internal pure returns (int168[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 21, false); + function decodeArray_int168(Slice _input) internal pure returns (int168[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 21, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int176(Slice self) internal pure returns (int176[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 22, false); + function decodeArray_int176(Slice _input) internal pure returns (int176[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 22, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int184(Slice self) internal pure returns (int184[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 23, false); + function decodeArray_int184(Slice _input) internal pure returns (int184[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 23, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int192(Slice self) internal pure returns (int192[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 24, false); + function decodeArray_int192(Slice _input) internal pure returns (int192[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 24, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int200(Slice self) internal pure returns (int200[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 25, false); + function decodeArray_int200(Slice _input) internal pure returns (int200[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 25, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int208(Slice self) internal pure returns (int208[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 26, false); + function decodeArray_int208(Slice _input) internal pure returns (int208[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 26, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int216(Slice self) internal pure returns (int216[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 27, false); + function decodeArray_int216(Slice _input) internal pure returns (int216[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 27, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int224(Slice self) internal pure returns (int224[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 28, false); + function decodeArray_int224(Slice _input) internal pure returns (int224[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 28, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int232(Slice self) internal pure returns (int232[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 29, false); + function decodeArray_int232(Slice _input) internal pure returns (int232[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 29, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int240(Slice self) internal pure returns (int240[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 30, false); + function decodeArray_int240(Slice _input) internal pure returns (int240[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 30, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int248(Slice self) internal pure returns (int248[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 31, false); + function decodeArray_int248(Slice _input) internal pure returns (int248[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 31, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_int256(Slice self) internal pure returns (int256[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 32, false); + function decodeArray_int256(Slice _input) internal pure returns (int256[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 32, false); assembly { - output := genericArray + _output := _genericArray } } - /************************************************************************ - * - * bytes1 - bytes32 - * - ************************************************************************/ - - function decodeArray_bytes1(Slice self) internal pure returns (bytes1[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 1, true); + function decodeArray_bytes1(Slice _input) internal pure returns (bytes1[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes2(Slice self) internal pure returns (bytes2[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 2, true); + function decodeArray_bytes2(Slice _input) internal pure returns (bytes2[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 2, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes3(Slice self) internal pure returns (bytes3[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 3, true); + function decodeArray_bytes3(Slice _input) internal pure returns (bytes3[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 3, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes4(Slice self) internal pure returns (bytes4[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 4, true); + function decodeArray_bytes4(Slice _input) internal pure returns (bytes4[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 4, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes5(Slice self) internal pure returns (bytes5[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 5, true); + function decodeArray_bytes5(Slice _input) internal pure returns (bytes5[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 5, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes6(Slice self) internal pure returns (bytes6[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 6, true); + function decodeArray_bytes6(Slice _input) internal pure returns (bytes6[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 6, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes7(Slice self) internal pure returns (bytes7[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 7, true); + function decodeArray_bytes7(Slice _input) internal pure returns (bytes7[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 7, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes8(Slice self) internal pure returns (bytes8[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 8, true); + function decodeArray_bytes8(Slice _input) internal pure returns (bytes8[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 8, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes9(Slice self) internal pure returns (bytes9[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 9, true); + function decodeArray_bytes9(Slice _input) internal pure returns (bytes9[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 9, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes10(Slice self) internal pure returns (bytes10[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 10, true); + function decodeArray_bytes10(Slice _input) internal pure returns (bytes10[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 10, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes11(Slice self) internal pure returns (bytes11[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 11, true); + function decodeArray_bytes11(Slice _input) internal pure returns (bytes11[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 11, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes12(Slice self) internal pure returns (bytes12[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 12, true); + function decodeArray_bytes12(Slice _input) internal pure returns (bytes12[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 12, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes13(Slice self) internal pure returns (bytes13[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 13, true); + function decodeArray_bytes13(Slice _input) internal pure returns (bytes13[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 13, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes14(Slice self) internal pure returns (bytes14[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 14, true); + function decodeArray_bytes14(Slice _input) internal pure returns (bytes14[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 14, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes15(Slice self) internal pure returns (bytes15[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 15, true); + function decodeArray_bytes15(Slice _input) internal pure returns (bytes15[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 15, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes16(Slice self) internal pure returns (bytes16[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 16, true); + function decodeArray_bytes16(Slice _input) internal pure returns (bytes16[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 16, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes17(Slice self) internal pure returns (bytes17[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 17, true); + function decodeArray_bytes17(Slice _input) internal pure returns (bytes17[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 17, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes18(Slice self) internal pure returns (bytes18[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 18, true); + function decodeArray_bytes18(Slice _input) internal pure returns (bytes18[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 18, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes19(Slice self) internal pure returns (bytes19[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 19, true); + function decodeArray_bytes19(Slice _input) internal pure returns (bytes19[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 19, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes20(Slice self) internal pure returns (bytes20[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 20, true); + function decodeArray_bytes20(Slice _input) internal pure returns (bytes20[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes21(Slice self) internal pure returns (bytes21[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 21, true); + function decodeArray_bytes21(Slice _input) internal pure returns (bytes21[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 21, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes22(Slice self) internal pure returns (bytes22[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 22, true); + function decodeArray_bytes22(Slice _input) internal pure returns (bytes22[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 22, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes23(Slice self) internal pure returns (bytes23[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 23, true); + function decodeArray_bytes23(Slice _input) internal pure returns (bytes23[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 23, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes24(Slice self) internal pure returns (bytes24[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 24, true); + function decodeArray_bytes24(Slice _input) internal pure returns (bytes24[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 24, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes25(Slice self) internal pure returns (bytes25[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 25, true); + function decodeArray_bytes25(Slice _input) internal pure returns (bytes25[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 25, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes26(Slice self) internal pure returns (bytes26[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 26, true); + function decodeArray_bytes26(Slice _input) internal pure returns (bytes26[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 26, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes27(Slice self) internal pure returns (bytes27[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 27, true); + function decodeArray_bytes27(Slice _input) internal pure returns (bytes27[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 27, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes28(Slice self) internal pure returns (bytes28[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 28, true); + function decodeArray_bytes28(Slice _input) internal pure returns (bytes28[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 28, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes29(Slice self) internal pure returns (bytes29[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 29, true); + function decodeArray_bytes29(Slice _input) internal pure returns (bytes29[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 29, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes30(Slice self) internal pure returns (bytes30[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 30, true); + function decodeArray_bytes30(Slice _input) internal pure returns (bytes30[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 30, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes31(Slice self) internal pure returns (bytes31[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 31, true); + function decodeArray_bytes31(Slice _input) internal pure returns (bytes31[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 31, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bytes32(Slice self) internal pure returns (bytes32[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 32, true); - assembly { - output := genericArray - } - } - - /************************************************************************ - * - * Other types - * - ************************************************************************/ - - function decodeArray_address(Slice self) internal pure returns (address[] memory output) { - // Note: internally address is right-aligned, like uint160 - bytes32[] memory genericArray = TightCoder.decode(self, 20, false); + function decodeArray_bytes32(Slice _input) internal pure returns (bytes32[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 32, true); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_bool(Slice self) internal pure returns (bool[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 1, false); + function decodeArray_bool(Slice _input) internal pure returns (bool[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, false); assembly { - output := genericArray + _output := _genericArray } } - function decodeArray_SchemaType(Slice self) internal pure returns (SchemaType[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 1, false); + function decodeArray_address(Slice _input) internal pure returns (address[] memory _output) { + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, false); assembly { - output := genericArray + _output := _genericArray } } } diff --git a/packages/store/src/tightcoder/EncodeArray.sol b/packages/store/src/tightcoder/EncodeArray.sol index dd2dc875ad..455db997b6 100644 --- a/packages/store/src/tightcoder/EncodeArray.sol +++ b/packages/store/src/tightcoder/EncodeArray.sol @@ -1,836 +1,791 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.0; -/* Autogenerated via `pnpm codegen`. Do not edit the `.sol` file manually. */ - -import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; +/* Autogenerated file. Do not edit manually. */ import { TightCoder } from "./TightCoder.sol"; library EncodeArray { - /************************************************************************ - * - * uint8 - uint256 - * - ************************************************************************/ - - function encode(uint8[] memory input) internal pure returns (bytes memory output) { + function encode(uint8[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 1, false); + return TightCoder.encode(_genericArray, 1, 248); } - function encode(uint16[] memory input) internal pure returns (bytes memory output) { + function encode(uint16[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 2, false); + return TightCoder.encode(_genericArray, 2, 240); } - function encode(uint24[] memory input) internal pure returns (bytes memory output) { + function encode(uint24[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 3, false); + return TightCoder.encode(_genericArray, 3, 232); } - function encode(uint32[] memory input) internal pure returns (bytes memory output) { + function encode(uint32[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 4, false); + return TightCoder.encode(_genericArray, 4, 224); } - function encode(uint40[] memory input) internal pure returns (bytes memory output) { + function encode(uint40[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 5, false); + return TightCoder.encode(_genericArray, 5, 216); } - function encode(uint48[] memory input) internal pure returns (bytes memory output) { + function encode(uint48[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 6, false); + return TightCoder.encode(_genericArray, 6, 208); } - function encode(uint56[] memory input) internal pure returns (bytes memory output) { + function encode(uint56[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 7, false); + return TightCoder.encode(_genericArray, 7, 200); } - function encode(uint64[] memory input) internal pure returns (bytes memory output) { + function encode(uint64[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 8, false); + return TightCoder.encode(_genericArray, 8, 192); } - function encode(uint72[] memory input) internal pure returns (bytes memory output) { + function encode(uint72[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 9, false); + return TightCoder.encode(_genericArray, 9, 184); } - function encode(uint80[] memory input) internal pure returns (bytes memory output) { + function encode(uint80[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 10, false); + return TightCoder.encode(_genericArray, 10, 176); } - function encode(uint88[] memory input) internal pure returns (bytes memory output) { + function encode(uint88[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 11, false); + return TightCoder.encode(_genericArray, 11, 168); } - function encode(uint96[] memory input) internal pure returns (bytes memory output) { + function encode(uint96[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 12, false); + return TightCoder.encode(_genericArray, 12, 160); } - function encode(uint104[] memory input) internal pure returns (bytes memory output) { + function encode(uint104[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 13, false); + return TightCoder.encode(_genericArray, 13, 152); } - function encode(uint112[] memory input) internal pure returns (bytes memory output) { + function encode(uint112[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 14, false); + return TightCoder.encode(_genericArray, 14, 144); } - function encode(uint120[] memory input) internal pure returns (bytes memory output) { + function encode(uint120[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 15, false); + return TightCoder.encode(_genericArray, 15, 136); } - function encode(uint128[] memory input) internal pure returns (bytes memory output) { + function encode(uint128[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 16, false); + return TightCoder.encode(_genericArray, 16, 128); } - function encode(uint136[] memory input) internal pure returns (bytes memory output) { + function encode(uint136[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 17, false); + return TightCoder.encode(_genericArray, 17, 120); } - function encode(uint144[] memory input) internal pure returns (bytes memory output) { + function encode(uint144[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 18, false); + return TightCoder.encode(_genericArray, 18, 112); } - function encode(uint152[] memory input) internal pure returns (bytes memory output) { + function encode(uint152[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 19, false); + return TightCoder.encode(_genericArray, 19, 104); } - function encode(uint160[] memory input) internal pure returns (bytes memory output) { + function encode(uint160[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 20, false); + return TightCoder.encode(_genericArray, 20, 96); } - function encode(uint168[] memory input) internal pure returns (bytes memory output) { + function encode(uint168[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 21, false); + return TightCoder.encode(_genericArray, 21, 88); } - function encode(uint176[] memory input) internal pure returns (bytes memory output) { + function encode(uint176[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 22, false); + return TightCoder.encode(_genericArray, 22, 80); } - function encode(uint184[] memory input) internal pure returns (bytes memory output) { + function encode(uint184[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 23, false); + return TightCoder.encode(_genericArray, 23, 72); } - function encode(uint192[] memory input) internal pure returns (bytes memory output) { + function encode(uint192[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 24, false); + return TightCoder.encode(_genericArray, 24, 64); } - function encode(uint200[] memory input) internal pure returns (bytes memory output) { + function encode(uint200[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 25, false); + return TightCoder.encode(_genericArray, 25, 56); } - function encode(uint208[] memory input) internal pure returns (bytes memory output) { + function encode(uint208[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 26, false); + return TightCoder.encode(_genericArray, 26, 48); } - function encode(uint216[] memory input) internal pure returns (bytes memory output) { + function encode(uint216[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 27, false); + return TightCoder.encode(_genericArray, 27, 40); } - function encode(uint224[] memory input) internal pure returns (bytes memory output) { + function encode(uint224[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 28, false); + return TightCoder.encode(_genericArray, 28, 32); } - function encode(uint232[] memory input) internal pure returns (bytes memory output) { + function encode(uint232[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 29, false); + return TightCoder.encode(_genericArray, 29, 24); } - function encode(uint240[] memory input) internal pure returns (bytes memory output) { + function encode(uint240[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 30, false); + return TightCoder.encode(_genericArray, 30, 16); } - function encode(uint248[] memory input) internal pure returns (bytes memory output) { + function encode(uint248[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 31, false); + return TightCoder.encode(_genericArray, 31, 8); } - function encode(uint256[] memory input) internal pure returns (bytes memory output) { + function encode(uint256[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 32, false); + return TightCoder.encode(_genericArray, 32, 0); } - /************************************************************************ - * - * int8 - int256 - * - ************************************************************************/ - - function encode(int8[] memory input) internal pure returns (bytes memory output) { + function encode(int8[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 1, false); + return TightCoder.encode(_genericArray, 1, 248); } - function encode(int16[] memory input) internal pure returns (bytes memory output) { + function encode(int16[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 2, false); + return TightCoder.encode(_genericArray, 2, 240); } - function encode(int24[] memory input) internal pure returns (bytes memory output) { + function encode(int24[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 3, false); + return TightCoder.encode(_genericArray, 3, 232); } - function encode(int32[] memory input) internal pure returns (bytes memory output) { + function encode(int32[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 4, false); + return TightCoder.encode(_genericArray, 4, 224); } - function encode(int40[] memory input) internal pure returns (bytes memory output) { + function encode(int40[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 5, false); + return TightCoder.encode(_genericArray, 5, 216); } - function encode(int48[] memory input) internal pure returns (bytes memory output) { + function encode(int48[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 6, false); + return TightCoder.encode(_genericArray, 6, 208); } - function encode(int56[] memory input) internal pure returns (bytes memory output) { + function encode(int56[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 7, false); + return TightCoder.encode(_genericArray, 7, 200); } - function encode(int64[] memory input) internal pure returns (bytes memory output) { + function encode(int64[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 8, false); + return TightCoder.encode(_genericArray, 8, 192); } - function encode(int72[] memory input) internal pure returns (bytes memory output) { + function encode(int72[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 9, false); + return TightCoder.encode(_genericArray, 9, 184); } - function encode(int80[] memory input) internal pure returns (bytes memory output) { + function encode(int80[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 10, false); + return TightCoder.encode(_genericArray, 10, 176); } - function encode(int88[] memory input) internal pure returns (bytes memory output) { + function encode(int88[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 11, false); + return TightCoder.encode(_genericArray, 11, 168); } - function encode(int96[] memory input) internal pure returns (bytes memory output) { + function encode(int96[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 12, false); + return TightCoder.encode(_genericArray, 12, 160); } - function encode(int104[] memory input) internal pure returns (bytes memory output) { + function encode(int104[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 13, false); + return TightCoder.encode(_genericArray, 13, 152); } - function encode(int112[] memory input) internal pure returns (bytes memory output) { + function encode(int112[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 14, false); + return TightCoder.encode(_genericArray, 14, 144); } - function encode(int120[] memory input) internal pure returns (bytes memory output) { + function encode(int120[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 15, false); + return TightCoder.encode(_genericArray, 15, 136); } - function encode(int128[] memory input) internal pure returns (bytes memory output) { + function encode(int128[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 16, false); + return TightCoder.encode(_genericArray, 16, 128); } - function encode(int136[] memory input) internal pure returns (bytes memory output) { + function encode(int136[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 17, false); + return TightCoder.encode(_genericArray, 17, 120); } - function encode(int144[] memory input) internal pure returns (bytes memory output) { + function encode(int144[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 18, false); + return TightCoder.encode(_genericArray, 18, 112); } - function encode(int152[] memory input) internal pure returns (bytes memory output) { + function encode(int152[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 19, false); + return TightCoder.encode(_genericArray, 19, 104); } - function encode(int160[] memory input) internal pure returns (bytes memory output) { + function encode(int160[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 20, false); + return TightCoder.encode(_genericArray, 20, 96); } - function encode(int168[] memory input) internal pure returns (bytes memory output) { + function encode(int168[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 21, false); + return TightCoder.encode(_genericArray, 21, 88); } - function encode(int176[] memory input) internal pure returns (bytes memory output) { + function encode(int176[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 22, false); + return TightCoder.encode(_genericArray, 22, 80); } - function encode(int184[] memory input) internal pure returns (bytes memory output) { + function encode(int184[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 23, false); + return TightCoder.encode(_genericArray, 23, 72); } - function encode(int192[] memory input) internal pure returns (bytes memory output) { + function encode(int192[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 24, false); + return TightCoder.encode(_genericArray, 24, 64); } - function encode(int200[] memory input) internal pure returns (bytes memory output) { + function encode(int200[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 25, false); + return TightCoder.encode(_genericArray, 25, 56); } - function encode(int208[] memory input) internal pure returns (bytes memory output) { + function encode(int208[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 26, false); + return TightCoder.encode(_genericArray, 26, 48); } - function encode(int216[] memory input) internal pure returns (bytes memory output) { + function encode(int216[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 27, false); + return TightCoder.encode(_genericArray, 27, 40); } - function encode(int224[] memory input) internal pure returns (bytes memory output) { + function encode(int224[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 28, false); + return TightCoder.encode(_genericArray, 28, 32); } - function encode(int232[] memory input) internal pure returns (bytes memory output) { + function encode(int232[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 29, false); + return TightCoder.encode(_genericArray, 29, 24); } - function encode(int240[] memory input) internal pure returns (bytes memory output) { + function encode(int240[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 30, false); + return TightCoder.encode(_genericArray, 30, 16); } - function encode(int248[] memory input) internal pure returns (bytes memory output) { + function encode(int248[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 31, false); + return TightCoder.encode(_genericArray, 31, 8); } - function encode(int256[] memory input) internal pure returns (bytes memory output) { + function encode(int256[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 32, false); + return TightCoder.encode(_genericArray, 32, 0); } - /************************************************************************ - * - * bytes1 - bytes32 - * - ************************************************************************/ - - function encode(bytes1[] memory input) internal pure returns (bytes memory output) { + function encode(bytes1[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 1, true); + return TightCoder.encode(_genericArray, 1, 0); } - function encode(bytes2[] memory input) internal pure returns (bytes memory output) { + function encode(bytes2[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 2, true); + return TightCoder.encode(_genericArray, 2, 0); } - function encode(bytes3[] memory input) internal pure returns (bytes memory output) { + function encode(bytes3[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 3, true); + return TightCoder.encode(_genericArray, 3, 0); } - function encode(bytes4[] memory input) internal pure returns (bytes memory output) { + function encode(bytes4[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 4, true); + return TightCoder.encode(_genericArray, 4, 0); } - function encode(bytes5[] memory input) internal pure returns (bytes memory output) { + function encode(bytes5[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 5, true); + return TightCoder.encode(_genericArray, 5, 0); } - function encode(bytes6[] memory input) internal pure returns (bytes memory output) { + function encode(bytes6[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 6, true); + return TightCoder.encode(_genericArray, 6, 0); } - function encode(bytes7[] memory input) internal pure returns (bytes memory output) { + function encode(bytes7[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 7, true); + return TightCoder.encode(_genericArray, 7, 0); } - function encode(bytes8[] memory input) internal pure returns (bytes memory output) { + function encode(bytes8[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 8, true); + return TightCoder.encode(_genericArray, 8, 0); } - function encode(bytes9[] memory input) internal pure returns (bytes memory output) { + function encode(bytes9[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 9, true); + return TightCoder.encode(_genericArray, 9, 0); } - function encode(bytes10[] memory input) internal pure returns (bytes memory output) { + function encode(bytes10[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 10, true); + return TightCoder.encode(_genericArray, 10, 0); } - function encode(bytes11[] memory input) internal pure returns (bytes memory output) { + function encode(bytes11[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 11, true); + return TightCoder.encode(_genericArray, 11, 0); } - function encode(bytes12[] memory input) internal pure returns (bytes memory output) { + function encode(bytes12[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 12, true); + return TightCoder.encode(_genericArray, 12, 0); } - function encode(bytes13[] memory input) internal pure returns (bytes memory output) { + function encode(bytes13[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 13, true); + return TightCoder.encode(_genericArray, 13, 0); } - function encode(bytes14[] memory input) internal pure returns (bytes memory output) { + function encode(bytes14[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 14, true); + return TightCoder.encode(_genericArray, 14, 0); } - function encode(bytes15[] memory input) internal pure returns (bytes memory output) { + function encode(bytes15[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 15, true); + return TightCoder.encode(_genericArray, 15, 0); } - function encode(bytes16[] memory input) internal pure returns (bytes memory output) { + function encode(bytes16[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 16, true); + return TightCoder.encode(_genericArray, 16, 0); } - function encode(bytes17[] memory input) internal pure returns (bytes memory output) { + function encode(bytes17[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 17, true); + return TightCoder.encode(_genericArray, 17, 0); } - function encode(bytes18[] memory input) internal pure returns (bytes memory output) { + function encode(bytes18[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 18, true); + return TightCoder.encode(_genericArray, 18, 0); } - function encode(bytes19[] memory input) internal pure returns (bytes memory output) { + function encode(bytes19[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 19, true); + return TightCoder.encode(_genericArray, 19, 0); } - function encode(bytes20[] memory input) internal pure returns (bytes memory output) { + function encode(bytes20[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 20, true); + return TightCoder.encode(_genericArray, 20, 0); } - function encode(bytes21[] memory input) internal pure returns (bytes memory output) { + function encode(bytes21[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 21, true); + return TightCoder.encode(_genericArray, 21, 0); } - function encode(bytes22[] memory input) internal pure returns (bytes memory output) { + function encode(bytes22[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 22, true); + return TightCoder.encode(_genericArray, 22, 0); } - function encode(bytes23[] memory input) internal pure returns (bytes memory output) { + function encode(bytes23[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 23, true); + return TightCoder.encode(_genericArray, 23, 0); } - function encode(bytes24[] memory input) internal pure returns (bytes memory output) { + function encode(bytes24[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 24, true); + return TightCoder.encode(_genericArray, 24, 0); } - function encode(bytes25[] memory input) internal pure returns (bytes memory output) { + function encode(bytes25[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 25, true); + return TightCoder.encode(_genericArray, 25, 0); } - function encode(bytes26[] memory input) internal pure returns (bytes memory output) { + function encode(bytes26[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 26, true); + return TightCoder.encode(_genericArray, 26, 0); } - function encode(bytes27[] memory input) internal pure returns (bytes memory output) { + function encode(bytes27[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 27, true); + return TightCoder.encode(_genericArray, 27, 0); } - function encode(bytes28[] memory input) internal pure returns (bytes memory output) { + function encode(bytes28[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 28, true); + return TightCoder.encode(_genericArray, 28, 0); } - function encode(bytes29[] memory input) internal pure returns (bytes memory output) { + function encode(bytes29[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 29, true); + return TightCoder.encode(_genericArray, 29, 0); } - function encode(bytes30[] memory input) internal pure returns (bytes memory output) { + function encode(bytes30[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 30, true); + return TightCoder.encode(_genericArray, 30, 0); } - function encode(bytes31[] memory input) internal pure returns (bytes memory output) { + function encode(bytes31[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 31, true); + return TightCoder.encode(_genericArray, 31, 0); } - function encode(bytes32[] memory input) internal pure returns (bytes memory output) { + function encode(bytes32[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 32, true); + return TightCoder.encode(_genericArray, 32, 0); } - /************************************************************************ - * - * Other types - * - ************************************************************************/ - - function encode(address[] memory input) internal pure returns (bytes memory output) { + function encode(bool[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input + _genericArray := _input } - return TightCoder.encode(_genericArray, 20, false); + return TightCoder.encode(_genericArray, 1, 248); } - function encode(bool[] memory input) internal pure returns (bytes memory output) { + function encode(address[] memory _input) internal pure returns (bytes memory) { bytes32[] memory _genericArray; assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, 1, false); - } - - function encode(SchemaType[] memory input) internal pure returns (bytes memory output) { - bytes32[] memory _genericArray; - assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, 1, false); - } - - /** - * Converts a `bytes` memory array to a single `bytes` memory value. - * TODO: optimize gas cost - */ - function encode(bytes[] memory input) internal pure returns (bytes memory output) { - output = new bytes(0); - for (uint256 i; i < input.length; i++) { - output = bytes.concat(output, input[i]); + _genericArray := _input } + return TightCoder.encode(_genericArray, 20, 96); } } diff --git a/packages/store/src/tightcoder/TightCoder.sol b/packages/store/src/tightcoder/TightCoder.sol index 200f9e4290..a2f4d91f81 100644 --- a/packages/store/src/tightcoder/TightCoder.sol +++ b/packages/store/src/tightcoder/TightCoder.sol @@ -6,60 +6,38 @@ import { Slice, SliceLib } from "../Slice.sol"; library TightCoder { /** - * @dev Copies the array to the location of `packedSlice`, + * @dev Copies the array to a new bytes array, * tightly packing it using the given size per element (in bytes) - * - * TODO this function is currently not used externally and will be changed in the future - * (see https://github.com/latticexyz/mud/issues/444) */ - function _encodeToLocation( + function encode( bytes32[] memory array, - Slice packedSlice, uint256 elementSize, - bool leftAligned - ) private pure { + uint256 shiftLeftBits + ) internal pure returns (bytes memory data) { uint256 arrayLength = array.length; - uint256 packedPointer = packedSlice.pointer(); - uint256 shiftLeft = leftAligned ? 0 : 256 - elementSize * 8; - - // TODO temporary check to catch bugs, either remove it or use a custom error - // (see https://github.com/latticexyz/mud/issues/444) - uint256 packedLength = arrayLength * elementSize; - if (packedLength > packedSlice.length()) { - revert("packFromArray: insufficient allocated packedSlice length"); - } + uint256 packedLength = array.length * elementSize; + data = new bytes(packedLength); /// @solidity memory-safe-assembly assembly { for { let i := 0 - let arrayCursor := add(array, 0x20) // skip array length - let packedCursor := packedPointer + // Skip array length + let fromPointer := add(array, 0x20) + let toPointer := add(data, 0x20) } lt(i, arrayLength) { // Loop until we reach the end of the array i := add(i, 1) - arrayCursor := add(arrayCursor, 0x20) // increment array pointer by one word - packedCursor := add(packedCursor, elementSize) // increment packed pointer by one element size + // Increment array pointer by one word + fromPointer := add(fromPointer, 0x20) + // Increment packed pointer by one element size + toPointer := add(toPointer, elementSize) } { - mstore(packedCursor, shl(shiftLeft, mload(arrayCursor))) // pack one array element + mstore(toPointer, shl(shiftLeftBits, mload(fromPointer))) // pack one array element } } } - /** - * @dev Copies the array to a new bytes array, - * tightly packing it using the given size per element (in bytes) - */ - function encode( - bytes32[] memory array, - uint256 elementSize, - bool leftAligned - ) internal pure returns (bytes memory data) { - uint256 packedLength = array.length * elementSize; - data = new bytes(packedLength); - _encodeToLocation(array, SliceLib.fromBytes(data), elementSize, leftAligned); - } - /** * @dev Unpacks the slice to a new memory location * and lays it out like a memory array with the given size per element (in bytes) diff --git a/packages/store/test/tightcoder/EncodeArray.t.sol b/packages/store/test/tightcoder/EncodeArray.t.sol index 989cfc0038..2dcd24d9e1 100644 --- a/packages/store/test/tightcoder/EncodeArray.t.sol +++ b/packages/store/test/tightcoder/EncodeArray.t.sol @@ -7,24 +7,6 @@ import { Bytes } from "../../src/Bytes.sol"; import { EncodeArray } from "../../src/tightcoder/EncodeArray.sol"; contract EncodeArrayTest is Test, GasReporter { - function testEncodeBytesArray() public { - bytes[] memory input = new bytes[](2); - input[0] = new bytes(32); - input[0][0] = 0x01; - input[0][31] = 0x02; - input[1] = new bytes(32); - input[1][0] = 0x03; - input[1][31] = 0x04; - - startGasReport("encode packed bytes[]"); - bytes memory output = EncodeArray.encode(input); - endGasReport(); - - assertEq(output.length, 64); - assertEq(uint256(Bytes.toBytes32(output, 0)), 0x0100000000000000000000000000000000000000000000000000000000000002); - assertEq(uint256(Bytes.toBytes32(output, 32)), 0x0300000000000000000000000000000000000000000000000000000000000004); - } - function testEncodeUint8Array() public { uint8 val0 = 0x01; uint8 val1 = 0x02; diff --git a/packages/store/test/tightcoder/TightCoder.t.sol b/packages/store/test/tightcoder/TightCoder.t.sol index 0f1e183769..5803407e88 100644 --- a/packages/store/test/tightcoder/TightCoder.t.sol +++ b/packages/store/test/tightcoder/TightCoder.t.sol @@ -36,7 +36,7 @@ contract TightCoderTest is Test, GasReporter { assertEq(packed.length, 48); - startGasReport("decode packed uint32[]"); + startGasReport("decode packed bytes24[]"); bytes24[] memory output = SliceLib.fromBytes(packed).decodeArray_bytes24(); endGasReport(); @@ -44,56 +44,4 @@ contract TightCoderTest is Test, GasReporter { assertEq(output[0], input[0]); assertEq(output[1], input[1]); } - - /************************************************************************ - * - * Other types - * - ************************************************************************/ - - function testEncodeDecodeArray__address(address val0, address val1, address val2) public { - address[] memory input = new address[](3); - input[0] = val0; - input[1] = val1; - input[2] = val2; - - bytes memory output = EncodeArray.encode(input); - - assertEq(output, abi.encodePacked(val0, val1, val2)); - } - - function testEncodeDecodeArray__bool(bool val0, bool val1, bool val2) public { - bool[] memory input = new bool[](3); - input[0] = val0; - input[1] = val1; - input[2] = val2; - - bytes memory encoded = EncodeArray.encode(input); - assertEq(encoded, abi.encodePacked(val0, val1, val2)); - - bool[] memory decoded = SliceLib.fromBytes(encoded).decodeArray_bool(); - assertEq(decoded.length, 3); - assertEq(decoded[0], val0); - assertEq(decoded[1], val1); - assertEq(decoded[2], val2); - } - - function testEncodeDecodeArray__SchemaType() public { - SchemaType val0 = SchemaType.UINT8; - SchemaType val1 = SchemaType.INT128; - SchemaType val2 = SchemaType.STRING; - SchemaType[] memory input = new SchemaType[](3); - input[0] = val0; - input[1] = val1; - input[2] = val2; - - bytes memory encoded = EncodeArray.encode(input); - assertEq(encoded, abi.encodePacked(val0, val1, val2)); - - SchemaType[] memory decoded = SliceLib.fromBytes(encoded).decodeArray_SchemaType(); - assertEq(decoded.length, 3); - assertEq(uint8(decoded[0]), uint8(val0)); - assertEq(uint8(decoded[1]), uint8(val1)); - assertEq(uint8(decoded[2]), uint8(val2)); - } } diff --git a/packages/store/test/tightcoder/TightCoderAuto.t.sol b/packages/store/test/tightcoder/TightCoderAuto.t.sol index 1543e1e366..0022bc9634 100644 --- a/packages/store/test/tightcoder/TightCoderAuto.t.sol +++ b/packages/store/test/tightcoder/TightCoderAuto.t.sol @@ -1,20 +1,13 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.0; -/* Autogenerated via `pnpm codegen`. Do not edit the `.sol` file manually. */ +/* Autogenerated file. Do not edit manually. */ -import "forge-std/Test.sol"; -import { Bytes } from "../../src/Bytes.sol"; +import { Test } from "forge-std/Test.sol"; import { EncodeArray } from "../../src/tightcoder/EncodeArray.sol"; import { SliceLib } from "../../src/Slice.sol"; contract TightCoderAutoTest is Test { - /************************************************************************ - * - * uint8 - uint256 - * - ************************************************************************/ - function testEncodeDecodeArray_uint8(uint8 val0, uint8 val1, uint8 val2) public { uint8[] memory input = new uint8[](3); input[0] = val0; @@ -527,12 +520,6 @@ contract TightCoderAutoTest is Test { assertEq(decoded[2], val2); } - /************************************************************************ - * - * int8 - int256 - * - ************************************************************************/ - function testEncodeDecodeArray_int8(int8 val0, int8 val1, int8 val2) public { int8[] memory input = new int8[](3); input[0] = val0; @@ -1045,12 +1032,6 @@ contract TightCoderAutoTest is Test { assertEq(decoded[2], val2); } - /************************************************************************ - * - * bytes1 - bytes32 - * - ************************************************************************/ - function testEncodeDecodeArray_bytes1(bytes1 val0, bytes1 val1, bytes1 val2) public { bytes1[] memory input = new bytes1[](3); input[0] = val0; @@ -1562,4 +1543,36 @@ contract TightCoderAutoTest is Test { assertEq(decoded[1], val1); assertEq(decoded[2], val2); } + + function testEncodeDecodeArray_bool(bool val0, bool val1, bool val2) public { + bool[] memory input = new bool[](3); + input[0] = val0; + input[1] = val1; + input[2] = val2; + + bytes memory encoded = EncodeArray.encode(input); + assertEq(encoded, abi.encodePacked(val0, val1, val2)); + + bool[] memory decoded = SliceLib.fromBytes(encoded).decodeArray_bool(); + assertEq(decoded.length, 3); + assertEq(decoded[0], val0); + assertEq(decoded[1], val1); + assertEq(decoded[2], val2); + } + + function testEncodeDecodeArray_address(address val0, address val1, address val2) public { + address[] memory input = new address[](3); + input[0] = val0; + input[1] = val1; + input[2] = val2; + + bytes memory encoded = EncodeArray.encode(input); + assertEq(encoded, abi.encodePacked(val0, val1, val2)); + + address[] memory decoded = SliceLib.fromBytes(encoded).decodeArray_address(); + assertEq(decoded.length, 3); + assertEq(decoded[0], val0); + assertEq(decoded[1], val1); + assertEq(decoded[2], val2); + } } diff --git a/packages/store/ts/codegen/tightcoder/index.ts b/packages/store/ts/codegen/tightcoder/index.ts new file mode 100644 index 0000000000..0d46ef2076 --- /dev/null +++ b/packages/store/ts/codegen/tightcoder/index.ts @@ -0,0 +1,4 @@ +export * from "./renderDecodeSlice"; +export * from "./renderEncodeArray"; +export * from "./renderFunctions"; +export * from "./renderTightCoderAutoTest"; diff --git a/packages/store/ts/codegen/tightcoder/renderDecodeSlice.ts b/packages/store/ts/codegen/tightcoder/renderDecodeSlice.ts new file mode 100644 index 0000000000..11745263de --- /dev/null +++ b/packages/store/ts/codegen/tightcoder/renderDecodeSlice.ts @@ -0,0 +1,22 @@ +import { renderedSolidityHeader } from "@latticexyz/common/codegen"; +import { staticAbiTypeToByteLength, staticAbiTypes } from "@latticexyz/schema-type"; +import { renderTightCoderDecode } from "./renderFunctions"; + +export function renderDecodeSlice() { + let result = `${renderedSolidityHeader} + import { TightCoder } from "./TightCoder.sol"; + import { Slice } from "../Slice.sol"; + library DecodeSlice { + `; + + for (const staticAbiType of staticAbiTypes) { + const staticByteLength = staticAbiTypeToByteLength[staticAbiType]; + result += renderTightCoderDecode({ internalTypeId: staticAbiType, staticByteLength }); + } + + result += ` + } + `; + + return result; +} diff --git a/packages/store/ts/codegen/tightcoder/renderEncodeArray.ts b/packages/store/ts/codegen/tightcoder/renderEncodeArray.ts new file mode 100644 index 0000000000..5e9206ee47 --- /dev/null +++ b/packages/store/ts/codegen/tightcoder/renderEncodeArray.ts @@ -0,0 +1,21 @@ +import { renderedSolidityHeader } from "@latticexyz/common/codegen"; +import { staticAbiTypeToByteLength, staticAbiTypes } from "@latticexyz/schema-type"; +import { renderTightCoderEncode } from "./renderFunctions"; + +export function renderEncodeArray() { + let result = `${renderedSolidityHeader} + import { TightCoder } from "./TightCoder.sol"; + library EncodeArray { + `; + + for (const staticAbiType of staticAbiTypes) { + const staticByteLength = staticAbiTypeToByteLength[staticAbiType]; + result += renderTightCoderEncode({ internalTypeId: staticAbiType, staticByteLength }); + } + + result += ` + } + `; + + return result; +} diff --git a/packages/store/ts/codegen/tightcoder/renderFunctions.ts b/packages/store/ts/codegen/tightcoder/renderFunctions.ts new file mode 100644 index 0000000000..7e4f80df1c --- /dev/null +++ b/packages/store/ts/codegen/tightcoder/renderFunctions.ts @@ -0,0 +1,36 @@ +import { isLeftAligned, shiftLeftBits } from "@latticexyz/common/codegen"; + +export function renderTightCoderDecode(element: { internalTypeId: string; staticByteLength: number }) { + return ` + function decodeArray_${element.internalTypeId}( + Slice _input + ) internal pure returns ( + ${element.internalTypeId}[] memory _output + ) { + bytes32[] memory _genericArray = TightCoder.decode( + _input, + ${element.staticByteLength}, + ${isLeftAligned(element)} + ); + assembly { + _output := _genericArray + } + } + `.trim(); +} + +export function renderTightCoderEncode(element: { internalTypeId: string; staticByteLength: number }) { + return ` + function encode(${element.internalTypeId}[] memory _input) internal pure returns (bytes memory) { + bytes32[] memory _genericArray; + assembly { + _genericArray := _input + } + return TightCoder.encode( + _genericArray, + ${element.staticByteLength}, + ${shiftLeftBits(element)} + ); + } + `.trim(); +} diff --git a/packages/store/ts/codegen/tightcoder/renderTightCoderAutoTest.ts b/packages/store/ts/codegen/tightcoder/renderTightCoderAutoTest.ts new file mode 100644 index 0000000000..84f9cb9a86 --- /dev/null +++ b/packages/store/ts/codegen/tightcoder/renderTightCoderAutoTest.ts @@ -0,0 +1,47 @@ +import { renderedSolidityHeader } from "@latticexyz/common/codegen"; +import { staticAbiTypes } from "@latticexyz/schema-type"; + +export function renderTightCoderAutoTestFunction({ typeId }: { typeId: string }) { + return ` + function testEncodeDecodeArray_${typeId}( + ${typeId} val0, + ${typeId} val1, + ${typeId} val2 + ) public { + ${typeId}[] memory input = new ${typeId}[](3); + input[0] = val0; + input[1] = val1; + input[2] = val2; + + bytes memory encoded = EncodeArray.encode(input); + assertEq(encoded, abi.encodePacked(val0, val1, val2)); + + ${typeId}[] memory decoded = SliceLib.fromBytes(encoded).decodeArray_${typeId}(); + assertEq(decoded.length, 3); + assertEq(decoded[0], val0); + assertEq(decoded[1], val1); + assertEq(decoded[2], val2); + } + `.trim(); +} + +export function renderTightCoderAutoTest() { + let result = `${renderedSolidityHeader} + + import { Test } from "forge-std/Test.sol"; + import { EncodeArray } from "../../src/tightcoder/EncodeArray.sol"; + import { SliceLib } from "../../src/Slice.sol"; + + contract TightCoderAutoTest is Test { + `; + + for (const staticAbiType of staticAbiTypes) { + result += renderTightCoderAutoTestFunction({ typeId: staticAbiType }); + } + + result += ` + } + `; + + return result; +} diff --git a/packages/store/ts/scripts/generate-tightcoder.ts b/packages/store/ts/scripts/generate-tightcoder.ts index ec09fc3505..dd95ddb9bb 100644 --- a/packages/store/ts/scripts/generate-tightcoder.ts +++ b/packages/store/ts/scripts/generate-tightcoder.ts @@ -1,17 +1,10 @@ -import { renderFile } from "ejs"; -import { writeFileSync } from "fs"; -import { extname, basename, join, dirname } from "path"; +import { formatAndWriteSolidity } from "@latticexyz/common/codegen"; +import { renderDecodeSlice, renderEncodeArray, renderTightCoderAutoTest } from "../codegen/tightcoder"; -function renderEjsToSol(file: string, data: object) { - renderFile(file, data, {}, (err, str) => { - if (err) throw err; - const solFile = basename(file, extname(file)) + ".sol"; - const outFullPath = join(dirname(file), solFile); - writeFileSync(outFullPath, str); - console.log(`generated: ${outFullPath}`); - }); -} - -renderEjsToSol("src/tightcoder/DecodeSlice.ejs", {}); -renderEjsToSol("src/tightcoder/EncodeArray.ejs", {}); -renderEjsToSol("test/tightcoder/TightCoderAuto.t.ejs", {}); +await formatAndWriteSolidity(renderDecodeSlice(), "src/tightcoder/DecodeSlice.sol", "Generated DecodeSlice"); +await formatAndWriteSolidity(renderEncodeArray(), "src/tightcoder/EncodeArray.sol", "Generated EncodeArray"); +await formatAndWriteSolidity( + renderTightCoderAutoTest(), + "test/tightcoder/TightCoderAuto.t.sol", + "Generated TightCoderAutoTest" +); From 908bf9d274dcecd1c444023c8b2a612e370efd81 Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 08:28:48 +0300 Subject: [PATCH 03/12] cleanup --- packages/store/src/Bytes.sol | 2 -- packages/store/src/Memory.sol | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/store/src/Bytes.sol b/packages/store/src/Bytes.sol index 7fff4357db..8853b52fd4 100644 --- a/packages/store/src/Bytes.sol +++ b/packages/store/src/Bytes.sol @@ -1,8 +1,6 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.0; -import { SliceLib } from "./Slice.sol"; - library Bytes { /** * Converts a `bytes` memory blob to a single `bytes32` memory value, starting at the given byte offset. diff --git a/packages/store/src/Memory.sol b/packages/store/src/Memory.sol index 1856cc18a9..4872c9423a 100644 --- a/packages/store/src/Memory.sol +++ b/packages/store/src/Memory.sol @@ -29,6 +29,8 @@ library Memory { length -= 32; } } + if (length == 0) return; + // Copy the 0-31 length tail uint256 mask = leftMask(length); /// @solidity memory-safe-assembly From 97502405f25b705983f084437dbf0c37d5346887 Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 08:30:39 +0300 Subject: [PATCH 04/12] gas-report --- packages/store/gas-report.json | 56 +++++++++++++++------------------- packages/world/gas-report.json | 14 ++++----- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index 4f8c9d2578..7b3286ddca 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -75,13 +75,13 @@ "file": "test/Gas.t.sol", "test": "testCompareAbiEncodeVsCustom", "name": "custom encode", - "gasUsed": 3354 + "gasUsed": 2909 }, { "file": "test/Gas.t.sol", "test": "testCompareAbiEncodeVsCustom", "name": "custom decode", - "gasUsed": 2915 + "gasUsed": 2938 }, { "file": "test/Gas.t.sol", @@ -261,13 +261,13 @@ "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "set record in Mixed", - "gasUsed": 110779 + "gasUsed": 110334 }, { "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "get record from Mixed", - "gasUsed": 12438 + "gasUsed": 12461 }, { "file": "test/PackedCounter.t.sol", @@ -375,37 +375,37 @@ "file": "test/Slice.t.sol", "test": "testToBytes", "name": "Slice (0 bytes) to bytes memory", - "gasUsed": 476 + "gasUsed": 298 }, { "file": "test/Slice.t.sol", "test": "testToBytes", "name": "Slice (2 bytes) to bytes memory", - "gasUsed": 511 + "gasUsed": 534 }, { "file": "test/Slice.t.sol", "test": "testToBytes", "name": "Slice (32 bytes) to bytes memory", - "gasUsed": 724 + "gasUsed": 545 }, { "file": "test/Slice.t.sol", "test": "testToBytes", "name": "Slice (34 bytes) to bytes memory", - "gasUsed": 727 + "gasUsed": 750 }, { "file": "test/Slice.t.sol", "test": "testToBytes", "name": "Slice (1024 bytes) to bytes memory", - "gasUsed": 7443 + "gasUsed": 7264 }, { "file": "test/Slice.t.sol", "test": "testToBytes", "name": "Slice (1024x1024 bytes) to bytes memory", - "gasUsed": 9205372 + "gasUsed": 9205065 }, { "file": "test/Slice.t.sol", @@ -483,25 +483,25 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (cold, 1 slot, 1 uint32 item)", - "gasUsed": 30424 + "gasUsed": 30447 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (warm, 1 slot, 1 uint32 item)", - "gasUsed": 18479 + "gasUsed": 18502 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (cold, 2 slots, 10 uint32 items)", - "gasUsed": 32244 + "gasUsed": 32197 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (warm, 2 slots, 10 uint32 items)", - "gasUsed": 18299 + "gasUsed": 18252 }, { "file": "test/StoreCoreGas.t.sol", @@ -753,13 +753,13 @@ "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "update in field (1 slot, 1 uint32 item)", - "gasUsed": 15670 + "gasUsed": 15646 }, { "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "push to field (2 slots, 6 uint64 items)", - "gasUsed": 16641 + "gasUsed": 16687 }, { "file": "test/StoreMetadata.t.sol", @@ -771,7 +771,7 @@ "file": "test/StoreMetadata.t.sol", "test": "testSetAndGet", "name": "get record from StoreMetadataTable", - "gasUsed": 11430 + "gasUsed": 11402 }, { "file": "test/StoreSwitch.t.sol", @@ -789,7 +789,7 @@ "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "set field in Callbacks", - "gasUsed": 61975 + "gasUsed": 61696 }, { "file": "test/tables/Callbacks.t.sol", @@ -807,7 +807,7 @@ "file": "test/tables/Hooks.t.sol", "test": "testSetAndGet", "name": "set field in Hooks", - "gasUsed": 64132 + "gasUsed": 63687 }, { "file": "test/tables/Hooks.t.sol", @@ -833,46 +833,40 @@ "name": "decode packed bytes32[]", "gasUsed": 611 }, - { - "file": "test/tightcoder/EncodeArray.t.sol", - "test": "testEncodeBytesArray", - "name": "encode packed bytes[]", - "gasUsed": 1357 - }, { "file": "test/tightcoder/EncodeArray.t.sol", "test": "testEncodeUint16Array", "name": "encode packed uint16[]", - "gasUsed": 1143 + "gasUsed": 698 }, { "file": "test/tightcoder/EncodeArray.t.sol", "test": "testEncodeUint32Array", "name": "encode packed uint32[]", - "gasUsed": 1049 + "gasUsed": 604 }, { "file": "test/tightcoder/EncodeArray.t.sol", "test": "testEncodeUint8Array", "name": "encode packed uint8[]", - "gasUsed": 1038 + "gasUsed": 593 }, { "file": "test/tightcoder/TightCoder.t.sol", "test": "testFromAndToUint32Array", "name": "decode packed uint32[]", - "gasUsed": 788 + "gasUsed": 785 }, { "file": "test/tightcoder/TightCoder.t.sol", "test": "testToAndFromBytes24Array", "name": "encode packed bytes24[]", - "gasUsed": 886 + "gasUsed": 604 }, { "file": "test/tightcoder/TightCoder.t.sol", "test": "testToAndFromBytes24Array", - "name": "decode packed uint32[]", + "name": "decode packed bytes24[]", "gasUsed": 622 }, { diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index e7a68eba2d..1937f9704d 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -39,7 +39,7 @@ "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "delete a composite record on a table with keysInTableModule installed", - "gasUsed": 262682 + "gasUsed": 262259 }, { "file": "test/KeysInTableModule.t.sol", @@ -57,7 +57,7 @@ "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "delete a record on a table with keysInTableModule installed", - "gasUsed": 137742 + "gasUsed": 137601 }, { "file": "test/KeysWithValueModule.t.sol", @@ -99,7 +99,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "change a record on a table with KeysWithValueModule installed", - "gasUsed": 127327 + "gasUsed": 127047 }, { "file": "test/KeysWithValueModule.t.sol", @@ -297,24 +297,24 @@ "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (cold)", - "gasUsed": 38297 + "gasUsed": 38320 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (warm)", - "gasUsed": 21087 + "gasUsed": 21110 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (cold)", - "gasUsed": 40609 + "gasUsed": 40140 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (warm)", - "gasUsed": 23811 + "gasUsed": 23342 } ] From 0f27a0c8bf5775cbbccac94c16b7dcaf65798f24 Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 09:00:44 +0300 Subject: [PATCH 05/12] remove ejs files --- packages/store/src/tightcoder/DecodeSlice.ejs | 83 ---------------- packages/store/src/tightcoder/EncodeArray.ejs | 98 ------------------- .../test/tightcoder/TightCoderAuto.t.ejs | 44 --------- 3 files changed, 225 deletions(-) delete mode 100644 packages/store/src/tightcoder/DecodeSlice.ejs delete mode 100644 packages/store/src/tightcoder/EncodeArray.ejs delete mode 100644 packages/store/test/tightcoder/TightCoderAuto.t.ejs diff --git a/packages/store/src/tightcoder/DecodeSlice.ejs b/packages/store/src/tightcoder/DecodeSlice.ejs deleted file mode 100644 index dac751e1fc..0000000000 --- a/packages/store/src/tightcoder/DecodeSlice.ejs +++ /dev/null @@ -1,83 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -/* Autogenerated via `pnpm codegen`. Do not edit the `.sol` file manually. */ - -import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; -import { TightCoder } from "./TightCoder.sol"; -import { Slice } from "../Slice.sol"; - -library DecodeSlice { - /************************************************************************ - * - * uint8 - uint256 - * - ************************************************************************/ -<% for (let i = 8; i <= 256; i += 8) { -%> - - function decodeArray_uint<%= i %>(Slice self) internal pure returns (uint<%= i %>[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, <%= i / 8 %>, false); - assembly { - output := genericArray - } - } -<% } -%> - - /************************************************************************ - * - * int8 - int256 - * - ************************************************************************/ -<% for (let i = 8; i <= 256; i += 8) { -%> - - function decodeArray_int<%= i %>(Slice self) internal pure returns (int<%= i %>[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, <%= i / 8 %>, false); - assembly { - output := genericArray - } - } -<% } -%> - - /************************************************************************ - * - * bytes1 - bytes32 - * - ************************************************************************/ -<% for (let i = 1; i <= 32; i += 1) { -%> - - function decodeArray_bytes<%= i %>(Slice self) internal pure returns (bytes<%= i %>[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, <%= i %>, true); - assembly { - output := genericArray - } - } -<% } -%> - - /************************************************************************ - * - * Other types - * - ************************************************************************/ - - function decodeArray_address(Slice self) internal pure returns (address[] memory output) { - // Note: internally address is right-aligned, like uint160 - bytes32[] memory genericArray = TightCoder.decode(self, 20, false); - assembly { - output := genericArray - } - } - - function decodeArray_bool(Slice self) internal pure returns (bool[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 1, false); - assembly { - output := genericArray - } - } - - function decodeArray_SchemaType(Slice self) internal pure returns (SchemaType[] memory output) { - bytes32[] memory genericArray = TightCoder.decode(self, 1, false); - assembly { - output := genericArray - } - } -} diff --git a/packages/store/src/tightcoder/EncodeArray.ejs b/packages/store/src/tightcoder/EncodeArray.ejs deleted file mode 100644 index 7a4e56d7c2..0000000000 --- a/packages/store/src/tightcoder/EncodeArray.ejs +++ /dev/null @@ -1,98 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -/* Autogenerated via `pnpm codegen`. Do not edit the `.sol` file manually. */ - -import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; -import { TightCoder } from "./TightCoder.sol"; - -library EncodeArray { - /************************************************************************ - * - * uint8 - uint256 - * - ************************************************************************/ -<% for (let i = 8; i <= 256; i += 8) { -%> - - function encode(uint<%= i %>[] memory input) internal pure returns (bytes memory output) { - bytes32[] memory _genericArray; - assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, <%= i / 8 %>, false); - } -<% } -%> - - /************************************************************************ - * - * int8 - int256 - * - ************************************************************************/ -<% for (let i = 8; i <= 256; i += 8) { -%> - - function encode(int<%= i %>[] memory input) internal pure returns (bytes memory output) { - bytes32[] memory _genericArray; - assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, <%= i / 8 %>, false); - } -<% } -%> - - /************************************************************************ - * - * bytes1 - bytes32 - * - ************************************************************************/ -<% for (let i = 1; i <= 32; i += 1) { -%> - - function encode(bytes<%= i %>[] memory input) internal pure returns (bytes memory output) { - bytes32[] memory _genericArray; - assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, <%= i %>, true); - } -<% } -%> - - /************************************************************************ - * - * Other types - * - ************************************************************************/ - - function encode(address[] memory input) internal pure returns (bytes memory output) { - bytes32[] memory _genericArray; - assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, 20, false); - } - - function encode(bool[] memory input) internal pure returns (bytes memory output) { - bytes32[] memory _genericArray; - assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, 1, false); - } - - function encode(SchemaType[] memory input) internal pure returns (bytes memory output) { - bytes32[] memory _genericArray; - assembly { - _genericArray := input - } - return TightCoder.encode(_genericArray, 1, false); - } - - /** - * Converts a `bytes` memory array to a single `bytes` memory value. - * TODO: optimize gas cost - */ - function encode(bytes[] memory input) internal pure returns (bytes memory output) { - output = new bytes(0); - for (uint256 i; i < input.length; i++) { - output = bytes.concat(output, input[i]); - } - } -} diff --git a/packages/store/test/tightcoder/TightCoderAuto.t.ejs b/packages/store/test/tightcoder/TightCoderAuto.t.ejs deleted file mode 100644 index 621557fec1..0000000000 --- a/packages/store/test/tightcoder/TightCoderAuto.t.ejs +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.0; - -/* Autogenerated via `pnpm codegen`. Do not edit the `.sol` file manually. */ - -import "forge-std/Test.sol"; -import { Bytes } from "../../src/Bytes.sol"; -import { EncodeArray } from "../../src/tightcoder/EncodeArray.sol"; -import { SliceLib } from "../../src/Slice.sol"; - -contract TightCoderAutoTest is Test { -<% for (const prefix of ["uint", "int", "bytes"]) { -%> -<% const [start, end, step] = prefix === "bytes" ? [1, 32, 1] : [8, 256, 8]; -%> -<%= prefix === "uint" ? "" : "\n" -%> - /************************************************************************ - * - * <%= prefix %><%= start %> - <%= prefix %><%= end %> - * - ************************************************************************/ -<% for (let i = start; i <= end; i += step) { -%> -<% const T = `${prefix}${i}`; -%> - - function testEncodeDecodeArray_<%= `${prefix}${i}` %>( - <%= T %> val0, - <%= T %> val1, - <%= T %> val2 - ) public { - <%= T %>[] memory input = new <%= T %>[](3); - input[0] = val0; - input[1] = val1; - input[2] = val2; - - bytes memory encoded = EncodeArray.encode(input); - assertEq(encoded, abi.encodePacked(val0, val1, val2)); - - <%= T %>[] memory decoded = SliceLib.fromBytes(encoded).decodeArray_<%= T %>(); - assertEq(decoded.length, 3); - assertEq(decoded[0], val0); - assertEq(decoded[1], val1); - assertEq(decoded[2], val2); - } -<% } -%> -<% } -%> -} From a7dead275c96457fe209c0632ae6d4777e581477 Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 10:03:43 +0300 Subject: [PATCH 06/12] manually allocate memory in encode, update comments --- packages/store/gas-report.json | 22 +++++++------- packages/store/src/tightcoder/TightCoder.sol | 32 +++++++++++++++----- packages/world/gas-report.json | 6 ++-- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index 7b3286ddca..ad82313961 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -75,7 +75,7 @@ "file": "test/Gas.t.sol", "test": "testCompareAbiEncodeVsCustom", "name": "custom encode", - "gasUsed": 2909 + "gasUsed": 2806 }, { "file": "test/Gas.t.sol", @@ -261,7 +261,7 @@ "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "set record in Mixed", - "gasUsed": 110334 + "gasUsed": 110234 }, { "file": "test/Mixed.t.sol", @@ -597,7 +597,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "delete (dynamic) record on table with subscriber", - "gasUsed": 23977 + "gasUsed": 23976 }, { "file": "test/StoreCoreGas.t.sol", @@ -609,7 +609,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (2 slots, 10 uint32 items)", - "gasUsed": 38780 + "gasUsed": 38785 }, { "file": "test/StoreCoreGas.t.sol", @@ -705,7 +705,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "get dynamic field (1 slot, first dynamic field)", - "gasUsed": 3610 + "gasUsed": 3609 }, { "file": "test/StoreCoreGas.t.sol", @@ -789,7 +789,7 @@ "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "set field in Callbacks", - "gasUsed": 61696 + "gasUsed": 61596 }, { "file": "test/tables/Callbacks.t.sol", @@ -807,7 +807,7 @@ "file": "test/tables/Hooks.t.sol", "test": "testSetAndGet", "name": "set field in Hooks", - "gasUsed": 63687 + "gasUsed": 63587 }, { "file": "test/tables/Hooks.t.sol", @@ -837,19 +837,19 @@ "file": "test/tightcoder/EncodeArray.t.sol", "test": "testEncodeUint16Array", "name": "encode packed uint16[]", - "gasUsed": 698 + "gasUsed": 595 }, { "file": "test/tightcoder/EncodeArray.t.sol", "test": "testEncodeUint32Array", "name": "encode packed uint32[]", - "gasUsed": 604 + "gasUsed": 504 }, { "file": "test/tightcoder/EncodeArray.t.sol", "test": "testEncodeUint8Array", "name": "encode packed uint8[]", - "gasUsed": 593 + "gasUsed": 493 }, { "file": "test/tightcoder/TightCoder.t.sol", @@ -861,7 +861,7 @@ "file": "test/tightcoder/TightCoder.t.sol", "test": "testToAndFromBytes24Array", "name": "encode packed bytes24[]", - "gasUsed": 604 + "gasUsed": 501 }, { "file": "test/tightcoder/TightCoder.t.sol", diff --git a/packages/store/src/tightcoder/TightCoder.sol b/packages/store/src/tightcoder/TightCoder.sol index a2f4d91f81..738cbab90f 100644 --- a/packages/store/src/tightcoder/TightCoder.sol +++ b/packages/store/src/tightcoder/TightCoder.sol @@ -4,10 +4,16 @@ pragma solidity >=0.8.0; import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol"; import { Slice, SliceLib } from "../Slice.sol"; +/** + * Low-level generic implementation of tight encoding for arrays, used by codegen. + * This is the same as solidity's internal tight encoding for array data in storage. + */ library TightCoder { /** - * @dev Copies the array to a new bytes array, - * tightly packing it using the given size per element (in bytes) + * Copies the array to a new bytes array, tightly packing it. + * elementSize is in bytes, shiftLeftBits is in bits. + * elementSize and shiftLeftBits must be correctly provided by the caller based on the array's element type. + * @return data a tightly packed array */ function encode( bytes32[] memory array, @@ -16,15 +22,26 @@ library TightCoder { ) internal pure returns (bytes memory data) { uint256 arrayLength = array.length; uint256 packedLength = array.length * elementSize; - data = new bytes(packedLength); + // Manual memory allocation is cheaper and removes the issue of memory corruption at the tail /// @solidity memory-safe-assembly assembly { + // Solidity's YulUtilFunctions::roundUpFunction + function round_up_to_mul_of_32(value) -> _result { + _result := and(add(value, 31), not(31)) + } + + // Allocate memory + data := mload(0x40) + let toPointer := add(data, 0x20) + mstore(0x40, round_up_to_mul_of_32(add(toPointer, packedLength))) + // Store length + mstore(data, packedLength) + for { let i := 0 // Skip array length let fromPointer := add(array, 0x20) - let toPointer := add(data, 0x20) } lt(i, arrayLength) { // Loop until we reach the end of the array i := add(i, 1) @@ -33,14 +50,15 @@ library TightCoder { // Increment packed pointer by one element size toPointer := add(toPointer, elementSize) } { - mstore(toPointer, shl(shiftLeftBits, mload(fromPointer))) // pack one array element + // Pack one array element + mstore(toPointer, shl(shiftLeftBits, mload(fromPointer))) } } } /** - * @dev Unpacks the slice to a new memory location - * and lays it out like a memory array with the given size per element (in bytes) + * Unpacks the slice to a new memory location and lays it out like a memory array. + * elementSize is in bytes. * @return array a generic array, needs to be casted to the expected type using assembly */ function decode( diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 1937f9704d..7e43e749d2 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -99,7 +99,7 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "change a record on a table with KeysWithValueModule installed", - "gasUsed": 127047 + "gasUsed": 126950 }, { "file": "test/KeysWithValueModule.t.sol", @@ -309,12 +309,12 @@ "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (cold)", - "gasUsed": 40140 + "gasUsed": 40040 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (warm)", - "gasUsed": 23342 + "gasUsed": 23245 } ] From ad77b29111ce30de6b0dd8ae6279386253a0c85b Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 10:46:00 +0300 Subject: [PATCH 07/12] optimize decode --- packages/store/gas-report.json | 80 +++---- packages/store/src/tightcoder/DecodeSlice.sol | 196 +++++++++--------- packages/store/src/tightcoder/TightCoder.sol | 39 ++-- .../ts/codegen/tightcoder/renderFunctions.ts | 4 +- packages/world/gas-report.json | 94 ++++----- 5 files changed, 206 insertions(+), 207 deletions(-) diff --git a/packages/store/gas-report.json b/packages/store/gas-report.json index ad82313961..61e8ad15e9 100644 --- a/packages/store/gas-report.json +++ b/packages/store/gas-report.json @@ -81,7 +81,7 @@ "file": "test/Gas.t.sol", "test": "testCompareAbiEncodeVsCustom", "name": "custom decode", - "gasUsed": 2938 + "gasUsed": 2707 }, { "file": "test/Gas.t.sol", @@ -261,13 +261,13 @@ "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "set record in Mixed", - "gasUsed": 110234 + "gasUsed": 110021 }, { "file": "test/Mixed.t.sol", "test": "testSetAndGet", "name": "get record from Mixed", - "gasUsed": 12461 + "gasUsed": 12236 }, { "file": "test/PackedCounter.t.sol", @@ -483,25 +483,25 @@ "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (cold, 1 slot, 1 uint32 item)", - "gasUsed": 30447 + "gasUsed": 30234 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromSecondField", "name": "pop from field (warm, 1 slot, 1 uint32 item)", - "gasUsed": 18502 + "gasUsed": 18289 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (cold, 2 slots, 10 uint32 items)", - "gasUsed": 32197 + "gasUsed": 31984 }, { "file": "test/StoreCoreDynamic.t.sol", "test": "testPopFromThirdField", "name": "pop from field (warm, 2 slots, 10 uint32 items)", - "gasUsed": 18252 + "gasUsed": 18039 }, { "file": "test/StoreCoreGas.t.sol", @@ -537,7 +537,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testDeleteData", "name": "delete record (complex data, 3 slots)", - "gasUsed": 10213 + "gasUsed": 10000 }, { "file": "test/StoreCoreGas.t.sol", @@ -555,61 +555,61 @@ "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "register subscriber", - "gasUsed": 66065 + "gasUsed": 65852 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set record on table with subscriber", - "gasUsed": 73933 + "gasUsed": 73501 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "set static field on table with subscriber", - "gasUsed": 29860 + "gasUsed": 29436 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooks", "name": "delete record on table with subscriber", - "gasUsed": 22510 + "gasUsed": 22078 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "register subscriber", - "gasUsed": 66065 + "gasUsed": 65852 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) record on table with subscriber", - "gasUsed": 167262 + "gasUsed": 166830 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "set (dynamic) field on table with subscriber", - "gasUsed": 32942 + "gasUsed": 32510 }, { "file": "test/StoreCoreGas.t.sol", "test": "testHooksDynamicData", "name": "delete (dynamic) record on table with subscriber", - "gasUsed": 23976 + "gasUsed": 23544 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (1 slot, 1 uint32 item)", - "gasUsed": 16124 + "gasUsed": 15911 }, { "file": "test/StoreCoreGas.t.sol", "test": "testPushToField", "name": "push to field (2 slots, 10 uint32 items)", - "gasUsed": 38785 + "gasUsed": 38572 }, { "file": "test/StoreCoreGas.t.sol", @@ -633,7 +633,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetDynamicData", "name": "set complex record with dynamic data (4 slots)", - "gasUsed": 108831 + "gasUsed": 108618 }, { "file": "test/StoreCoreGas.t.sol", @@ -675,7 +675,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (1 slot)", - "gasUsed": 39144 + "gasUsed": 38931 }, { "file": "test/StoreCoreGas.t.sol", @@ -687,7 +687,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set static field (overlap 2 slot)", - "gasUsed": 34091 + "gasUsed": 33878 }, { "file": "test/StoreCoreGas.t.sol", @@ -699,7 +699,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, first dynamic field)", - "gasUsed": 56571 + "gasUsed": 56358 }, { "file": "test/StoreCoreGas.t.sol", @@ -711,7 +711,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetField", "name": "set dynamic field (1 slot, second dynamic field)", - "gasUsed": 34704 + "gasUsed": 34491 }, { "file": "test/StoreCoreGas.t.sol", @@ -723,7 +723,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticData", "name": "set static record (1 slot)", - "gasUsed": 38606 + "gasUsed": 38393 }, { "file": "test/StoreCoreGas.t.sol", @@ -735,7 +735,7 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetAndGetStaticDataSpanningWords", "name": "set static record (2 slots)", - "gasUsed": 61111 + "gasUsed": 60898 }, { "file": "test/StoreCoreGas.t.sol", @@ -747,25 +747,25 @@ "file": "test/StoreCoreGas.t.sol", "test": "testSetMetadata", "name": "StoreCore: set table metadata", - "gasUsed": 251951 + "gasUsed": 251738 }, { "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "update in field (1 slot, 1 uint32 item)", - "gasUsed": 15646 + "gasUsed": 15433 }, { "file": "test/StoreCoreGas.t.sol", "test": "testUpdateInField", "name": "push to field (2 slots, 6 uint64 items)", - "gasUsed": 16687 + "gasUsed": 16474 }, { "file": "test/StoreMetadata.t.sol", "test": "testSetAndGet", "name": "set record in StoreMetadataTable", - "gasUsed": 250424 + "gasUsed": 250211 }, { "file": "test/StoreMetadata.t.sol", @@ -789,49 +789,49 @@ "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "set field in Callbacks", - "gasUsed": 61596 + "gasUsed": 61383 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "get field from Callbacks (warm)", - "gasUsed": 5104 + "gasUsed": 5051 }, { "file": "test/tables/Callbacks.t.sol", "test": "testSetAndGet", "name": "push field to Callbacks", - "gasUsed": 39481 + "gasUsed": 39268 }, { "file": "test/tables/Hooks.t.sol", "test": "testSetAndGet", "name": "set field in Hooks", - "gasUsed": 63587 + "gasUsed": 63374 }, { "file": "test/tables/Hooks.t.sol", "test": "testSetAndGet", "name": "get field from Hooks (warm)", - "gasUsed": 5254 + "gasUsed": 5035 }, { "file": "test/tables/Hooks.t.sol", "test": "testSetAndGet", "name": "push field to Hooks", - "gasUsed": 39472 + "gasUsed": 39259 }, { "file": "test/tightcoder/DecodeSlice.t.sol", "test": "testToArrayUint32", "name": "decode packed uint32[]", - "gasUsed": 785 + "gasUsed": 563 }, { "file": "test/tightcoder/DecodeSlice.t.sol", "test": "testToBytes32Array", "name": "decode packed bytes32[]", - "gasUsed": 611 + "gasUsed": 552 }, { "file": "test/tightcoder/EncodeArray.t.sol", @@ -855,7 +855,7 @@ "file": "test/tightcoder/TightCoder.t.sol", "test": "testFromAndToUint32Array", "name": "decode packed uint32[]", - "gasUsed": 785 + "gasUsed": 563 }, { "file": "test/tightcoder/TightCoder.t.sol", @@ -867,7 +867,7 @@ "file": "test/tightcoder/TightCoder.t.sol", "test": "testToAndFromBytes24Array", "name": "decode packed bytes24[]", - "gasUsed": 622 + "gasUsed": 563 }, { "file": "test/Vector2.t.sol", @@ -879,7 +879,7 @@ "file": "test/Vector2.t.sol", "test": "testSetAndGet", "name": "set Vector2 record", - "gasUsed": 37425 + "gasUsed": 37212 }, { "file": "test/Vector2.t.sol", diff --git a/packages/store/src/tightcoder/DecodeSlice.sol b/packages/store/src/tightcoder/DecodeSlice.sol index 19d0e24010..d1a6348f8a 100644 --- a/packages/store/src/tightcoder/DecodeSlice.sol +++ b/packages/store/src/tightcoder/DecodeSlice.sol @@ -7,686 +7,686 @@ import { Slice } from "../Slice.sol"; library DecodeSlice { function decodeArray_uint8(Slice _input) internal pure returns (uint8[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 1, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, 248); assembly { _output := _genericArray } } function decodeArray_uint16(Slice _input) internal pure returns (uint16[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 2, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 2, 240); assembly { _output := _genericArray } } function decodeArray_uint24(Slice _input) internal pure returns (uint24[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 3, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 3, 232); assembly { _output := _genericArray } } function decodeArray_uint32(Slice _input) internal pure returns (uint32[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 4, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 4, 224); assembly { _output := _genericArray } } function decodeArray_uint40(Slice _input) internal pure returns (uint40[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 5, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 5, 216); assembly { _output := _genericArray } } function decodeArray_uint48(Slice _input) internal pure returns (uint48[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 6, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 6, 208); assembly { _output := _genericArray } } function decodeArray_uint56(Slice _input) internal pure returns (uint56[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 7, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 7, 200); assembly { _output := _genericArray } } function decodeArray_uint64(Slice _input) internal pure returns (uint64[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 8, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 8, 192); assembly { _output := _genericArray } } function decodeArray_uint72(Slice _input) internal pure returns (uint72[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 9, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 9, 184); assembly { _output := _genericArray } } function decodeArray_uint80(Slice _input) internal pure returns (uint80[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 10, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 10, 176); assembly { _output := _genericArray } } function decodeArray_uint88(Slice _input) internal pure returns (uint88[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 11, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 11, 168); assembly { _output := _genericArray } } function decodeArray_uint96(Slice _input) internal pure returns (uint96[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 12, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 12, 160); assembly { _output := _genericArray } } function decodeArray_uint104(Slice _input) internal pure returns (uint104[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 13, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 13, 152); assembly { _output := _genericArray } } function decodeArray_uint112(Slice _input) internal pure returns (uint112[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 14, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 14, 144); assembly { _output := _genericArray } } function decodeArray_uint120(Slice _input) internal pure returns (uint120[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 15, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 15, 136); assembly { _output := _genericArray } } function decodeArray_uint128(Slice _input) internal pure returns (uint128[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 16, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 16, 128); assembly { _output := _genericArray } } function decodeArray_uint136(Slice _input) internal pure returns (uint136[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 17, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 17, 120); assembly { _output := _genericArray } } function decodeArray_uint144(Slice _input) internal pure returns (uint144[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 18, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 18, 112); assembly { _output := _genericArray } } function decodeArray_uint152(Slice _input) internal pure returns (uint152[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 19, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 19, 104); assembly { _output := _genericArray } } function decodeArray_uint160(Slice _input) internal pure returns (uint160[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 20, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, 96); assembly { _output := _genericArray } } function decodeArray_uint168(Slice _input) internal pure returns (uint168[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 21, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 21, 88); assembly { _output := _genericArray } } function decodeArray_uint176(Slice _input) internal pure returns (uint176[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 22, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 22, 80); assembly { _output := _genericArray } } function decodeArray_uint184(Slice _input) internal pure returns (uint184[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 23, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 23, 72); assembly { _output := _genericArray } } function decodeArray_uint192(Slice _input) internal pure returns (uint192[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 24, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 24, 64); assembly { _output := _genericArray } } function decodeArray_uint200(Slice _input) internal pure returns (uint200[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 25, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 25, 56); assembly { _output := _genericArray } } function decodeArray_uint208(Slice _input) internal pure returns (uint208[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 26, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 26, 48); assembly { _output := _genericArray } } function decodeArray_uint216(Slice _input) internal pure returns (uint216[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 27, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 27, 40); assembly { _output := _genericArray } } function decodeArray_uint224(Slice _input) internal pure returns (uint224[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 28, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 28, 32); assembly { _output := _genericArray } } function decodeArray_uint232(Slice _input) internal pure returns (uint232[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 29, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 29, 24); assembly { _output := _genericArray } } function decodeArray_uint240(Slice _input) internal pure returns (uint240[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 30, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 30, 16); assembly { _output := _genericArray } } function decodeArray_uint248(Slice _input) internal pure returns (uint248[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 31, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 31, 8); assembly { _output := _genericArray } } function decodeArray_uint256(Slice _input) internal pure returns (uint256[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 32, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 32, 0); assembly { _output := _genericArray } } function decodeArray_int8(Slice _input) internal pure returns (int8[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 1, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, 248); assembly { _output := _genericArray } } function decodeArray_int16(Slice _input) internal pure returns (int16[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 2, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 2, 240); assembly { _output := _genericArray } } function decodeArray_int24(Slice _input) internal pure returns (int24[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 3, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 3, 232); assembly { _output := _genericArray } } function decodeArray_int32(Slice _input) internal pure returns (int32[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 4, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 4, 224); assembly { _output := _genericArray } } function decodeArray_int40(Slice _input) internal pure returns (int40[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 5, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 5, 216); assembly { _output := _genericArray } } function decodeArray_int48(Slice _input) internal pure returns (int48[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 6, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 6, 208); assembly { _output := _genericArray } } function decodeArray_int56(Slice _input) internal pure returns (int56[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 7, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 7, 200); assembly { _output := _genericArray } } function decodeArray_int64(Slice _input) internal pure returns (int64[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 8, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 8, 192); assembly { _output := _genericArray } } function decodeArray_int72(Slice _input) internal pure returns (int72[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 9, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 9, 184); assembly { _output := _genericArray } } function decodeArray_int80(Slice _input) internal pure returns (int80[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 10, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 10, 176); assembly { _output := _genericArray } } function decodeArray_int88(Slice _input) internal pure returns (int88[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 11, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 11, 168); assembly { _output := _genericArray } } function decodeArray_int96(Slice _input) internal pure returns (int96[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 12, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 12, 160); assembly { _output := _genericArray } } function decodeArray_int104(Slice _input) internal pure returns (int104[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 13, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 13, 152); assembly { _output := _genericArray } } function decodeArray_int112(Slice _input) internal pure returns (int112[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 14, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 14, 144); assembly { _output := _genericArray } } function decodeArray_int120(Slice _input) internal pure returns (int120[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 15, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 15, 136); assembly { _output := _genericArray } } function decodeArray_int128(Slice _input) internal pure returns (int128[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 16, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 16, 128); assembly { _output := _genericArray } } function decodeArray_int136(Slice _input) internal pure returns (int136[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 17, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 17, 120); assembly { _output := _genericArray } } function decodeArray_int144(Slice _input) internal pure returns (int144[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 18, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 18, 112); assembly { _output := _genericArray } } function decodeArray_int152(Slice _input) internal pure returns (int152[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 19, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 19, 104); assembly { _output := _genericArray } } function decodeArray_int160(Slice _input) internal pure returns (int160[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 20, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, 96); assembly { _output := _genericArray } } function decodeArray_int168(Slice _input) internal pure returns (int168[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 21, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 21, 88); assembly { _output := _genericArray } } function decodeArray_int176(Slice _input) internal pure returns (int176[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 22, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 22, 80); assembly { _output := _genericArray } } function decodeArray_int184(Slice _input) internal pure returns (int184[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 23, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 23, 72); assembly { _output := _genericArray } } function decodeArray_int192(Slice _input) internal pure returns (int192[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 24, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 24, 64); assembly { _output := _genericArray } } function decodeArray_int200(Slice _input) internal pure returns (int200[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 25, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 25, 56); assembly { _output := _genericArray } } function decodeArray_int208(Slice _input) internal pure returns (int208[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 26, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 26, 48); assembly { _output := _genericArray } } function decodeArray_int216(Slice _input) internal pure returns (int216[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 27, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 27, 40); assembly { _output := _genericArray } } function decodeArray_int224(Slice _input) internal pure returns (int224[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 28, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 28, 32); assembly { _output := _genericArray } } function decodeArray_int232(Slice _input) internal pure returns (int232[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 29, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 29, 24); assembly { _output := _genericArray } } function decodeArray_int240(Slice _input) internal pure returns (int240[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 30, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 30, 16); assembly { _output := _genericArray } } function decodeArray_int248(Slice _input) internal pure returns (int248[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 31, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 31, 8); assembly { _output := _genericArray } } function decodeArray_int256(Slice _input) internal pure returns (int256[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 32, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 32, 0); assembly { _output := _genericArray } } function decodeArray_bytes1(Slice _input) internal pure returns (bytes1[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 1, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, 0); assembly { _output := _genericArray } } function decodeArray_bytes2(Slice _input) internal pure returns (bytes2[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 2, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 2, 0); assembly { _output := _genericArray } } function decodeArray_bytes3(Slice _input) internal pure returns (bytes3[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 3, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 3, 0); assembly { _output := _genericArray } } function decodeArray_bytes4(Slice _input) internal pure returns (bytes4[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 4, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 4, 0); assembly { _output := _genericArray } } function decodeArray_bytes5(Slice _input) internal pure returns (bytes5[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 5, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 5, 0); assembly { _output := _genericArray } } function decodeArray_bytes6(Slice _input) internal pure returns (bytes6[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 6, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 6, 0); assembly { _output := _genericArray } } function decodeArray_bytes7(Slice _input) internal pure returns (bytes7[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 7, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 7, 0); assembly { _output := _genericArray } } function decodeArray_bytes8(Slice _input) internal pure returns (bytes8[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 8, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 8, 0); assembly { _output := _genericArray } } function decodeArray_bytes9(Slice _input) internal pure returns (bytes9[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 9, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 9, 0); assembly { _output := _genericArray } } function decodeArray_bytes10(Slice _input) internal pure returns (bytes10[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 10, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 10, 0); assembly { _output := _genericArray } } function decodeArray_bytes11(Slice _input) internal pure returns (bytes11[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 11, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 11, 0); assembly { _output := _genericArray } } function decodeArray_bytes12(Slice _input) internal pure returns (bytes12[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 12, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 12, 0); assembly { _output := _genericArray } } function decodeArray_bytes13(Slice _input) internal pure returns (bytes13[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 13, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 13, 0); assembly { _output := _genericArray } } function decodeArray_bytes14(Slice _input) internal pure returns (bytes14[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 14, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 14, 0); assembly { _output := _genericArray } } function decodeArray_bytes15(Slice _input) internal pure returns (bytes15[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 15, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 15, 0); assembly { _output := _genericArray } } function decodeArray_bytes16(Slice _input) internal pure returns (bytes16[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 16, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 16, 0); assembly { _output := _genericArray } } function decodeArray_bytes17(Slice _input) internal pure returns (bytes17[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 17, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 17, 0); assembly { _output := _genericArray } } function decodeArray_bytes18(Slice _input) internal pure returns (bytes18[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 18, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 18, 0); assembly { _output := _genericArray } } function decodeArray_bytes19(Slice _input) internal pure returns (bytes19[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 19, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 19, 0); assembly { _output := _genericArray } } function decodeArray_bytes20(Slice _input) internal pure returns (bytes20[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 20, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, 0); assembly { _output := _genericArray } } function decodeArray_bytes21(Slice _input) internal pure returns (bytes21[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 21, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 21, 0); assembly { _output := _genericArray } } function decodeArray_bytes22(Slice _input) internal pure returns (bytes22[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 22, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 22, 0); assembly { _output := _genericArray } } function decodeArray_bytes23(Slice _input) internal pure returns (bytes23[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 23, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 23, 0); assembly { _output := _genericArray } } function decodeArray_bytes24(Slice _input) internal pure returns (bytes24[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 24, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 24, 0); assembly { _output := _genericArray } } function decodeArray_bytes25(Slice _input) internal pure returns (bytes25[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 25, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 25, 0); assembly { _output := _genericArray } } function decodeArray_bytes26(Slice _input) internal pure returns (bytes26[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 26, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 26, 0); assembly { _output := _genericArray } } function decodeArray_bytes27(Slice _input) internal pure returns (bytes27[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 27, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 27, 0); assembly { _output := _genericArray } } function decodeArray_bytes28(Slice _input) internal pure returns (bytes28[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 28, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 28, 0); assembly { _output := _genericArray } } function decodeArray_bytes29(Slice _input) internal pure returns (bytes29[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 29, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 29, 0); assembly { _output := _genericArray } } function decodeArray_bytes30(Slice _input) internal pure returns (bytes30[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 30, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 30, 0); assembly { _output := _genericArray } } function decodeArray_bytes31(Slice _input) internal pure returns (bytes31[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 31, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 31, 0); assembly { _output := _genericArray } } function decodeArray_bytes32(Slice _input) internal pure returns (bytes32[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 32, true); + bytes32[] memory _genericArray = TightCoder.decode(_input, 32, 0); assembly { _output := _genericArray } } function decodeArray_bool(Slice _input) internal pure returns (bool[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 1, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 1, 248); assembly { _output := _genericArray } } function decodeArray_address(Slice _input) internal pure returns (address[] memory _output) { - bytes32[] memory _genericArray = TightCoder.decode(_input, 20, false); + bytes32[] memory _genericArray = TightCoder.decode(_input, 20, 96); assembly { _output := _genericArray } diff --git a/packages/store/src/tightcoder/TightCoder.sol b/packages/store/src/tightcoder/TightCoder.sol index 738cbab90f..277b93210e 100644 --- a/packages/store/src/tightcoder/TightCoder.sol +++ b/packages/store/src/tightcoder/TightCoder.sol @@ -33,42 +33,42 @@ library TightCoder { // Allocate memory data := mload(0x40) - let toPointer := add(data, 0x20) - mstore(0x40, round_up_to_mul_of_32(add(toPointer, packedLength))) + let packedPointer := add(data, 0x20) + mstore(0x40, round_up_to_mul_of_32(add(packedPointer, packedLength))) // Store length mstore(data, packedLength) for { let i := 0 // Skip array length - let fromPointer := add(array, 0x20) + let arrayPointer := add(array, 0x20) } lt(i, arrayLength) { // Loop until we reach the end of the array i := add(i, 1) // Increment array pointer by one word - fromPointer := add(fromPointer, 0x20) + arrayPointer := add(arrayPointer, 0x20) // Increment packed pointer by one element size - toPointer := add(toPointer, elementSize) + packedPointer := add(packedPointer, elementSize) } { // Pack one array element - mstore(toPointer, shl(shiftLeftBits, mload(fromPointer))) + mstore(packedPointer, shl(shiftLeftBits, mload(arrayPointer))) } } } /** * Unpacks the slice to a new memory location and lays it out like a memory array. - * elementSize is in bytes. + * elementSize is in bytes, unshiftLeftBits is in bits. + * elementSize and unshiftLeftBits must be correctly provided by the caller based on the array's element type. * @return array a generic array, needs to be casted to the expected type using assembly */ function decode( Slice packedSlice, uint256 elementSize, - bool leftAligned + uint256 unshiftLeftBits ) internal pure returns (bytes32[] memory array) { uint256 packedPointer = packedSlice.pointer(); uint256 packedLength = packedSlice.length(); - uint256 padLeft = leftAligned ? 0 : 256 - elementSize * 8; // Array length (number of elements) uint256 arrayLength; unchecked { @@ -83,26 +83,25 @@ library TightCoder { /// @solidity memory-safe-assembly assembly { - // Allocate a word for each element, and a word for the array's length - let allocateBytes := add(mul(arrayLength, 32), 0x20) - // Allocate memory and update the free memory pointer + // Allocate memory array := mload(0x40) - mstore(0x40, add(array, allocateBytes)) - - // Store array length + let arrayPointer := add(array, 0x20) + mstore(0x40, add(arrayPointer, mul(arrayLength, 32))) + // Store length mstore(array, arrayLength) for { let i := 0 - let arrayCursor := add(array, 0x20) // skip array length - let packedCursor := packedPointer } lt(i, arrayLength) { // Loop until we reach the end of the array i := add(i, 1) - arrayCursor := add(arrayCursor, 0x20) // increment array pointer by one word - packedCursor := add(packedCursor, elementSize) // increment packed pointer by one element size + // Increment array pointer by one word + arrayPointer := add(arrayPointer, 0x20) + // Increment packed pointer by one element size + packedPointer := add(packedPointer, elementSize) } { - mstore(arrayCursor, shr(padLeft, mload(packedCursor))) // unpack one array element + // Unpack one array element + mstore(arrayPointer, shr(unshiftLeftBits, mload(packedPointer))) } } } diff --git a/packages/store/ts/codegen/tightcoder/renderFunctions.ts b/packages/store/ts/codegen/tightcoder/renderFunctions.ts index 7e4f80df1c..f7dd73497c 100644 --- a/packages/store/ts/codegen/tightcoder/renderFunctions.ts +++ b/packages/store/ts/codegen/tightcoder/renderFunctions.ts @@ -1,4 +1,4 @@ -import { isLeftAligned, shiftLeftBits } from "@latticexyz/common/codegen"; +import { shiftLeftBits } from "@latticexyz/common/codegen"; export function renderTightCoderDecode(element: { internalTypeId: string; staticByteLength: number }) { return ` @@ -10,7 +10,7 @@ export function renderTightCoderDecode(element: { internalTypeId: string; static bytes32[] memory _genericArray = TightCoder.decode( _input, ${element.staticByteLength}, - ${isLeftAligned(element)} + ${shiftLeftBits(element)} ); assembly { _output := _genericArray diff --git a/packages/world/gas-report.json b/packages/world/gas-report.json index 7e43e749d2..86f26ac32d 100644 --- a/packages/world/gas-report.json +++ b/packages/world/gas-report.json @@ -3,73 +3,73 @@ "file": "test/KeysInTableModule.t.sol", "test": "testInstallComposite", "name": "install keys in table module", - "gasUsed": 1248704 + "gasUsed": 1245509 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "install keys in table module", - "gasUsed": 1248704 + "gasUsed": 1245509 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallGas", "name": "set a record on a table with keysInTableModule installed", - "gasUsed": 187021 + "gasUsed": 186376 }, { "file": "test/KeysInTableModule.t.sol", "test": "testInstallSingleton", "name": "install keys in table module", - "gasUsed": 1248704 + "gasUsed": 1245509 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "install keys in table module", - "gasUsed": 1248704 + "gasUsed": 1245509 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "change a composite record on a table with keysInTableModule installed", - "gasUsed": 27801 + "gasUsed": 27582 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookCompositeGas", "name": "delete a composite record on a table with keysInTableModule installed", - "gasUsed": 262259 + "gasUsed": 260336 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "install keys in table module", - "gasUsed": 1248704 + "gasUsed": 1245509 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "change a record on a table with keysInTableModule installed", - "gasUsed": 26415 + "gasUsed": 26196 }, { "file": "test/KeysInTableModule.t.sol", "test": "testSetAndDeleteRecordHookGas", "name": "delete a record on a table with keysInTableModule installed", - "gasUsed": 137601 + "gasUsed": 136530 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "install keys with value module", - "gasUsed": 602496 + "gasUsed": 600153 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testGetKeysWithValueGas", "name": "Get list of keys with a given value", - "gasUsed": 7444 + "gasUsed": 7391 }, { "file": "test/KeysWithValueModule.t.sol", @@ -81,61 +81,61 @@ "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "install keys with value module", - "gasUsed": 602496 + "gasUsed": 600153 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testInstall", "name": "set a record on a table with KeysWithValueModule installed", - "gasUsed": 159927 + "gasUsed": 159235 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "install keys with value module", - "gasUsed": 602496 + "gasUsed": 600153 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "change a record on a table with KeysWithValueModule installed", - "gasUsed": 126950 + "gasUsed": 126246 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetAndDeleteRecordHook", "name": "delete a record on a table with KeysWithValueModule installed", - "gasUsed": 49418 + "gasUsed": 48933 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "install keys with value module", - "gasUsed": 602496 + "gasUsed": 600153 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "set a field on a table with KeysWithValueModule installed", - "gasUsed": 168075 + "gasUsed": 167383 }, { "file": "test/KeysWithValueModule.t.sol", "test": "testSetField", "name": "change a field on a table with KeysWithValueModule installed", - "gasUsed": 130339 + "gasUsed": 129641 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueNotQuery", "name": "CombinedHasHasValueNotQuery", - "gasUsed": 160604 + "gasUsed": 160409 }, { "file": "test/query.t.sol", "test": "testCombinedHasHasValueQuery", "name": "CombinedHasHasValueQuery", - "gasUsed": 75381 + "gasUsed": 75186 }, { "file": "test/query.t.sol", @@ -153,13 +153,13 @@ "file": "test/query.t.sol", "test": "testCombinedHasValueNotQuery", "name": "CombinedHasValueNotQuery", - "gasUsed": 137295 + "gasUsed": 137230 }, { "file": "test/query.t.sol", "test": "testCombinedHasValueQuery", "name": "CombinedHasValueQuery", - "gasUsed": 19014 + "gasUsed": 18902 }, { "file": "test/query.t.sol", @@ -183,138 +183,138 @@ "file": "test/query.t.sol", "test": "testHasValueQuery", "name": "HasValueQuery", - "gasUsed": 9187 + "gasUsed": 9128 }, { "file": "test/query.t.sol", "test": "testNotValueQuery", "name": "NotValueQuery", - "gasUsed": 68939 + "gasUsed": 68780 }, { "file": "test/SnapSyncModule.t.sol", "test": "testSnapSyncGas", "name": "Call snap sync on a table with 1 record", - "gasUsed": 39143 + "gasUsed": 38930 }, { "file": "test/SnapSyncModule.t.sol", "test": "testSnapSyncGas", "name": "Call snap sync on a table with 2 records", - "gasUsed": 56220 + "gasUsed": 56007 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "install unique entity module", - "gasUsed": 791266 + "gasUsed": 787858 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstall", "name": "get a unique entity nonce (non-root module)", - "gasUsed": 70073 + "gasUsed": 69647 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "installRoot unique entity module", - "gasUsed": 763358 + "gasUsed": 760163 }, { "file": "test/UniqueEntityModule.t.sol", "test": "testInstallRoot", "name": "get a unique entity nonce (root module)", - "gasUsed": 70073 + "gasUsed": 69647 }, { "file": "test/World.t.sol", "test": "testDeleteRecord", "name": "Delete record", - "gasUsed": 14659 + "gasUsed": 14446 }, { "file": "test/World.t.sol", "test": "testPushToField", "name": "Push data to the table", - "gasUsed": 94777 + "gasUsed": 94564 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a fallback system", - "gasUsed": 80591 + "gasUsed": 80165 }, { "file": "test/World.t.sol", "test": "testRegisterFallbackSystem", "name": "Register a root fallback system", - "gasUsed": 71771 + "gasUsed": 71345 }, { "file": "test/World.t.sol", "test": "testRegisterFunctionSelector", "name": "Register a function selector", - "gasUsed": 101182 + "gasUsed": 100756 }, { "file": "test/World.t.sol", "test": "testRegisterNamespace", "name": "Register a new namespace", - "gasUsed": 156426 + "gasUsed": 155574 }, { "file": "test/World.t.sol", "test": "testRegisterRootFunctionSelector", "name": "Register a root function selector", - "gasUsed": 87674 + "gasUsed": 87248 }, { "file": "test/World.t.sol", "test": "testRegisterTable", "name": "Register a new table in the namespace", - "gasUsed": 253992 + "gasUsed": 252927 }, { "file": "test/World.t.sol", "test": "testSetField", "name": "Write data to a table field", - "gasUsed": 43248 + "gasUsed": 43035 }, { "file": "test/World.t.sol", "test": "testSetMetadata", "name": "Set metadata", - "gasUsed": 278225 + "gasUsed": 277799 }, { "file": "test/World.t.sol", "test": "testSetRecord", "name": "Write data to the table", - "gasUsed": 41103 + "gasUsed": 40890 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (cold)", - "gasUsed": 38320 + "gasUsed": 38107 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testPopFromField", "name": "pop 1 address (warm)", - "gasUsed": 21110 + "gasUsed": 20897 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (cold)", - "gasUsed": 40040 + "gasUsed": 39827 }, { "file": "test/WorldDynamicUpdate.t.sol", "test": "testUpdateInField", "name": "updateInField 1 item (warm)", - "gasUsed": 23245 + "gasUsed": 23032 } ] From 8a27d2dbd1192b5679d6e538605df7b4d483ebd7 Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 12:04:41 +0300 Subject: [PATCH 08/12] Create witty-jokes-serve.md --- .changeset/witty-jokes-serve.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .changeset/witty-jokes-serve.md diff --git a/.changeset/witty-jokes-serve.md b/.changeset/witty-jokes-serve.md new file mode 100644 index 0000000000..6602f4827a --- /dev/null +++ b/.changeset/witty-jokes-serve.md @@ -0,0 +1,9 @@ +--- +"@latticexyz/common": patch +"@latticexyz/store": patch +"@latticexyz/world": patch +--- + +Refactor tightcoder to use typescript functions instead of ejs +Optimize `TightCoder` library +Add `isLeftAligned` and `shiftLeftBits` common codegen helpers From c4ad0af5fcb756ed6c7b366a90c98cc978d87bac Mon Sep 17 00:00:00 2001 From: dk1a Date: Mon, 31 Jul 2023 12:24:52 +0300 Subject: [PATCH 09/12] update codegen --- packages/store/ts/codegen/field.ts | 8 ++++---- packages/store/ts/codegen/index.ts | 1 + packages/store/ts/codegen/renderTable.ts | 6 ++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/store/ts/codegen/field.ts b/packages/store/ts/codegen/field.ts index 51952e8a9f..24b8f94161 100644 --- a/packages/store/ts/codegen/field.ts +++ b/packages/store/ts/codegen/field.ts @@ -42,7 +42,7 @@ export function renderFieldMethods(options: RenderTableOptions) { _typedFieldName, ])}) internal { ${_keyTupleDefinition} - ${_store}.setField(_tableId, _keyTuple, ${schemaIndex}, ${renderEncodeField(field)}); + ${_store}.setField(_tableId, _keyTuple, ${schemaIndex}, ${renderEncodeFieldSingle(field)}); } ` ); @@ -148,7 +148,7 @@ export function renderFieldMethods(options: RenderTableOptions) { return result; } -export function renderEncodeField(field: RenderField) { +export function renderEncodeFieldSingle(field: RenderField) { let func; if (field.arrayElement) { func = "EncodeArray.encode"; @@ -190,7 +190,7 @@ function fieldPortionData(field: RenderField) { return { typeWithLocation: field.arrayElement.typeWithLocation, name: "_element", - encoded: renderEncodeField(elementFieldData), + encoded: renderEncodeFieldSingle(elementFieldData), decoded: renderDecodeFieldSingle(elementFieldData), title: "an element", elementLength: field.arrayElement.staticByteLength, @@ -201,7 +201,7 @@ function fieldPortionData(field: RenderField) { return { typeWithLocation: `${field.typeId} memory`, name, - encoded: renderEncodeField(elementFieldData), + encoded: renderEncodeFieldSingle(elementFieldData), decoded: renderDecodeFieldSingle(elementFieldData), title: "a slice", elementLength: 1, diff --git a/packages/store/ts/codegen/index.ts b/packages/store/ts/codegen/index.ts index 3915c4f1db..d5dfbf0f96 100644 --- a/packages/store/ts/codegen/index.ts +++ b/packages/store/ts/codegen/index.ts @@ -4,5 +4,6 @@ export * from "./renderTable"; export * from "./renderTypesFromConfig"; export * from "./tablegen"; export * from "./tableOptions"; +export * from "./tightcoder"; export * from "./types"; export * from "./userType"; diff --git a/packages/store/ts/codegen/renderTable.ts b/packages/store/ts/codegen/renderTable.ts index 7d4897cb19..d2d6c73ab9 100644 --- a/packages/store/ts/codegen/renderTable.ts +++ b/packages/store/ts/codegen/renderTable.ts @@ -11,7 +11,7 @@ import { RenderDynamicField, } from "@latticexyz/common/codegen"; import { renderEphemeralMethods } from "./ephemeral"; -import { renderEncodeField, renderFieldMethods } from "./field"; +import { renderEncodeFieldSingle, renderFieldMethods } from "./field"; import { renderRecordMethods } from "./record"; import { RenderTableOptions } from "./types"; @@ -128,11 +128,9 @@ library ${libraryName} { ${renderEncodedLengths(dynamicFields)} return abi.encodePacked(${renderArguments([ renderArguments(staticFields.map(({ name }) => name)), - // TODO try gas optimization (preallocate for all, encodePacked statics, and direct encode dynamics) - // (see https://github.com/latticexyz/mud/issues/444) ...(dynamicFields.length === 0 ? [] - : ["_encodedLengths.unwrap()", renderArguments(dynamicFields.map((field) => renderEncodeField(field)))]), + : ["_encodedLengths.unwrap()", renderArguments(dynamicFields.map((field) => renderEncodeFieldSingle(field)))]), ])}); } From 9559957377dc80c8d3b977bc352666068dfa29a7 Mon Sep 17 00:00:00 2001 From: dk1a Date: Thu, 3 Aug 2023 20:17:47 +0300 Subject: [PATCH 10/12] Update .changeset/witty-jokes-serve.md Co-authored-by: alvarius --- .changeset/witty-jokes-serve.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.changeset/witty-jokes-serve.md b/.changeset/witty-jokes-serve.md index 6602f4827a..af5bd95628 100644 --- a/.changeset/witty-jokes-serve.md +++ b/.changeset/witty-jokes-serve.md @@ -4,6 +4,6 @@ "@latticexyz/world": patch --- -Refactor tightcoder to use typescript functions instead of ejs -Optimize `TightCoder` library -Add `isLeftAligned` and `shiftLeftBits` common codegen helpers +- Refactor tightcoder to use typescript functions instead of ejs +- Optimize `TightCoder` library +- Add `isLeftAligned` and `shiftLeftBits` common codegen helpers From 666e2d024a6aeaa0f31589eaf52763e8c4dd5f01 Mon Sep 17 00:00:00 2001 From: dk1a Date: Thu, 3 Aug 2023 20:37:44 +0300 Subject: [PATCH 11/12] rename shiftLeftBits to getLeftPaddingBits --- .changeset/witty-jokes-serve.md | 2 +- .../common/src/codegen/render-solidity/common.ts | 2 +- packages/store/src/tightcoder/TightCoder.sol | 16 ++++++++-------- .../ts/codegen/tightcoder/renderFunctions.ts | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.changeset/witty-jokes-serve.md b/.changeset/witty-jokes-serve.md index af5bd95628..e37e695d91 100644 --- a/.changeset/witty-jokes-serve.md +++ b/.changeset/witty-jokes-serve.md @@ -6,4 +6,4 @@ - Refactor tightcoder to use typescript functions instead of ejs - Optimize `TightCoder` library -- Add `isLeftAligned` and `shiftLeftBits` common codegen helpers +- Add `isLeftAligned` and `getLeftPaddingBits` common codegen helpers diff --git a/packages/common/src/codegen/render-solidity/common.ts b/packages/common/src/codegen/render-solidity/common.ts index 54399134f2..a853ff8513 100644 --- a/packages/common/src/codegen/render-solidity/common.ts +++ b/packages/common/src/codegen/render-solidity/common.ts @@ -184,7 +184,7 @@ export function isLeftAligned(field: Pick): boolea return field.internalTypeId.match(/^bytes\d{1,2}$/) !== null; } -export function shiftLeftBits(field: Pick): number { +export function getLeftPaddingBits(field: Pick): number { if (isLeftAligned(field)) { return 0; } else { diff --git a/packages/store/src/tightcoder/TightCoder.sol b/packages/store/src/tightcoder/TightCoder.sol index 277b93210e..6274f26f0d 100644 --- a/packages/store/src/tightcoder/TightCoder.sol +++ b/packages/store/src/tightcoder/TightCoder.sol @@ -11,14 +11,14 @@ import { Slice, SliceLib } from "../Slice.sol"; library TightCoder { /** * Copies the array to a new bytes array, tightly packing it. - * elementSize is in bytes, shiftLeftBits is in bits. - * elementSize and shiftLeftBits must be correctly provided by the caller based on the array's element type. + * elementSize is in bytes, leftPaddingBits is in bits. + * elementSize and leftPaddingBits must be correctly provided by the caller based on the array's element type. * @return data a tightly packed array */ function encode( bytes32[] memory array, uint256 elementSize, - uint256 shiftLeftBits + uint256 leftPaddingBits ) internal pure returns (bytes memory data) { uint256 arrayLength = array.length; uint256 packedLength = array.length * elementSize; @@ -51,21 +51,21 @@ library TightCoder { packedPointer := add(packedPointer, elementSize) } { // Pack one array element - mstore(packedPointer, shl(shiftLeftBits, mload(arrayPointer))) + mstore(packedPointer, shl(leftPaddingBits, mload(arrayPointer))) } } } /** * Unpacks the slice to a new memory location and lays it out like a memory array. - * elementSize is in bytes, unshiftLeftBits is in bits. - * elementSize and unshiftLeftBits must be correctly provided by the caller based on the array's element type. + * elementSize is in bytes, leftPaddingBits is in bits. + * elementSize and leftPaddingBits must be correctly provided by the caller based on the array's element type. * @return array a generic array, needs to be casted to the expected type using assembly */ function decode( Slice packedSlice, uint256 elementSize, - uint256 unshiftLeftBits + uint256 leftPaddingBits ) internal pure returns (bytes32[] memory array) { uint256 packedPointer = packedSlice.pointer(); uint256 packedLength = packedSlice.length(); @@ -101,7 +101,7 @@ library TightCoder { packedPointer := add(packedPointer, elementSize) } { // Unpack one array element - mstore(arrayPointer, shr(unshiftLeftBits, mload(packedPointer))) + mstore(arrayPointer, shr(leftPaddingBits, mload(packedPointer))) } } } diff --git a/packages/store/ts/codegen/tightcoder/renderFunctions.ts b/packages/store/ts/codegen/tightcoder/renderFunctions.ts index f7dd73497c..d8065f2d93 100644 --- a/packages/store/ts/codegen/tightcoder/renderFunctions.ts +++ b/packages/store/ts/codegen/tightcoder/renderFunctions.ts @@ -1,4 +1,4 @@ -import { shiftLeftBits } from "@latticexyz/common/codegen"; +import { getLeftPaddingBits } from "@latticexyz/common/codegen"; export function renderTightCoderDecode(element: { internalTypeId: string; staticByteLength: number }) { return ` @@ -10,7 +10,7 @@ export function renderTightCoderDecode(element: { internalTypeId: string; static bytes32[] memory _genericArray = TightCoder.decode( _input, ${element.staticByteLength}, - ${shiftLeftBits(element)} + ${getLeftPaddingBits(element)} ); assembly { _output := _genericArray @@ -29,7 +29,7 @@ export function renderTightCoderEncode(element: { internalTypeId: string; static return TightCoder.encode( _genericArray, ${element.staticByteLength}, - ${shiftLeftBits(element)} + ${getLeftPaddingBits(element)} ); } `.trim(); From f09aed73a431131dbe4f32cfa70d51459d2eef40 Mon Sep 17 00:00:00 2001 From: dk1a Date: Sun, 6 Aug 2023 09:13:21 +0300 Subject: [PATCH 12/12] rename generate-tightcoder script to build:tightcoder --- packages/store/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/store/package.json b/packages/store/package.json index d7014f0f03..5110b51940 100644 --- a/packages/store/package.json +++ b/packages/store/package.json @@ -34,10 +34,11 @@ } }, "scripts": { - "build": "pnpm run generate-tightcoder && pnpm run build:mud && pnpm run build:abi && pnpm run build:typechain && pnpm run build:js", + "build": "pnpm run build:tightcoder && pnpm run build:mud && pnpm run build:abi && pnpm run build:typechain && pnpm run build:js", "build:abi": "forge build --extra-output-files abi --out abi --skip test script MudTest.sol", "build:js": "tsup", "build:mud": "tsx ./ts/scripts/tablegen.ts", + "build:tightcoder": "tsx ./ts/scripts/generate-tightcoder.ts", "build:typechain": "typechain --target ethers-v5 'abi/**/*.sol/!(*.abi).json'", "clean": "pnpm run clean:abi && pnpm run clean:js && pnpm run clean:mud && pnpm run clean:typechain", "clean:abi": "rimraf abi", @@ -46,7 +47,6 @@ "clean:typechain": "rimraf types", "dev": "tsup --watch", "gas-report": "mud-gas-report --save gas-report.json", - "generate-tightcoder": "tsx ./ts/scripts/generate-tightcoder.ts", "lint": "solhint --config ./.solhint.json 'src/**/*.sol'", "test": "vitest typecheck --run && vitest --run --passWithNoTests && forge test" },