Skip to content

Commit

Permalink
Optimize BaseParser#unnormalize method
Browse files Browse the repository at this point in the history
## Benchmark
```
RUBYLIB= BUNDLER_ORIG_RUBYLIB= /Users/naitoh/.rbenv/versions/3.3.3/bin/ruby -v -S benchmark-driver /Users/naitoh/ghq/github.com/naitoh/rexml/benchmark/parse.yaml
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [arm64-darwin22]
Calculating -------------------------------------
                         before       after  before(YJIT)  after(YJIT)
                 dom     17.628      17.649        33.498       32.620 i/s -     100.000 times in 5.672656s 5.666057s 2.985297s 3.065603s
                 sax     25.255      25.388        48.751       48.927 i/s -     100.000 times in 3.959536s 3.938845s 2.051240s 2.043874s
                pull     29.061      29.050        61.483       61.910 i/s -     100.000 times in 3.441025s 3.442284s 1.626476s 1.615253s
              stream     28.401      29.467        55.235       60.095 i/s -     100.000 times in 3.520992s 3.393656s 1.810444s 1.664032s

Comparison:
                              dom
        before(YJIT):        33.5 i/s
         after(YJIT):        32.6 i/s - 1.03x  slower
               after:        17.6 i/s - 1.90x  slower
              before:        17.6 i/s - 1.90x  slower

                              sax
         after(YJIT):        48.9 i/s
        before(YJIT):        48.8 i/s - 1.00x  slower
               after:        25.4 i/s - 1.93x  slower
              before:        25.3 i/s - 1.94x  slower

                             pull
         after(YJIT):        61.9 i/s
        before(YJIT):        61.5 i/s - 1.01x  slower
              before:        29.1 i/s - 2.13x  slower
               after:        29.1 i/s - 2.13x  slower

                           stream
         after(YJIT):        60.1 i/s
        before(YJIT):        55.2 i/s - 1.09x  slower
               after:        29.5 i/s - 2.04x  slower
              before:        28.4 i/s - 2.12x  slower

```

- YJIT=ON : 0.97x - 1.09x faster
- YJIT=OFF : 0.99x - 1.03x faster
  • Loading branch information
naitoh committed Jun 23, 2024
1 parent f6bacc9 commit 7727211
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ module Private
GEDECL_PATTERN = "\\s+#{NAME}\\s+#{ENTITYDEF}\\s*>"
PEDECL_PATTERN = "\\s+(%)\\s+#{NAME}\\s+#{PEDEF}\\s*>"
ENTITYDECL_PATTERN = /(?:#{GEDECL_PATTERN})|(?:#{PEDECL_PATTERN})/um
CHARACTER_REFERENCES = /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/
DEFAULT_ENTITIES_PATTERNS = {}
default_entities = ['gt', 'lt', 'quot', 'apos', 'amp']
default_entities.each do |term|
DEFAULT_ENTITIES_PATTERNS[term] = /&#{term};/
end
end
private_constant :Private

Expand Down Expand Up @@ -506,7 +512,7 @@ def normalize( input, entities=nil, entity_filter=nil )
def unnormalize( string, entities=nil, filter=nil )
matches = string.scan( REFERENCE_RE )
return string if matches.size == 0
rv = string.gsub( /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/ ) {
rv = string.gsub( Private::CHARACTER_REFERENCES ) {
m=$1
m = "0#{m}" if m[0] == ?x
[Integer(m)].pack('U*')
Expand All @@ -517,15 +523,15 @@ def unnormalize( string, entities=nil, filter=nil )
unless filter and filter.include?(entity_reference)
entity_value = entity( entity_reference, entities )
if entity_value
re = /&#{entity_reference};/
re = Private::DEFAULT_ENTITIES_PATTERNS[entity_reference] || /&#{entity_reference};/
rv.gsub!( re, entity_value )
else
er = DEFAULT_ENTITIES[entity_reference]
rv.gsub!( er[0], er[2] ) if er
end
end
end
rv.gsub!( /&/, '&' )
rv.gsub!( Private::DEFAULT_ENTITIES_PATTERNS['amp'], '&' )
end
rv
end
Expand Down

0 comments on commit 7727211

Please sign in to comment.