Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce calls to Source#buffer(StringScanner#rest) #106

Merged
merged 1 commit into from
Jan 20, 2024

Conversation

naitoh
Copy link
Contributor

@naitoh naitoh commented Jan 19, 2024

Reduce calls to Source#buffer(StringScanner#rest)

Why

Source#buffer calling StringScanner#rest.
StringScanner#rest is slow.
Reduce calls to Source#buffer.

Benchmark

RUBYLIB= BUNDLER_ORIG_RUBYLIB= /Users/naitoh/.rbenv/versions/3.3.0/bin/ruby -v -S benchmark-driver /Users/naitoh/ghq/github.com/naitoh/rexml/benchmark/parse.yaml
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin22]
Calculating -------------------------------------
                         before       after  before(YJIT)  after(YJIT)
                 dom     10.639      10.985        16.213       16.221 i/s -     100.000 times in 9.399033s 9.103461s 6.167962s 6.164794s
                 sax     28.357      29.440        42.900       44.375 i/s -     100.000 times in 3.526479s 3.396688s 2.331024s 2.253511s
                pull     32.852      34.210        48.976       51.273 i/s -     100.000 times in 3.043965s 2.923140s 2.041816s 1.950344s
              stream     30.821      31.908        43.953       44.697 i/s -     100.000 times in 3.244539s 3.134020s 2.275172s 2.237310s

Comparison:
                              dom
         after(YJIT):        16.2 i/s
        before(YJIT):        16.2 i/s - 1.00x  slower
               after:        11.0 i/s - 1.48x  slower
              before:        10.6 i/s - 1.52x  slower

                              sax
         after(YJIT):        44.4 i/s
        before(YJIT):        42.9 i/s - 1.03x  slower
               after:        29.4 i/s - 1.51x  slower
              before:        28.4 i/s - 1.56x  slower

                             pull
         after(YJIT):        51.3 i/s
        before(YJIT):        49.0 i/s - 1.05x  slower
               after:        34.2 i/s - 1.50x  slower
              before:        32.9 i/s - 1.56x  slower

                           stream
         after(YJIT):        44.7 i/s
        before(YJIT):        44.0 i/s - 1.02x  slower
               after:        31.9 i/s - 1.40x  slower
              before:        30.8 i/s - 1.45x  slower

  • YJIT=ON : 1.00x - 1.05x faster
  • YJIT=OFF : 1.03x - 1.04x faster

@@ -349,8 +349,9 @@ def pull_event
end
begin
@source.read if @source.buffer.size<2
if @source.buffer[0] == ?<
if @source.buffer[1] == ?/
pattern = @source.buffer[0,2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is not a pattern. Can we use more suitable name such as buffer and next_data?

Can we remove [0,2]? I think that it's a needless method call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.
I see.

Comment on lines 351 to 352
@source.read if @source.buffer.size<2
if @source.buffer[0] == ?<
if @source.buffer[1] == ?/
pattern = @source.buffer[0,2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to reduce one more @source.buffer call.

pattern = @source.buffer
if pattern.size < 2
  @source.read
  pattern = @source.buffer
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked with this content.
Thank you!

[Why]
`Source#buffer` calling `StringScanner#rest`.
`StringScanner#rest` is slow.
Reduce calls to `Source#buffer`.

## Benchmark

```
RUBYLIB= BUNDLER_ORIG_RUBYLIB= /Users/naitoh/.rbenv/versions/3.3.0/bin/ruby -v -S benchmark-driver /Users/naitoh/ghq/github.com/naitoh/rexml/benchmark/parse.yaml
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin22]
Calculating -------------------------------------
                         before       after  before(YJIT)  after(YJIT)
                 dom     10.639      10.985        16.213       16.221 i/s -     100.000 times in 9.399033s 9.103461s 6.167962s 6.164794s
                 sax     28.357      29.440        42.900       44.375 i/s -     100.000 times in 3.526479s 3.396688s 2.331024s 2.253511s
                pull     32.852      34.210        48.976       51.273 i/s -     100.000 times in 3.043965s 2.923140s 2.041816s 1.950344s
              stream     30.821      31.908        43.953       44.697 i/s -     100.000 times in 3.244539s 3.134020s 2.275172s 2.237310s

Comparison:
                              dom
         after(YJIT):        16.2 i/s
        before(YJIT):        16.2 i/s - 1.00x  slower
               after:        11.0 i/s - 1.48x  slower
              before:        10.6 i/s - 1.52x  slower

                              sax
         after(YJIT):        44.4 i/s
        before(YJIT):        42.9 i/s - 1.03x  slower
               after:        29.4 i/s - 1.51x  slower
              before:        28.4 i/s - 1.56x  slower

                             pull
         after(YJIT):        51.3 i/s
        before(YJIT):        49.0 i/s - 1.05x  slower
               after:        34.2 i/s - 1.50x  slower
              before:        32.9 i/s - 1.56x  slower

                           stream
         after(YJIT):        44.7 i/s
        before(YJIT):        44.0 i/s - 1.02x  slower
               after:        31.9 i/s - 1.40x  slower
              before:        30.8 i/s - 1.45x  slower

```

- YJIT=ON : 1.00x - 1.05x faster
- YJIT=OFF : 1.03x - 1.04x faster
@naitoh naitoh force-pushed the reduce_call_string_scanner_rest branch from 0a51890 to 98835dd Compare January 20, 2024 12:46
@naitoh naitoh requested a review from kou January 20, 2024 12:51
@kou kou merged commit 83ca5c4 into ruby:master Jan 20, 2024
39 checks passed
@kou
Copy link
Member

kou commented Jan 20, 2024

Thanks!

@naitoh naitoh deleted the reduce_call_string_scanner_rest branch January 20, 2024 22:11
@naitoh
Copy link
Contributor Author

naitoh commented Jan 20, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants