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

Add a percent decoding fast-path #101

Merged
merged 1 commit into from
Dec 18, 2021
Merged

Add a percent decoding fast-path #101

merged 1 commit into from
Dec 18, 2021

Conversation

karwa
Copy link
Owner

@karwa karwa commented Dec 18, 2021

If the source data does not even contain a "%", we can trivially determine that percent-decoding is not required. Similarly, if it does not contain a "%" or "+", we can determine that form-decoding is not required.

This cuts the time to iterate path components (which often don't contain percent-encoding) by about 30-40%, and sometimes much more -- if the source is a String, we don't even need to copy. And it has essentially no impact on data which actually is encoded, because constructing a string from decoded contents is way more expensive than this check.

Before:

name                                    time        std        iterations
-------------------------------------------------------------------------
PathComponents.Iteration.Small.Forwards 1284.000 ns ±  49.23 %    1000000       <-----
PathComponents.Iteration.Small.Reverse  1255.000 ns ±  44.46 %    1000000       <-----
PathComponents.Iteration.Long.Forwards  5708.000 ns ±  29.53 %     223292       <-----
PathComponents.Iteration.Long.Reverse   5663.000 ns ±  28.37 %     224446       <-----
PathComponents.Append.Single            1604.000 ns ±  44.27 %     776674
PathComponents.Append.Multiple          2206.000 ns ±  46.02 %     557985
PathComponents.RemoveLast.Single         836.000 ns ±  69.40 %    1000000
PathComponents.RemoveLast.Multiple       863.000 ns ±  46.90 %    1000000
PathComponents.ReplaceSubrange.Shrink   1616.000 ns ±  46.60 %     774023
PathComponents.ReplaceSubrange.Grow     2695.000 ns ±  38.74 %     465685
name                                    time        std        iterations
-------------------------------------------------------------------------
URLEncoded.String.urlEncoded            9156.000 ns ±  28.04 %     142419
URLEncoded.String.urlDecoded            4634.000 ns ±  30.95 %     275012
URLEncoded.String.urlDecoded.notEncoded 2114.000 ns ±  48.68 %     583163       <-----

After:

name                                    time        std        iterations
-------------------------------------------------------------------------
PathComponents.Iteration.Small.Forwards  878.000 ns ±  36.19 %    1000000       <-----
PathComponents.Iteration.Small.Reverse   861.000 ns ±  34.98 %    1000000       <-----
PathComponents.Iteration.Long.Forwards  3908.000 ns ±  17.90 %     342676       <-----
PathComponents.Iteration.Long.Reverse   3768.000 ns ±  18.62 %     354951       <-----
PathComponents.Append.Single            1562.000 ns ±  27.47 %     804555
PathComponents.Append.Multiple          2140.000 ns ±  23.04 %     631751
PathComponents.RemoveLast.Single         698.000 ns ±  33.81 %    1000000
PathComponents.RemoveLast.Multiple       714.000 ns ±  32.44 %    1000000
PathComponents.ReplaceSubrange.Shrink   1642.000 ns ±  24.41 %     781916
PathComponents.ReplaceSubrange.Grow     2658.000 ns ±  21.13 %     500778
name                                    time        std        iterations
-------------------------------------------------------------------------
URLEncoded.String.urlEncoded            8775.000 ns ±  26.12 %     138089
URLEncoded.String.urlDecoded            4417.000 ns ±  36.92 %     290181
URLEncoded.String.urlDecoded.notEncoded  403.000 ns ±  68.14 %    1000000       <-----

@karwa karwa merged commit b97bef0 into main Dec 18, 2021
@karwa karwa deleted the twiddlywinks branch January 19, 2022 22:06
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.

None yet

1 participant