Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize array allocation in ERC1155 #4196

Merged
merged 10 commits into from
Jun 2, 2023
35 changes: 25 additions & 10 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
function _safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) internal {
require(to != address(0), "ERC1155: transfer to the zero address");
require(from != address(0), "ERC1155: transfer from the zero address");
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);
uint256[] memory ids;
uint256[] memory amounts;
(ids, amounts) = _asSingletonArrays(id, amount);
RenanSouza2 marked this conversation as resolved.
Show resolved Hide resolved
_update(from, to, ids, amounts, data);
}

Expand Down Expand Up @@ -269,8 +270,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
*/
function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal {
require(to != address(0), "ERC1155: mint to the zero address");
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);
uint256[] memory ids;
uint256[] memory amounts;
(ids, amounts) = _asSingletonArrays(id, amount);
RenanSouza2 marked this conversation as resolved.
Show resolved Hide resolved
_update(address(0), to, ids, amounts, data);
}

Expand Down Expand Up @@ -302,8 +304,9 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
*/
function _burn(address from, uint256 id, uint256 amount) internal {
require(from != address(0), "ERC1155: burn from the zero address");
uint256[] memory ids = _asSingletonArray(id);
uint256[] memory amounts = _asSingletonArray(amount);
uint256[] memory ids;
uint256[] memory amounts;
(ids, amounts) = _asSingletonArrays(id, amount);
RenanSouza2 marked this conversation as resolved.
Show resolved Hide resolved
_update(from, address(0), ids, amounts, "");
}

Expand Down Expand Up @@ -376,10 +379,22 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
}
}

function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
uint256[] memory array = new uint256[](1);
array[0] = element;
function _asSingletonArrays(uint256 element1, uint256 element2) private pure returns (uint256[] memory, uint256[] memory) {
uint256[] memory array1;
uint256[] memory array2;

assembly {
RenanSouza2 marked this conversation as resolved.
Show resolved Hide resolved
array1 := mload(0x40)
mstore(array1, 1)
mstore(add(array1, 0x20), element1)

return array;
array2 := add(array1, 0x40)
mstore(array2, 1)
mstore(add(array2, 0x20), element2)

mstore(0x40, add(array2, 0x40))
}

return (array1, array2);
}
}