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

Speedup #generate_json by using case/when/end #674

Merged
merged 2 commits into from
Nov 4, 2024

Conversation

eregon
Copy link
Member

@eregon eregon commented Nov 1, 2024

$ ruby -Ilib:ext benchmark/standalone.rb dump pure JSON::Pure::Generator
truffleruby 24.2.0-dev-07b978e4, like ruby 3.2.4, Oracle GraalVM JVM [x86_64-linux] Calculating -------------------------------------
      JSON.dump(obj)      6.426k (± 5.9%) i/s  (155.62 μs/i) -     32.395k in   5.064479s
      JSON.dump(obj)      6.380k (± 7.4%) i/s  (156.73 μs/i) -     31.806k in   5.021304s
      JSON.dump(obj)      6.276k (±10.5%) i/s  (159.33 μs/i) -     31.217k in   5.060762s
      JSON.dump(obj)      6.450k (± 7.0%) i/s  (155.05 μs/i) -     32.395k in   5.059538s
      JSON.dump(obj)      6.413k (± 6.2%) i/s  (155.93 μs/i) -     32.395k in   5.081573s
  • After:
$ ruby -Ilib:ext benchmark/standalone.rb dump pure
JSON::Pure::Generator
truffleruby 24.2.0-dev-07b978e4, like ruby 3.2.4, Oracle GraalVM JVM [x86_64-linux]
Calculating -------------------------------------
      JSON.dump(obj)      8.237k (± 5.0%) i/s  (121.41 μs/i) -     41.600k in   5.069507s
      JSON.dump(obj)      8.179k (± 5.1%) i/s  (122.26 μs/i) -     40.768k in   5.002035s
      JSON.dump(obj)      8.147k (± 7.9%) i/s  (122.74 μs/i) -     40.768k in   5.044840s
      JSON.dump(obj)      8.137k (± 6.9%) i/s  (122.90 μs/i) -     40.768k in   5.048690s
      JSON.dump(obj)      8.112k (±10.2%) i/s  (123.27 μs/i) -     39.936k in   5.023502s

@byroot
Copy link
Member

byroot commented Nov 2, 2024

  • The only case where an exact class check is needed so far is for String

That's the one case where we had a failing test for it, but does that mean we should special case it?

I'm open to say only String subclasses have that behavior, but then all 3 implementations should match behavior. Could you add some tests to make sure we converge them?

Also I'm very surprised .class is this expensive on Truffle, I wouldn't have expected it.

casperisfine pushed a commit to casperisfine/json that referenced this pull request Nov 4, 2024
Ref: ruby#674
Ref: ruby#668

The behavior on such case it quite unclear, the goal here is to
figure out whatever was the behavior on Cext version of `json 2.7.0`
and get all implementations to converge.

We can then decide to make them all behave differently if we so wish.
casperisfine pushed a commit to casperisfine/json that referenced this pull request Nov 4, 2024
Ref: ruby#674
Ref: ruby#668

The behavior on such case it quite unclear, the goal here is to
figure out whatever was the behavior on Cext version of `json 2.7.0`
and get all implementations to converge.

We can then decide to make them all behave differently if we so wish.
casperisfine pushed a commit to casperisfine/json that referenced this pull request Nov 4, 2024
Ref: ruby#674
Ref: ruby#668

The behavior on such case it quite unclear, the goal here is to
figure out whatever was the behavior on Cext version of `json 2.7.0`
and get all implementations to converge.

We can then decide to make them all behave differently if we so wish.
@eregon
Copy link
Member Author

eregon commented Nov 4, 2024

I'll rebase and see if the added tests fail on this PR or if they are fine.

* if/elsif comparing `obj.class` is significantly slower:
  ruby#668 (comment)
* The only case where an exact class check is needed so far is for String (ruby#667).
* Before: $ ruby -Ilib:ext benchmark/standalone.rb dump pure
JSON::Pure::Generator
truffleruby 24.2.0-dev-07b978e4, like ruby 3.2.4, Oracle GraalVM JVM [x86_64-linux]
Calculating -------------------------------------
      JSON.dump(obj)      6.426k (± 5.9%) i/s  (155.62 μs/i) -     32.395k in   5.064479s
      JSON.dump(obj)      6.380k (± 7.4%) i/s  (156.73 μs/i) -     31.806k in   5.021304s
      JSON.dump(obj)      6.276k (±10.5%) i/s  (159.33 μs/i) -     31.217k in   5.060762s
      JSON.dump(obj)      6.450k (± 7.0%) i/s  (155.05 μs/i) -     32.395k in   5.059538s
      JSON.dump(obj)      6.413k (± 6.2%) i/s  (155.93 μs/i) -     32.395k in   5.081573s
* After: $ ruby -Ilib:ext benchmark/standalone.rb dump pure
JSON::Pure::Generator
truffleruby 24.2.0-dev-07b978e4, like ruby 3.2.4, Oracle GraalVM JVM [x86_64-linux]
Calculating -------------------------------------
      JSON.dump(obj)      8.237k (± 5.0%) i/s  (121.41 μs/i) -     41.600k in   5.069507s
      JSON.dump(obj)      8.179k (± 5.1%) i/s  (122.26 μs/i) -     40.768k in   5.002035s
      JSON.dump(obj)      8.147k (± 7.9%) i/s  (122.74 μs/i) -     40.768k in   5.044840s
      JSON.dump(obj)      8.137k (± 6.9%) i/s  (122.90 μs/i) -     40.768k in   5.048690s
      JSON.dump(obj)      8.112k (±10.2%) i/s  (123.27 μs/i) -     39.936k in   5.023502s
@eregon eregon force-pushed the faster-checks-for-generate_json branch from ac44975 to 82d21f0 Compare November 4, 2024 13:20
@eregon
Copy link
Member Author

eregon commented Nov 4, 2024

Tests seem to pass after rebase from a local test run.

Copy link
Member

@byroot byroot left a comment

Choose a reason for hiding this comment

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

If tests pass, then that's good for me.

@byroot byroot merged commit 4af700e into ruby:master Nov 4, 2024
36 checks passed
casperisfine pushed a commit to Shopify/ruby that referenced this pull request Nov 5, 2024
…es subclasses

Ref: ruby/json#674
Ref: ruby/json#668

The behavior on such case it quite unclear, the goal here is to
figure out whatever was the behavior on Cext version of `json 2.7.0`
and get all implementations to converge.

We can then decide to make them all behave differently if we so wish.

ruby/json@614921dcef
casperisfine pushed a commit to Shopify/ruby that referenced this pull request Nov 5, 2024
…es subclasses

Ref: ruby/json#674
Ref: ruby/json#668

The behavior on such case it quite unclear, the goal here is to
figure out whatever was the behavior on Cext version of `json 2.7.0`
and get all implementations to converge.

We can then decide to make them all behave differently if we so wish.

ruby/json@614921dcef
byroot added a commit to ruby/ruby that referenced this pull request Nov 5, 2024
…es subclasses

Ref: ruby/json#674
Ref: ruby/json#668

The behavior on such case it quite unclear, the goal here is to
figure out whatever was the behavior on Cext version of `json 2.7.0`
and get all implementations to converge.

We can then decide to make them all behave differently if we so wish.

ruby/json@614921dcef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants