judofyr/temple

Add support for Ruby 2.3 native CGI.escapeHTML.

Closed this issue · 2 comments

Ruby 2.3 added an optimized escapeHTML method with a native extension. ruby/ruby#1164

I modified the benchmark that was included in the pull request to add escape_util's escapeHTML method. https://gist.github.com/skunkworker/9e492580df0242dce087

$ ruby bench_escape.rb "'&\"<>"
Escape: '&"<>
Warming up --------------------------------------
                 old    16.414k i/100ms
        escape utils    44.654k i/100ms
                 new    66.623k i/100ms
Calculating -------------------------------------
                 old    209.822k (± 7.1%) i/s -      1.050M
        escape utils    896.613k (± 8.2%) i/s -      4.465M
                 new      1.622M (± 5.5%) i/s -      8.128M

Comparison:
                 new:  1622028.8 i/s
        escape utils:   896612.9 i/s - 1.81x slower
                 old:   209822.2 i/s - 7.73x slower

$ ruby bench_escape.rb "hello world"
Escape: hello world
Warming up --------------------------------------
                 old    62.537k i/100ms
        escape utils   100.999k i/100ms
                 new   104.930k i/100ms
Calculating -------------------------------------
                 old      1.534M (± 6.3%) i/s -      7.692M
        escape utils      4.665M (± 5.6%) i/s -     23.331M
                 new      4.721M (± 5.6%) i/s -     23.609M

Comparison:
                 new:  4721399.0 i/s
        escape utils:  4664966.6 i/s - same-ish: difference falls within error
                 old:  1534447.7 i/s - 3.08x slower

which interestingly shows that escape_utils is similar on some strings while still quite a bit slower on others.
I've created a pull request with the newer code that passes all of the tests.

I went and looked at escapeUtil's escapeHTML method in the benchmarks and using the test case for downloading https://en.wikipedia.org/wiki/Succession_to_the_British_throne and escaping it.

EscapeUtils is still about 2x faster than the new native escapeHTML method, but I still think it's a good idea because we won't need to rely upon escape_utils if it isn't there.

https://gist.github.com/skunkworker/1b7bcf297afe70076051

$ ruby bench_escape.rb
Escape data from https://en.wikipedia.org/wiki/Succession_to_the_British_throne
Warming up --------------------------------------
                 old    13.000  i/100ms
        escape utils   121.000  i/100ms
                 new    64.000  i/100ms
Calculating -------------------------------------
                 old    130.037  (± 6.9%) i/s -    650.000 
        escape utils      1.280k (± 8.8%) i/s -      6.413k
                 new    646.935  (± 7.7%) i/s -      3.264k

Comparison:
        escape utils:     1279.8 i/s
                 new:      646.9 i/s - 1.98x slower
                 old:      130.0 i/s - 9.84x slower
minad commented

done