Skip to content

Commit

Permalink
Optimize array allocation in ERC1155 (#4196)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco <fg@frang.io>
  • Loading branch information
RenanSouza2 and frangio authored Jun 2, 2023
1 parent 30256fa commit 5cef83d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-books-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': patch
---

`ERC1155`: Optimize array allocation.
30 changes: 19 additions & 11 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ 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) = _asSingletonArrays(id, amount);
_update(from, to, ids, amounts, data);
}

Expand Down Expand Up @@ -269,8 +268,7 @@ 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) = _asSingletonArrays(id, amount);
_update(address(0), to, ids, amounts, data);
}

Expand Down Expand Up @@ -302,8 +300,7 @@ 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) = _asSingletonArrays(id, amount);
_update(from, address(0), ids, amounts, "");
}

Expand Down Expand Up @@ -376,10 +373,21 @@ 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;

return array;
function _asSingletonArrays(
uint256 element1,
uint256 element2
) private pure returns (uint256[] memory array1, uint256[] memory array2) {
/// @solidity memory-safe-assembly
assembly {
array1 := mload(0x40)
mstore(array1, 1)
mstore(add(array1, 0x20), element1)

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

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

0 comments on commit 5cef83d

Please sign in to comment.