Skip to content

Commit 3998897

Browse files
⚡️ Optimize toString(int256) (#351)
Co-authored-by: Vectorized <[email protected]>
1 parent b1db79b commit 3998897

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Diff for: .gas-snapshot

+4-4
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ FixedPointMathLibTest:testSqrt(uint256) (runs: 256, μ: 997, ~: 1013)
279279
FixedPointMathLibTest:testSqrtBack(uint256) (runs: 256, μ: 15210, ~: 340)
280280
FixedPointMathLibTest:testSqrtBackHashed(uint256) (runs: 256, μ: 59066, ~: 59500)
281281
FixedPointMathLibTest:testSqrtBackHashedSingle() (gas: 58937)
282-
LibStringTest:testDifferentiallyFuzzToString(uint256,bytes) (runs: 256, μ: 20875, ~: 8988)
283-
LibStringTest:testDifferentiallyFuzzToStringInt(int256,bytes) (runs: 256, μ: 20129, ~: 8360)
282+
LibStringTest:testDifferentiallyFuzzToString(uint256,bytes) (runs: 256, μ: 20892, ~: 8988)
283+
LibStringTest:testDifferentiallyFuzzToStringInt(int256,bytes) (runs: 256, μ: 20081, ~: 8356)
284284
LibStringTest:testToString() (gas: 10069)
285285
LibStringTest:testToStringDirty() (gas: 8145)
286-
LibStringTest:testToStringIntNegative() (gas: 11096)
287-
LibStringTest:testToStringIntPositive() (gas: 10509)
286+
LibStringTest:testToStringIntNegative() (gas: 9634)
287+
LibStringTest:testToStringIntPositive() (gas: 10481)
288288
LibStringTest:testToStringOverwrite() (gas: 506)
289289
MerkleProofLibTest:testValidProofSupplied() (gas: 2153)
290290
MerkleProofLibTest:testVerifyEmptyMerkleProofSuppliedLeafAndRootDifferent() (gas: 1458)

Diff for: lib/ds-test

Submodule ds-test updated 1 file

Diff for: src/utils/LibString.sol

+17-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@ pragma solidity >=0.8.0;
55
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/LibString.sol)
66
/// @author Modified from Solady (https://github.com/Vectorized/solady/blob/main/src/utils/LibString.sol)
77
library LibString {
8-
function toString(int256 value) internal pure returns (string memory) {
8+
function toString(int256 value) internal pure returns (string memory str) {
9+
if (value >= 0) return toString(uint256(value));
10+
911
unchecked {
10-
return value >= 0 ? toString(uint256(value)) : string(abi.encodePacked("-", toString(uint256(-value))));
12+
str = toString(uint256(-value));
13+
14+
/// @solidity memory-safe-assembly
15+
assembly {
16+
// Note: This is only safe because we over-allocate memory
17+
// and write the string from right to left in toString(uint256),
18+
// and thus can be sure that sub(str, 1) is an unused memory location.
19+
20+
let length := mload(str) // Load the string length.
21+
// Put the - character at the start of the string contents.
22+
mstore(str, 45) // 45 is the ASCII code for the - character.
23+
str := sub(str, 1) // Move back the string pointer by a byte.
24+
mstore(str, add(length, 1)) // Update the string length.
25+
}
1126
}
1227
}
1328

0 commit comments

Comments
 (0)