Skip to content

Commit

Permalink
⚡️ Optimize toString(int256) (#351)
Browse files Browse the repository at this point in the history
Co-authored-by: Vectorized <[email protected]>
  • Loading branch information
transmissions11 and Vectorized authored Dec 31, 2022
1 parent b1db79b commit 3998897
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ FixedPointMathLibTest:testSqrt(uint256) (runs: 256, μ: 997, ~: 1013)
FixedPointMathLibTest:testSqrtBack(uint256) (runs: 256, μ: 15210, ~: 340)
FixedPointMathLibTest:testSqrtBackHashed(uint256) (runs: 256, μ: 59066, ~: 59500)
FixedPointMathLibTest:testSqrtBackHashedSingle() (gas: 58937)
LibStringTest:testDifferentiallyFuzzToString(uint256,bytes) (runs: 256, μ: 20875, ~: 8988)
LibStringTest:testDifferentiallyFuzzToStringInt(int256,bytes) (runs: 256, μ: 20129, ~: 8360)
LibStringTest:testDifferentiallyFuzzToString(uint256,bytes) (runs: 256, μ: 20892, ~: 8988)
LibStringTest:testDifferentiallyFuzzToStringInt(int256,bytes) (runs: 256, μ: 20081, ~: 8356)
LibStringTest:testToString() (gas: 10069)
LibStringTest:testToStringDirty() (gas: 8145)
LibStringTest:testToStringIntNegative() (gas: 11096)
LibStringTest:testToStringIntPositive() (gas: 10509)
LibStringTest:testToStringIntNegative() (gas: 9634)
LibStringTest:testToStringIntPositive() (gas: 10481)
LibStringTest:testToStringOverwrite() (gas: 506)
MerkleProofLibTest:testValidProofSupplied() (gas: 2153)
MerkleProofLibTest:testVerifyEmptyMerkleProofSuppliedLeafAndRootDifferent() (gas: 1458)
Expand Down
2 changes: 1 addition & 1 deletion lib/ds-test
Submodule ds-test updated 1 files
+15 −0 package.json
19 changes: 17 additions & 2 deletions src/utils/LibString.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ pragma solidity >=0.8.0;
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/LibString.sol)
/// @author Modified from Solady (https://github.com/Vectorized/solady/blob/main/src/utils/LibString.sol)
library LibString {
function toString(int256 value) internal pure returns (string memory) {
function toString(int256 value) internal pure returns (string memory str) {
if (value >= 0) return toString(uint256(value));

unchecked {
return value >= 0 ? toString(uint256(value)) : string(abi.encodePacked("-", toString(uint256(-value))));
str = toString(uint256(-value));

/// @solidity memory-safe-assembly
assembly {
// Note: This is only safe because we over-allocate memory
// and write the string from right to left in toString(uint256),
// and thus can be sure that sub(str, 1) is an unused memory location.

let length := mload(str) // Load the string length.
// Put the - character at the start of the string contents.
mstore(str, 45) // 45 is the ASCII code for the - character.
str := sub(str, 1) // Move back the string pointer by a byte.
mstore(str, add(length, 1)) // Update the string length.
}
}
}

Expand Down

0 comments on commit 3998897

Please sign in to comment.