Repository navigation
Decode URI components with cgi/escape - #276
Open
thomasklemm wants to merge 1 commit into
Open
thomasklemm wants to merge 1 commit into
thomasklemm wants to merge 1 commit into
Conversation
thomasklemm
marked this pull request as draft
November 27, 2026 21:10
thomasklemm
commented
Nov 27, 2026
URI.decode_www_form_component and URI.decode_uri_component copy the String with `b` and then run `gsub` with a regexp and a Hash table, which costs a match, a substring and a Hash lookup for every "+" and "%XX". Rack and Rails decode query strings, URL-encoded form fields and cookies through decode_www_form_component. The shared _decode_uri_component keeps the lone-"%" check, the `b` copy and the final force_encoding, and now takes the decoding step as a block. Where cgi/escape implements unescapeURIComponent natively (in C since cgi 0.3.3, in Java since 0.4.1), decode_www_form_component passes CGI.unescape and decode_uri_component passes CGI.unescapeURIComponent, which keeps "+"; both decode in one pass. Both get the BINARY copy, so the result has the same class, bytes and encoding as before. Elsewhere both methods keep the `gsub`: older cgi has no unescapeURIComponent, and on TruffleRuby cgi/escape is written in Ruby, where CGI.unescapeURIComponent keeps the String subclass that String#b returns there. URI.decode_www_form keeps its `gsub!`: it leaves a lone "%" as is, and for the plain keys and values most forms have, CGI.unescape would return a new String where `gsub!` changes nothing.
thomasklemm
force-pushed
the
thomasklemm/decode-www-form-component-cgi
branch
from
November 28, 2026 00:15
c43954f to
e2d2e21
Compare
thomasklemm
marked this pull request as ready for review
November 28, 2026 00:16
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
URI.decode_www_form_componentandURI.decode_uri_componentcopy the string withband then rungsubwith a regexp and a Hash, which costs a match, a substring and a Hash lookup for every+and%XX. Rack decodes query strings and cookies withdecode_www_form_component(Rack::QueryParser and Rack::Utils.unescape in Rack 3.2.9), and Rails decodes query parameters and URL-encoded form fields with it (ActionDispatch::QueryParser, Rails 8.1.4.1).The shared helper now yields the checked copy to
CGI.unescapeorCGI.unescapeURIComponent, which decode in one pass in C (in Java on JRuby). Results have the same class, bytes and encoding as before. Where cgi/escape has no nativeunescapeURIComponent(cgi before 0.3.3, TruffleRuby), both methods keep thegsub. uri now requires cgi/escape, which every supported Ruby ships.decode_www_form_component("hello")decode_www_form_component("article%5Btitle%5D")decode_www_form_component("Hello+world+from+Rack")decode_www_form_componenton a 248-byte cookie valuedecode_uri_component("hello")decode_uri_component("user%40example.com")With YJIT, and with ZJIT on Ruby master, time per call drops by 23% to 80%. Each call allocates 2 or 3 objects instead of 4 to 8.