Skip to content

Commit 5cef83d

Browse files
RenanSouza2frangio
andauthored
Optimize array allocation in ERC1155 (#4196)
Co-authored-by: Francisco <fg@frang.io>
1 parent 30256fa commit 5cef83d

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

.changeset/serious-books-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': patch
3+
---
4+
5+
`ERC1155`: Optimize array allocation.

contracts/token/ERC1155/ERC1155.sol

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
206206
function _safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data) internal {
207207
require(to != address(0), "ERC1155: transfer to the zero address");
208208
require(from != address(0), "ERC1155: transfer from the zero address");
209-
uint256[] memory ids = _asSingletonArray(id);
210-
uint256[] memory amounts = _asSingletonArray(amount);
209+
(uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
211210
_update(from, to, ids, amounts, data);
212211
}
213212

@@ -269,8 +268,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
269268
*/
270269
function _mint(address to, uint256 id, uint256 amount, bytes memory data) internal {
271270
require(to != address(0), "ERC1155: mint to the zero address");
272-
uint256[] memory ids = _asSingletonArray(id);
273-
uint256[] memory amounts = _asSingletonArray(amount);
271+
(uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
274272
_update(address(0), to, ids, amounts, data);
275273
}
276274

@@ -302,8 +300,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
302300
*/
303301
function _burn(address from, uint256 id, uint256 amount) internal {
304302
require(from != address(0), "ERC1155: burn from the zero address");
305-
uint256[] memory ids = _asSingletonArray(id);
306-
uint256[] memory amounts = _asSingletonArray(amount);
303+
(uint256[] memory ids, uint256[] memory amounts) = _asSingletonArrays(id, amount);
307304
_update(from, address(0), ids, amounts, "");
308305
}
309306

@@ -376,10 +373,21 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
376373
}
377374
}
378375

379-
function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
380-
uint256[] memory array = new uint256[](1);
381-
array[0] = element;
382-
383-
return array;
376+
function _asSingletonArrays(
377+
uint256 element1,
378+
uint256 element2
379+
) private pure returns (uint256[] memory array1, uint256[] memory array2) {
380+
/// @solidity memory-safe-assembly
381+
assembly {
382+
array1 := mload(0x40)
383+
mstore(array1, 1)
384+
mstore(add(array1, 0x20), element1)
385+
386+
array2 := add(array1, 0x40)
387+
mstore(array2, 1)
388+
mstore(add(array2, 0x20), element2)
389+
390+
mstore(0x40, add(array2, 0x40))
391+
}
384392
}
385393
}

0 commit comments

Comments
 (0)