Repository navigation
Build page URLs 1.5x faster by escaping the query with CGI.escape - #933
Merged
Merged
Conversation
QueryUtils.escape relied on URI.encode_www_form_component, which dups, re-encodes and gsubs every key and value. CGI.escape is a C extension returning the same bytes except for '*' and '~', which are still handed to URI, so every generated URL stays byte-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
|
Thanks, merged on dev. Yes to |
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.
Hi! 👋
QueryUtils.escapegoes throughURI.encode_www_form_component, which dups, re-encodes andgsubs every key and value: on apage_urlwith a 3-param query, that is about a third of the call. This swaps it forCGI.escape, the C extension behind theCGI.escapeHTMLpagy already uses. In absolute terms it saves about a microsecond per URL, so it won't show on a typical page; it adds up in helpers that build several URLs (page_url1.5x,urls_hash1.45x,series_nav1.15x faster).Both functions return the same bytes for every input except
*and~(URI keeps*and escapes~, CGI does the opposite), so strings containing either still go through URI and every URL stays byte-identical. Escaping a binary copy and tagging the result US-ASCII keeps URI's behaviour on invalid byte sequences and non-UTF-8 encodings as well. A differential fuzz over 1.3M inputs (random bytes in 8 encodings, symbols, integers, nil, every Unicode codepoint) found no difference on Ruby 3.3, 4.0 and 4.1; the new specs cover the edge cases plus a seeded 3,500-case version of it.Benchmarks
Median of 9 runs (7 on 3.3), page 7 of 42 with
q=ruby on rails&author=alice, master vs this branch:page_urlurls_hashseries_navseries_nav(:bootstrap)page_urlurls_hashseries_navseries_nav(:bootstrap)Separately,
a_lambda.rbstill doesrequire 'cgi', which prints "CGI library is removed from Ruby 4.0" under-W; switching it torequire 'cgi/escape'would silence it, but on 3.3 and 3.4 apps relying on pagy to load the full CGI library (CGI::Cookie,CGI.parse) would break. Would you like that change too, here or in its own PR, or would you rather keepcgias is?