Skip to content

Conversation

kirs
Copy link
Contributor

@kirs kirs commented Mar 29, 2021

This attempts to solve #2299.

Not relying on exception flow shows much better perf in interpreter mode.

require 'benchmark/ips'

puts RUBY_DESCRIPTION

struct = Struct.new(:foo).new(14)

Benchmark.ips do |x|
  x.report('found') do
      struct[:foo]
  end

  x.report('not-found') do
    struct.dig(:bar)
  end

  x.compare!
end

Before

$ jt ruby demo.rb
Using Interpreted TruffleRuby: mxbuild/truffleruby-jvm
$ /src/github.com/shopify/truffleruby/mxbuild/truffleruby-jvm/languages/ruby/bin/ruby \
  --experimental-options \
  --core-load-path=/src/github.com/shopify/truffleruby/src/main/ruby/truffleruby \
  demo.rb
[ruby] WARNING gem paths: /usr/local/bundle are not marked as installed by TruffleRuby. They might belong to another Ruby implementation and break unexpectedly. Configure your Ruby manager to use TruffleRuby, or `unset GEM_HOME GEM_PATH`. See https://github.com/oracle/truffleruby/blob/master/doc/user/ruby-managers.md
truffleruby 21.1.0-dev-2855d7c9, like ruby 2.7.2, Interpreted JVM [x86_64-linux]
Warming up --------------------------------------
               found    57.241k i/100ms
           not-found     5.873k i/100ms
Calculating -------------------------------------
               found    541.688k (±13.0%) i/s -      2.690M in   5.073909s
           not-found     56.472k (±13.2%) i/s -    281.904k in   5.079920s

Comparison:
               found:   541687.5 i/s
           not-found:    56472.2 i/s - 9.59x  (± 0.00) slower

After

$ jt ruby demo.rb
Using Interpreted TruffleRuby: mxbuild/truffleruby-jvm
$ /src/github.com/shopify/truffleruby/mxbuild/truffleruby-jvm/languages/ruby/bin/ruby \
  --experimental-options \
  --core-load-path=/src/github.com/shopify/truffleruby/src/main/ruby/truffleruby \
  demo.rb
[ruby] WARNING gem paths: /usr/local/bundle are not marked as installed by TruffleRuby. They might belong to another Ruby implementation and break unexpectedly. Configure your Ruby manager to use TruffleRuby, or `unset GEM_HOME GEM_PATH`. See https://github.com/oracle/truffleruby/blob/master/doc/user/ruby-managers.md
truffleruby 21.1.0-dev-2855d7c9, like ruby 2.7.2, Interpreted JVM [x86_64-linux]
Warming up --------------------------------------
               found    57.851k i/100ms
           not-found    52.936k i/100ms
Calculating -------------------------------------
               found    546.247k (± 3.8%) i/s -      2.777M in   5.091750s
           not-found    536.844k (± 2.0%) i/s -      2.700M in   5.030927s

Comparison:
               found:   546247.3 i/s
           not-found:   536844.3 i/s - same-ish: difference falls within error

@kirs kirs force-pushed the struct-no-exception branch from ef51ff6 to ec4c313 Compare March 29, 2021 22:31
@chrisseaton
Copy link
Collaborator

Thanks for this work. 10x slower for missing is another example of a 'performance cliff'. It's hard to explain to users why a missing field is an order of magnitude worse when it seems to be conceptually the same complexity.

Copy link
Member

@eregon eregon left a comment

Choose a reason for hiding this comment

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

Awesome, thank you for the PR!

@eregon eregon added the in-ci The PR is being tested in CI. Do not push new commits. label Mar 30, 2021
@eregon eregon added this to the 21.2.0 milestone Mar 30, 2021
@eregon eregon linked an issue Mar 30, 2021 that may be closed by this pull request
@chrisseaton chrisseaton added the shopify Pull requests from Shopify label Mar 30, 2021
graalvmbot pushed a commit that referenced this pull request Apr 1, 2021
@graalvmbot graalvmbot merged commit ec4c313 into oracle:master Apr 1, 2021
@chrisseaton chrisseaton deleted the struct-no-exception branch August 30, 2021 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-ci The PR is being tested in CI. Do not push new commits. oca-signed shopify Pull requests from Shopify
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Struct#dig uses exceptions for control-flow
4 participants