You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contracts/utils/README.adoc
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
38
38
* {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].
39
39
* {Comparators}: A library that contains comparator functions to use with the {Heap} library.
40
40
* {CAIP2}, {CAIP10}: Libraries for formatting and parsing CAIP-2 and CAIP-10 identifiers.
41
+
* {Memory}: A utility library to manipulate memory.
41
42
* {InteroperableAddress}: Library for formatting and parsing ERC-7930 interoperable addresses.
42
43
* {Blockhash}: A library for accessing historical block hashes beyond the standard 256 block limit utilizing EIP-2935's historical blockhash functionality.
43
44
* {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
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/utilities.adoc
+33-1Lines changed: 33 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -263,7 +263,7 @@ Some use cases require more powerful data structures than arrays and mappings of
263
263
- xref:api:utils.adoc#EnumerableSet[`EnumerableSet`]: A https://en.wikipedia.org/wiki/Set_(abstract_data_type)[set] with enumeration capabilities.
264
264
- xref:api:utils.adoc#EnumerableMap[`EnumerableMap`]: A `mapping` variant with enumeration capabilities.
265
265
- 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.
267
267
268
268
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.
269
269
@@ -461,6 +461,38 @@ await instance.multicall([
461
461
]);
462
462
----
463
463
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 {
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 {
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
+
464
496
=== Historical Block Hashes
465
497
466
498
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.
0 commit comments