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 access in ERC1155 #4300

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/hot-coins-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'openzeppelin-solidity': patch
---

`Arrays`: Add `unsafeMemoryAccess` helpers to read from a memory array without checking the length.
`ERC1155`: optimize memory accesses
14 changes: 9 additions & 5 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "./IERC1155Receiver.sol";
import "./extensions/IERC1155MetadataURI.sol";
import "../../utils/Context.sol";
import "../../utils/introspection/ERC165.sol";
import "../../utils/Arrays.sol";

/**
* @dev Implementation of the basic standard multi-token.
Expand All @@ -17,6 +18,9 @@ import "../../utils/introspection/ERC165.sol";
* _Available since v3.1._
*/
contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
using Arrays for uint256[];
using Arrays for address[];

// Mapping from token ID to account balances
mapping(uint256 => mapping(address => uint256)) private _balances;

Expand Down Expand Up @@ -84,7 +88,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256[] memory batchBalances = new uint256[](accounts.length);

for (uint256 i = 0; i < accounts.length; ++i) {
batchBalances[i] = balanceOf(accounts[i], ids[i]);
batchBalances[i] = balanceOf(accounts.unsafeMemoryAccess(i), ids.unsafeMemoryAccess(i));
}

return batchBalances;
Expand Down Expand Up @@ -160,8 +164,8 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
address operator = _msgSender();

for (uint256 i = 0; i < ids.length; ++i) {
uint256 id = ids[i];
uint256 amount = amounts[i];
uint256 id = ids.unsafeMemoryAccess(i);
uint256 amount = amounts.unsafeMemoryAccess(i);

if (from != address(0)) {
uint256 fromBalance = _balances[id][from];
Expand All @@ -177,8 +181,8 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
}

if (ids.length == 1) {
uint256 id = ids[0];
uint256 amount = amounts[0];
uint256 id = ids.unsafeMemoryAccess(0);
uint256 amount = amounts.unsafeMemoryAccess(0);
emit TransferSingle(operator, from, to, id, amount);
if (to != address(0)) {
_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
Expand Down
12 changes: 12 additions & 0 deletions contracts/utils/Arrays.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,16 @@ library Arrays {
}
return slot.getUint256Slot();
}

function unsafeMemoryAccess(uint256[] memory arr, uint256 pos) internal pure returns (uint256 res) {
assembly {
res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
}
}

function unsafeMemoryAccess(address[] memory arr, uint256 pos) internal pure returns (address res) {
assembly {
res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
}
}
}