Skip to content

Commit 21cd7e8

Browse files
ernestognwAmxxarr00
authored
Add Memory utility library (#5189)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
1 parent a5350ec commit 21cd7e8

File tree

7 files changed

+146
-1
lines changed

7 files changed

+146
-1
lines changed

.changeset/dull-students-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': minor
3+
---
4+
5+
`Memory`: Add library with utilities to manipulate memory

contracts/mocks/Stateless.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {SignatureChecker} from "../utils/cryptography/SignatureChecker.sol";
4949
import {SignedMath} from "../utils/math/SignedMath.sol";
5050
import {StorageSlot} from "../utils/StorageSlot.sol";
5151
import {Strings} from "../utils/Strings.sol";
52+
import {Memory} from "../utils/Memory.sol";
5253
import {Time} from "../utils/types/Time.sol";
5354

5455
contract Dummy1234 {}

contracts/utils/Memory.sol

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
/**
6+
* @dev Utilities to manipulate memory.
7+
*
8+
* Memory is a contiguous and dynamic byte array in which Solidity stores non-primitive types.
9+
* This library provides functions to manipulate pointers to this dynamic array.
10+
*
11+
* WARNING: When manipulating memory, make sure to follow the Solidity documentation
12+
* guidelines for https://docs.soliditylang.org/en/v0.8.20/assembly.html#memory-safety[Memory Safety].
13+
*/
14+
library Memory {
15+
type Pointer is bytes32;
16+
17+
/// @dev Returns a `Pointer` to the current free `Pointer`.
18+
function getFreeMemoryPointer() internal pure returns (Pointer ptr) {
19+
assembly ("memory-safe") {
20+
ptr := mload(0x40)
21+
}
22+
}
23+
24+
/**
25+
* @dev Sets the free `Pointer` to a specific value.
26+
*
27+
* WARNING: Everything after the pointer may be overwritten.
28+
**/
29+
function setFreeMemoryPointer(Pointer ptr) internal pure {
30+
assembly ("memory-safe") {
31+
mstore(0x40, ptr)
32+
}
33+
}
34+
35+
/// @dev `Pointer` to `bytes32`. Expects a pointer to a properly ABI-encoded `bytes` object.
36+
function asBytes32(Pointer ptr) internal pure returns (bytes32) {
37+
return Pointer.unwrap(ptr);
38+
}
39+
40+
/// @dev `bytes32` to `Pointer`. Expects a pointer to a properly ABI-encoded `bytes` object.
41+
function asPointer(bytes32 value) internal pure returns (Pointer) {
42+
return Pointer.wrap(value);
43+
}
44+
}

contracts/utils/README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
3838
* {Panic}: A library to revert with https://docs.soliditylang.org/en/v0.8.20/control-structures.html#panic-via-assert-and-error-via-require[Solidity panic codes].
3939
* {Comparators}: A library that contains comparator functions to use with the {Heap} library.
4040
* {CAIP2}, {CAIP10}: Libraries for formatting and parsing CAIP-2 and CAIP-10 identifiers.
41+
* {Memory}: A utility library to manipulate memory.
4142
* {InteroperableAddress}: Library for formatting and parsing ERC-7930 interoperable addresses.
4243
* {Blockhash}: A library for accessing historical block hashes beyond the standard 256 block limit utilizing EIP-2935's historical blockhash functionality.
4344
* {Time}: A library that provides helpers for manipulating time-related objects, including a `Delay` type.
@@ -135,6 +136,8 @@ Ethereum contracts have no native concept of an interface, so applications must
135136

136137
{{CAIP10}}
137138

139+
{{Memory}}
140+
138141
{{InteroperableAddress}}
139142

140143
{{Blockhash}}

docs/modules/ROOT/pages/utilities.adoc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Some use cases require more powerful data structures than arrays and mappings of
263263
- xref:api:utils.adoc#EnumerableSet[`EnumerableSet`]: A https://en.wikipedia.org/wiki/Set_(abstract_data_type)[set] with enumeration capabilities.
264264
- xref:api:utils.adoc#EnumerableMap[`EnumerableMap`]: A `mapping` variant with enumeration capabilities.
265265
- xref:api:utils.adoc#MerkleTree[`MerkleTree`]: An on-chain https://wikipedia.org/wiki/Merkle_Tree[Merkle Tree] with helper functions.
266-
- xref:api:utils.adoc#Heap.sol[`Heap`]: A
266+
- xref:api:utils.adoc#Heap.sol[`Heap`]: A https://en.wikipedia.org/wiki/Binary_heap[binary heap] to store elements with priority defined by a compartor function.
267267

268268
The `Enumerable*` structures are similar to mappings in that they store and remove elements in constant time and don't allow for repeated entries, but they also support _enumeration_, which means you can easily query all stored entries both on and off-chain.
269269

@@ -461,6 +461,38 @@ await instance.multicall([
461461
]);
462462
----
463463

464+
=== Memory
465+
466+
The xref:api:utils.adoc#Memory[`Memory`] library provides functions for advanced use cases that require granular memory management. A common use case is to avoid unnecessary memory expansion costs when performing repeated operations that allocate memory in a loop. Consider the following example:
467+
468+
[source,solidity]
469+
----
470+
function processMultipleItems(uint256[] memory items) internal {
471+
for (uint256 i = 0; i < items.length; i++) {
472+
bytes memory tempData = abi.encode(items[i], block.timestamp);
473+
// Process tempData...
474+
}
475+
}
476+
----
477+
478+
Note that each iteration allocates new memory for `tempData`, causing the memory to expand continuously. This can be optimized by resetting the memory pointer between iterations:
479+
480+
[source,solidity]
481+
----
482+
function processMultipleItems(uint256[] memory items) internal {
483+
Memory.Pointer ptr = Memory.getFreeMemoryPointer(); // Cache pointer
484+
for (uint256 i = 0; i < items.length; i++) {
485+
bytes memory tempData = abi.encode(items[i], block.timestamp);
486+
// Process tempData...
487+
Memory.setFreeMemoryPointer(ptr); // Reset pointer for reuse
488+
}
489+
}
490+
----
491+
492+
This way, memory allocated for `tempData` in each iteration is reused, significantly reducing memory expansion costs when processing many items.
493+
494+
IMPORTANT: Only use these functions after carefully confirming they're necessary. By default, Solidity handles memory safely. Using this library without understanding memory layout and safety may be dangerous. See the https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_memory.html[memory layout] and https://docs.soliditylang.org/en/v0.8.20/assembly.html#memory-safety[memory safety] documentation for details.
495+
464496
=== Historical Block Hashes
465497

466498
xref:api:utils.adoc#Blockhash[`Blockhash`] provides L2 protocol developers with extended access to historical block hashes beyond Ethereum's native 256-block limit. By leveraging https://eips.ethereum.org/EIPS/eip-2935[EIP-2935]'s history storage contract, the library enables access to block hashes up to 8,191 blocks in the past, making it invaluable for L2 fraud proofs and state verification systems.

test/utils/Memory.t.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
import {Test} from "forge-std/Test.sol";
6+
import {Memory} from "@openzeppelin/contracts/utils/Memory.sol";
7+
8+
contract MemoryTest is Test {
9+
using Memory for *;
10+
11+
// - first 0x80 bytes are reserved (scratch + FMP + zero)
12+
uint256 constant START_PTR = 0x80;
13+
// - moving the free memory pointer to far causes OOG errors
14+
uint256 constant END_PTR = type(uint24).max;
15+
16+
function testGetsetFreeMemoryPointer(uint256 seed) public pure {
17+
bytes32 ptr = bytes32(bound(seed, START_PTR, END_PTR));
18+
ptr.asPointer().setFreeMemoryPointer();
19+
assertEq(Memory.getFreeMemoryPointer().asBytes32(), ptr);
20+
}
21+
}

test/utils/Memory.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { ethers } = require('hardhat');
2+
const { expect } = require('chai');
3+
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
4+
5+
async function fixture() {
6+
const mock = await ethers.deployContract('$Memory');
7+
return { mock };
8+
}
9+
10+
describe('Memory', function () {
11+
beforeEach(async function () {
12+
Object.assign(this, await loadFixture(fixture));
13+
});
14+
15+
describe('free pointer', function () {
16+
it('sets free memory pointer', async function () {
17+
const ptr = ethers.toBeHex(0xa0, 32);
18+
await expect(this.mock.$setFreeMemoryPointer(ptr)).to.not.be.reverted;
19+
});
20+
21+
it('gets free memory pointer', async function () {
22+
await expect(this.mock.$getFreeMemoryPointer()).to.eventually.equal(
23+
ethers.toBeHex(0x80, 32), // Default pointer
24+
);
25+
});
26+
});
27+
28+
describe('pointer conversions', function () {
29+
it('asBytes32', async function () {
30+
const ptr = ethers.toBeHex('0x1234', 32);
31+
await expect(this.mock.$asBytes32(ptr)).to.eventually.equal(ptr);
32+
});
33+
34+
it('asPointer', async function () {
35+
const ptr = ethers.toBeHex('0x1234', 32);
36+
await expect(this.mock.$asPointer(ptr)).to.eventually.equal(ptr);
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)