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 DynamicArrayLib #1084

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
T
  • Loading branch information
Vectorized committed Sep 19, 2024
commit 7a132d1a0eb56477a54c785aa19fdcba0d22ce71
7 changes: 4 additions & 3 deletions src/utils/DynamicArrayLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ library DynamicArrayLib {
function malloc(uint256 n) internal pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(0x40)
result := or(sub(0, shr(32, n)), mload(0x40))
mstore(result, n)
mstore(or(sub(0, shr(32, n)), 0x40), add(add(result, 0x20), shl(5, n)))
mstore(0x40, add(add(result, 0x20), shl(5, n)))
}
}

Expand Down Expand Up @@ -204,7 +204,8 @@ library DynamicArrayLib {
result = array;
/// @solidity memory-safe-assembly
assembly {
for { let arrData := mload(or(sub(0, shr(32, minimum)), array)) } 1 {} {
if iszero(lt(minimum, 0xffffffff)) { invalid() } // For extra safety.
for { let arrData := mload(array) } 1 {} {
// Some random prime number to multiply `cap`, so that
// we know that the `cap` is for a dynamic array.
// Selected to be larger than any memory pointer realistically.
Expand Down