From 2e8ca596a8055f39e7f37343498845c622d5adc4 Mon Sep 17 00:00:00 2001 From: Jay Paik Date: Tue, 15 Oct 2024 14:33:20 -0400 Subject: [PATCH] feat: SparseCalldataSegmentLib optimization (#200) --- src/helpers/EmptyCalldataSlice.sol | 12 ++++++++++++ src/libraries/SparseCalldataSegmentLib.sol | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/helpers/EmptyCalldataSlice.sol diff --git a/src/helpers/EmptyCalldataSlice.sol b/src/helpers/EmptyCalldataSlice.sol new file mode 100644 index 00000000..381f5765 --- /dev/null +++ b/src/helpers/EmptyCalldataSlice.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity ^0.8.20; + +function getEmptyCalldataSlice() pure returns (bytes calldata) { + bytes calldata empty; + + assembly ("memory-safe") { + empty.length := 0 + } + + return empty; +} diff --git a/src/libraries/SparseCalldataSegmentLib.sol b/src/libraries/SparseCalldataSegmentLib.sol index 17f1e680..5b27c6ae 100644 --- a/src/libraries/SparseCalldataSegmentLib.sol +++ b/src/libraries/SparseCalldataSegmentLib.sol @@ -2,6 +2,7 @@ pragma solidity ^0.8.20; import {RESERVED_VALIDATION_DATA_INDEX} from "../helpers/Constants.sol"; +import {getEmptyCalldataSlice} from "../helpers/EmptyCalldataSlice.sol"; /// @title Sparse Calldata Segment Library /// @notice Library for working with sparsely-packed calldata segments, identified with an index. @@ -51,7 +52,7 @@ library SparseCalldataSegmentLib { function advanceSegmentIfAtIndex(bytes calldata source, uint8 index) internal pure - returns (bytes memory, bytes calldata) + returns (bytes calldata, bytes calldata) { uint8 nextIndex = getIndex(source); @@ -69,7 +70,7 @@ library SparseCalldataSegmentLib { return (segment, remainder); } - return ("", source); + return (getEmptyCalldataSlice(), source); } /// @notice Extracts the final segment from the source.