Fix escape_html/h aliases to use C extension instead of pure Ruby fallback#110
Merged
hsbt merged 1 commit intoruby:masterfrom Mar 9, 2026
Merged
Fix escape_html/h aliases to use C extension instead of pure Ruby fallback#110hsbt merged 1 commit intoruby:masterfrom
hsbt merged 1 commit intoruby:masterfrom
Conversation
rwstauner
reviewed
Mar 4, 2026
test/cgi/test_cgi_escape.rb
Outdated
|
|
||
| class CGIEscapeNativeExtTest < Test::Unit::TestCase | ||
| def test_escape_html_uses_native_implementation | ||
| omit "C extension not available" unless defined?(CGI::EscapeExt) |
There was a problem hiding this comment.
These tests are currently failing because the module is declared in ruby (empty):
Line 8 in 22a1f1d
You need the method_defined? check like you did in the lib.
…lback The snake_case aliases (escape_html, h, unescape_html) were defined before `require 'cgi/escape.so'`, so they captured references to the pure Ruby methods. After the C extension loads and prepends EscapeExt, only the camelCase names (escapeHTML, etc.) resolved to the fast C implementation — the aliases still called the ~10x slower Ruby path. Move alias definitions after the C extension loads, and define them on EscapeExt when available so they bind to the optimized C methods.
b429540 to
12dc843
Compare
Member
|
@jeremyevans does this LGTY? |
jeremyevans
approved these changes
Mar 9, 2026
Member
|
@hsbt would you mind taking a look at this PR? Thanks! |
hsbt
approved these changes
Mar 9, 2026
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.
Situation
CGI.escape_html,CGI.h, andCGI.unescape_html(aliases) run the pure Ruby fallback instead of the optimized C extension. The aliases are defined before the ext is required, so they capture references to the Ruby methods.Execution
Moved alias definitions after the C extension loads, defining them on
EscapeExtwhen available so they bind to the C methods. Falls back toself(pure Ruby) when the C ext isn't loaded (TruffleRuby, LoadError). Added tests verifying method owner and allocation parity.