Skip to content

Commit

Permalink
refactor(store): clean up Memory, make mcopy pure (#1153)
Browse files Browse the repository at this point in the history
* refactor(store): clean up Memory, make mcopy pure

* update comments

* Create nice-pandas-knock.md
  • Loading branch information
dk1a authored Jul 13, 2023
1 parent 0d43481 commit 8d51a03
Show file tree
Hide file tree
Showing 45 changed files with 114 additions and 123 deletions.
8 changes: 8 additions & 0 deletions .changeset/nice-pandas-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@latticexyz/cli": patch
"@latticexyz/common": patch
"@latticexyz/store": patch
"@latticexyz/world": patch
---

Clean up Memory.sol, make mcopy pure
2 changes: 1 addition & 1 deletion e2e/packages/contracts/src/codegen/tables/Multi.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ library Multi {
}

/** Tightly pack full data using this table's schema */
function encode(int256 num, bool value) internal view returns (bytes memory) {
function encode(int256 num, bool value) internal pure returns (bytes memory) {
return abi.encodePacked(num, value);
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/packages/contracts/src/codegen/tables/Number.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ library Number {
}

/** Tightly pack full data using this table's schema */
function encode(uint32 value) internal view returns (bytes memory) {
function encode(uint32 value) internal pure returns (bytes memory) {
return abi.encodePacked(value);
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/packages/contracts/src/codegen/tables/NumberList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ library NumberList {
}

/** Tightly pack full data using this table's schema */
function encode(uint32[] memory value) internal view returns (bytes memory) {
function encode(uint32[] memory value) internal pure returns (bytes memory) {
uint40[] memory _counters = new uint40[](1);
_counters[0] = uint40(value.length * 4);
PackedCounter _encodedLengths = PackedCounterLib.pack(_counters);
Expand Down
2 changes: 1 addition & 1 deletion e2e/packages/contracts/src/codegen/tables/Vector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ library Vector {
}

/** Tightly pack full data using this table's schema */
function encode(int32 x, int32 y) internal view returns (bytes memory) {
function encode(int32 x, int32 y) internal pure returns (bytes memory) {
return abi.encodePacked(x, y);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ library CounterTable {
}

/** Tightly pack full data using this table's schema */
function encode(uint32 value) internal view returns (bytes memory) {
function encode(uint32 value) internal pure returns (bytes memory) {
return abi.encodePacked(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ library Inventory {
}

/** Tightly pack full data using this table's schema */
function encode(uint32 amount) internal view returns (bytes memory) {
function encode(uint32 amount) internal pure returns (bytes memory) {
return abi.encodePacked(amount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ library MessageTable {
}

/** Tightly pack full data using this table's schema */
function encode(string memory value) internal view returns (bytes memory) {
function encode(string memory value) internal pure returns (bytes memory) {
uint40[] memory _counters = new uint40[](1);
_counters[0] = uint40(bytes(value).length);
PackedCounter _encodedLengths = PackedCounterLib.pack(_counters);
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/contracts/src/codegen/tables/Dynamics1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ library Dynamics1 {
}

/** Decode the tightly packed blob using this table's schema */
function decode(bytes memory _blob) internal view returns (Dynamics1Data memory _table) {
function decode(bytes memory _blob) internal pure returns (Dynamics1Data memory _table) {
// 0 is the total byte length of static data
PackedCounter _encodedLengths = PackedCounter.wrap(Bytes.slice32(_blob, 0));

Expand Down Expand Up @@ -774,7 +774,7 @@ library Dynamics1 {
uint128[3] memory staticU128,
address[4] memory staticAddrs,
bool[5] memory staticBools
) internal view returns (bytes memory) {
) internal pure returns (bytes memory) {
uint40[] memory _counters = new uint40[](5);
_counters[0] = uint40(staticB32.length * 32);
_counters[1] = uint40(staticI32.length * 4);
Expand Down Expand Up @@ -852,7 +852,7 @@ function toStaticArray_bool_5(bool[] memory _value) pure returns (bool[5] memory
}
}

function fromStaticArray_bytes32_1(bytes32[1] memory _value) view returns (bytes32[] memory _result) {
function fromStaticArray_bytes32_1(bytes32[1] memory _value) pure returns (bytes32[] memory _result) {
_result = new bytes32[](1);
uint256 fromPointer;
uint256 toPointer;
Expand All @@ -863,7 +863,7 @@ function fromStaticArray_bytes32_1(bytes32[1] memory _value) view returns (bytes
Memory.copy(fromPointer, toPointer, 32);
}

function fromStaticArray_int32_2(int32[2] memory _value) view returns (int32[] memory _result) {
function fromStaticArray_int32_2(int32[2] memory _value) pure returns (int32[] memory _result) {
_result = new int32[](2);
uint256 fromPointer;
uint256 toPointer;
Expand All @@ -874,7 +874,7 @@ function fromStaticArray_int32_2(int32[2] memory _value) view returns (int32[] m
Memory.copy(fromPointer, toPointer, 64);
}

function fromStaticArray_uint128_3(uint128[3] memory _value) view returns (uint128[] memory _result) {
function fromStaticArray_uint128_3(uint128[3] memory _value) pure returns (uint128[] memory _result) {
_result = new uint128[](3);
uint256 fromPointer;
uint256 toPointer;
Expand All @@ -885,7 +885,7 @@ function fromStaticArray_uint128_3(uint128[3] memory _value) view returns (uint1
Memory.copy(fromPointer, toPointer, 96);
}

function fromStaticArray_address_4(address[4] memory _value) view returns (address[] memory _result) {
function fromStaticArray_address_4(address[4] memory _value) pure returns (address[] memory _result) {
_result = new address[](4);
uint256 fromPointer;
uint256 toPointer;
Expand All @@ -896,7 +896,7 @@ function fromStaticArray_address_4(address[4] memory _value) view returns (addre
Memory.copy(fromPointer, toPointer, 128);
}

function fromStaticArray_bool_5(bool[5] memory _value) view returns (bool[] memory _result) {
function fromStaticArray_bool_5(bool[5] memory _value) pure returns (bool[] memory _result) {
_result = new bool[](5);
uint256 fromPointer;
uint256 toPointer;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/contracts/src/codegen/tables/Dynamics2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ library Dynamics2 {
}

/** Decode the tightly packed blob using this table's schema */
function decode(bytes memory _blob) internal view returns (Dynamics2Data memory _table) {
function decode(bytes memory _blob) internal pure returns (Dynamics2Data memory _table) {
// 0 is the total byte length of static data
PackedCounter _encodedLengths = PackedCounter.wrap(Bytes.slice32(_blob, 0));

Expand All @@ -503,7 +503,7 @@ library Dynamics2 {
}

/** Tightly pack full data using this table's schema */
function encode(uint64[] memory u64, string memory str, bytes memory b) internal view returns (bytes memory) {
function encode(uint64[] memory u64, string memory str, bytes memory b) internal pure returns (bytes memory) {
uint40[] memory _counters = new uint40[](3);
_counters[0] = uint40(u64.length * 8);
_counters[1] = uint40(bytes(str).length);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/contracts/src/codegen/tables/Ephemeral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ library Ephemeral {
}

/** Tightly pack full data using this table's schema */
function encode(uint256 value) internal view returns (bytes memory) {
function encode(uint256 value) internal pure returns (bytes memory) {
return abi.encodePacked(value);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/contracts/src/codegen/tables/Singleton.sol
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ library Singleton {
/** Decode the tightly packed blob using this table's schema */
function decode(
bytes memory _blob
) internal view returns (int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) {
) internal pure returns (int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) {
// 32 is the total byte length of static data
PackedCounter _encodedLengths = PackedCounter.wrap(Bytes.slice32(_blob, 32));

Expand Down Expand Up @@ -483,7 +483,7 @@ library Singleton {
uint32[2] memory v2,
uint32[2] memory v3,
uint32[1] memory v4
) internal view returns (bytes memory) {
) internal pure returns (bytes memory) {
uint40[] memory _counters = new uint40[](3);
_counters[0] = uint40(v2.length * 4);
_counters[1] = uint40(v3.length * 4);
Expand Down Expand Up @@ -534,7 +534,7 @@ function toStaticArray_uint32_1(uint32[] memory _value) pure returns (uint32[1]
}
}

function fromStaticArray_uint32_2(uint32[2] memory _value) view returns (uint32[] memory _result) {
function fromStaticArray_uint32_2(uint32[2] memory _value) pure returns (uint32[] memory _result) {
_result = new uint32[](2);
uint256 fromPointer;
uint256 toPointer;
Expand All @@ -545,7 +545,7 @@ function fromStaticArray_uint32_2(uint32[2] memory _value) view returns (uint32[
Memory.copy(fromPointer, toPointer, 64);
}

function fromStaticArray_uint32_1(uint32[1] memory _value) view returns (uint32[] memory _result) {
function fromStaticArray_uint32_1(uint32[1] memory _value) pure returns (uint32[] memory _result) {
_result = new uint32[](1);
uint256 fromPointer;
uint256 toPointer;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/contracts/src/codegen/tables/Statics.sol
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ library Statics {
bool v5,
Enum1 v6,
Enum2 v7
) internal view returns (bytes memory) {
) internal pure returns (bytes memory) {
return abi.encodePacked(v1, v2, v3, v4, v5, v6, v7);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function renderUnwrapperStaticArray(
return `
function ${functionName}(
${elementType}[${staticLength}] memory _value
) view returns (
) pure returns (
${internalTypeId} memory _result
) {
_result = new ${internalTypeId}(${staticLength});
Expand Down
30 changes: 15 additions & 15 deletions packages/store/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"file": "test/Gas.t.sol",
"test": "testCompareAbiEncodeVsCustom",
"name": "custom decode",
"gasUsed": 2124
"gasUsed": 2127
},
{
"file": "test/Gas.t.sol",
Expand Down Expand Up @@ -123,7 +123,7 @@
"file": "test/Mixed.t.sol",
"test": "testSetAndGet",
"name": "get record from Mixed",
"gasUsed": 13455
"gasUsed": 13458
},
{
"file": "test/PackedCounter.t.sol",
Expand Down Expand Up @@ -231,37 +231,37 @@
"file": "test/Slice.t.sol",
"test": "testToBytes",
"name": "Slice (0 bytes) to bytes memory",
"gasUsed": 473
"gasUsed": 476
},
{
"file": "test/Slice.t.sol",
"test": "testToBytes",
"name": "Slice (2 bytes) to bytes memory",
"gasUsed": 508
"gasUsed": 511
},
{
"file": "test/Slice.t.sol",
"test": "testToBytes",
"name": "Slice (32 bytes) to bytes memory",
"gasUsed": 507
"gasUsed": 724
},
{
"file": "test/Slice.t.sol",
"test": "testToBytes",
"name": "Slice (34 bytes) to bytes memory",
"gasUsed": 585
"gasUsed": 727
},
{
"file": "test/Slice.t.sol",
"test": "testToBytes",
"name": "Slice (1024 bytes) to bytes memory",
"gasUsed": 4849
"gasUsed": 7443
},
{
"file": "test/Slice.t.sol",
"test": "testToBytes",
"name": "Slice (1024x1024 bytes) to bytes memory",
"gasUsed": 6616506
"gasUsed": 9205372
},
{
"file": "test/Slice.t.sol",
Expand Down Expand Up @@ -339,25 +339,25 @@
"file": "test/StoreCoreDynamic.t.sol",
"test": "testPopFromSecondField",
"name": "pop from field (cold, 1 slot, 1 uint32 item)",
"gasUsed": 29291
"gasUsed": 29294
},
{
"file": "test/StoreCoreDynamic.t.sol",
"test": "testPopFromSecondField",
"name": "pop from field (warm, 1 slot, 1 uint32 item)",
"gasUsed": 19346
"gasUsed": 19349
},
{
"file": "test/StoreCoreDynamic.t.sol",
"test": "testPopFromThirdField",
"name": "pop from field (cold, 2 slots, 10 uint32 items)",
"gasUsed": 31174
"gasUsed": 31177
},
{
"file": "test/StoreCoreDynamic.t.sol",
"test": "testPopFromThirdField",
"name": "pop from field (warm, 2 slots, 10 uint32 items)",
"gasUsed": 19229
"gasUsed": 19232
},
{
"file": "test/StoreCoreGas.t.sol",
Expand Down Expand Up @@ -609,13 +609,13 @@
"file": "test/StoreCoreGas.t.sol",
"test": "testUpdateInField",
"name": "update in field (1 slot, 1 uint32 item)",
"gasUsed": 16605
"gasUsed": 16611
},
{
"file": "test/StoreCoreGas.t.sol",
"test": "testUpdateInField",
"name": "push to field (2 slots, 6 uint64 items)",
"gasUsed": 17696
"gasUsed": 17702
},
{
"file": "test/StoreMetadata.t.sol",
Expand All @@ -627,7 +627,7 @@
"file": "test/StoreMetadata.t.sol",
"test": "testSetAndGet",
"name": "get record from StoreMetadataTable",
"gasUsed": 12116
"gasUsed": 12689
},
{
"file": "test/StoreSwitch.t.sol",
Expand Down
Loading

0 comments on commit 8d51a03

Please sign in to comment.