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

✨ encodeURIComponent #1098

Merged
merged 5 commits into from
Oct 1, 2024
Merged

✨ encodeURIComponent #1098

merged 5 commits into from
Oct 1, 2024

Conversation

Vectorized
Copy link
Owner

@Vectorized Vectorized commented Oct 1, 2024

Description

Continued from #1097.

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

Vectorized and others added 3 commits October 1, 2024 16:31
* Feature: URI Component encoding

* run forge fmt

* add contact

* add doc link

* remove author
Copy link

github-actions bot commented Oct 1, 2024

Gas Snapshot Comparison Report

Generated at commit : 6399a3a, Compared to commit : fe23e6b

Contract Name Test Name Main Gas PR Gas Diff
LibStringTest testContains() 43988 44000 12
testFromAddressToHexStringWithLeadingZeros() 3113 3091 -22
testNormalizeSmallString() 1387 1365 -22
testStringConcat() 4003 4068 65
testStringEndsWith() 2829 2807 -22
testStringEscapeJSONHexEncode() 727636 712815 -14821
testStringIs7BitASCIIWithAllowedLookup() 4939 4945 6
testStringLowerDifferential() 3405674 3725445 319771
testStringPackAndUnpackOne() 756426 4029923 3273497
testStringPackAndUnpackTwo() 916242 896840 -19402
testStringRuneCount() 3035897 3043236 7339
testStringUpperOriginal() 1218 1262 44
testTo7BitASCIIAllowedLookup() 3113 3091 -22
testToHexStringFixedLengthUint256Max() 3656 3701 45
testToStringPositiveNumberBrutalized() 603313 588450 -14863
testToStringSignedOriginalGas() 9839 9899 60
testToStringUint256MaxBrutalized() 614604 600016 -14588
testToStringZeroBrutalized() 602785 587988 -14797
test__codesize() 43124 46388 3264
testStringEncodeURIComponent() - 19245 -

@Vectorized Vectorized merged commit 0883cea into main Oct 1, 2024
10 checks passed
@johnnyshankman
Copy link
Contributor

Happy to have contributed! Thanks for the help @Vectorized

}

// Original implementation of `_encodeURIComponentOriginal`
// credit to John Shankman aka White Lights - [email protected] (whitelights.eth).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

Comment on lines +1106 to +1132
function encodeURIComponent(string memory s) internal pure returns (string memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(0x40)
// Store "0123456789ABCDEF" in scratch space.
// Uppercased to be consistent with JavaScript's implementation.
mstore(0x0f, 0x30313233343536373839414243444546)
let o := add(result, 0x20)
for { let end := add(s, mload(s)) } iszero(eq(s, end)) {} {
s := add(s, 1)
let c := and(mload(s), 0xff)
// If not in `[0-9A-Z-a-z-.!~*'()]`.
if iszero(and(1, shr(c, 0x47fffffe07fffffe03ff678200000000))) {
mstore8(o, 0x25) // '%'.
mstore8(add(o, 1), mload(and(shr(4, c), 15)))
mstore8(add(o, 2), mload(and(c, 15)))
o := add(o, 3)
continue
}
mstore8(o, c)
o := add(o, 1)
}
mstore(result, sub(o, add(result, 0x20))) // Store the length.
mstore(o, 0) // Zeroize the slot after the string.
mstore(0x40, add(o, 0x20)) // Allocate memory.
}
}
Copy link
Contributor

@johnnyshankman johnnyshankman Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vectorized where can i learn more about how you optimized this? really impressive. even putting the lookup table in scratch space with mstore, that's pretty dang cool. never would've thought of that.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opcodes are like the movements of martial arts and fingerstyle guitar.
Study, practice, experiment.
Then combine them to create art.

Marcin is a great inspiration.

https://youtu.be/0FImBCNRGxo?si=hMxZMapKPksYmrFv

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

big thank you. this truly seems like a whole different mindset. once you have a working implementation, it's then about breaking it down into pieces of byte-wise memory and trying to optimize on the mload/mstore level.

this is fun cause now i have a little test. if i can manage to optimize my original function the same way you have (or similarly) i'll know i'm starting to get the hang of it.

thank you vec!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnnyshankman, may I know why you included _ in this encoding method? According to the documentation, it should be excluded.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch indeed, oversight on my part.

Screenshot 2024-10-03 at 4 16 46 PM

@Vectorized Vectorized deleted the encode-uri-component branch October 3, 2024 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants