Skip to content

Commit

Permalink
refactor(store): optimize table libraries (#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
dk1a authored Aug 16, 2023
1 parent 331f0d6 commit d5b73b1
Show file tree
Hide file tree
Showing 17 changed files with 1,205 additions and 409 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-spies-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store": patch
---

Optimize autogenerated table libraries
48 changes: 36 additions & 12 deletions e2e/packages/contracts/src/codegen/tables/NumberList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,45 @@ library NumberList {
bytes32[] memory _keyTuple = new bytes32[](0);

uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema());
return _byteLength / 4;
unchecked {
return _byteLength / 4;
}
}

/** Get the length of value (using the specified store) */
function length(IStore _store) internal view returns (uint256) {
bytes32[] memory _keyTuple = new bytes32[](0);

uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema());
return _byteLength / 4;
unchecked {
return _byteLength / 4;
}
}

/** Get an item of value (unchecked, returns invalid data if index overflows) */
/**
* Get an item of value
* (unchecked, returns invalid data if index overflows)
*/
function getItem(uint256 _index) internal view returns (uint32) {
bytes32[] memory _keyTuple = new bytes32[](0);

bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4);
return (uint32(Bytes.slice4(_blob, 0)));
unchecked {
bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4);
return (uint32(Bytes.slice4(_blob, 0)));
}
}

/** Get an item of value (using the specified store) (unchecked, returns invalid data if index overflows) */
/**
* Get an item of value (using the specified store)
* (unchecked, returns invalid data if index overflows)
*/
function getItem(IStore _store, uint256 _index) internal view returns (uint32) {
bytes32[] memory _keyTuple = new bytes32[](0);

bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4);
return (uint32(Bytes.slice4(_blob, 0)));
unchecked {
bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4);
return (uint32(Bytes.slice4(_blob, 0)));
}
}

/** Push an element to value */
Expand Down Expand Up @@ -154,18 +168,28 @@ library NumberList {
_store.popFromField(_tableId, _keyTuple, 0, 4);
}

/** Update an element of value at `_index` */
/**
* Update an element of value at `_index`
* (checked only to prevent modifying other tables; can corrupt own data if index overflows)
*/
function update(uint256 _index, uint32 _element) internal {
bytes32[] memory _keyTuple = new bytes32[](0);

StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)));
unchecked {
StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)));
}
}

/** Update an element of value (using the specified store) at `_index` */
/**
* Update an element of value (using the specified store) at `_index`
* (checked only to prevent modifying other tables; can corrupt own data if index overflows)
*/
function update(IStore _store, uint256 _index, uint32 _element) internal {
bytes32[] memory _keyTuple = new bytes32[](0);

_store.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)));
unchecked {
_store.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)));
}
}

/** Tightly pack full data using this table's schema */
Expand Down
Loading

0 comments on commit d5b73b1

Please sign in to comment.