Skip to content

Commit

Permalink
Optimize IOSource#read_until method
Browse files Browse the repository at this point in the history
## Why?
For PRE_DEFINED_TERM_PATTERNS terms, encode() is not required.

## Benchmark

```
RUBYLIB= BUNDLER_ORIG_RUBYLIB= /Users/naitoh/.rbenv/versions/3.3.4/bin/ruby -v -S benchmark-driver /Users/naitoh/ghq/github.com/naitoh/rexml/benchmark/parse.yaml
ruby 3.3.4 (2024-07-09 revision be1089c8ec) [arm64-darwin22]
Calculating -------------------------------------
                         before       after  before(YJIT)  after(YJIT)
                 dom     16.804      18.277        32.719       32.217 i/s -     100.000 times in 5.951028s 5.471237s 3.056310s 3.103960s
                 sax     25.975      26.537        45.543       49.366 i/s -     100.000 times in 3.849903s 3.768331s 2.195731s 2.025693s
                pull     29.198      30.201        54.224       57.690 i/s -     100.000 times in 3.424872s 3.311141s 1.844205s 1.733406s
              stream     29.091      28.778        52.944       54.882 i/s -     100.000 times in 3.437460s 3.474841s 1.888798s 1.822077s

Comparison:
                              dom
        before(YJIT):        32.7 i/s
         after(YJIT):        32.2 i/s - 1.02x  slower
               after:        18.3 i/s - 1.79x  slower
              before:        16.8 i/s - 1.95x  slower

                              sax
         after(YJIT):        49.4 i/s
        before(YJIT):        45.5 i/s - 1.08x  slower
               after:        26.5 i/s - 1.86x  slower
              before:        26.0 i/s - 1.90x  slower

                             pull
         after(YJIT):        57.7 i/s
        before(YJIT):        54.2 i/s - 1.06x  slower
               after:        30.2 i/s - 1.91x  slower
              before:        29.2 i/s - 1.98x  slower

                           stream
         after(YJIT):        54.9 i/s
        before(YJIT):        52.9 i/s - 1.04x  slower
              before:        29.1 i/s - 1.89x  slower
               after:        28.8 i/s - 1.91x  slower

```

- YJIT=ON : 1.02x - 1.08x faster
- YJIT=OFF : 0.98x - 1.08x faster
  • Loading branch information
naitoh committed Oct 6, 2024
1 parent 622011f commit 855c51d
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/rexml/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,11 @@ def read(term = nil, min_bytes = 1)
end

def read_until(term)
pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/
term = encode(term)
pattern = Private::PRE_DEFINED_TERM_PATTERNS[term]
if pattern.nil?
pattern = /#{Regexp.escape(term)}/
term = encode(term)
end
until str = @scanner.scan_until(pattern)
break if @source.nil?
break if @source.eof?
Expand Down

0 comments on commit 855c51d

Please sign in to comment.