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

Sort the conditional statements #946

Merged
merged 1 commit into from
Dec 28, 2024
Merged

Conversation

Watson1978
Copy link
Collaborator

This patch will sort the conditional statements to reduce the conditional branches by skipping if statements that do not need to be executed depending on the value of first.

before after result
Oj.load 325.594k 341.356k 1.048x

Environment

  • Linux
    • Manjaro Linux x86_64
    • Kernel: 6.12.4-1-MANJARO
    • AMD Ryzen 9 8945HS
    • gcc version 14.2.1
    • Ruby 3.4.1

Code

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
  gem 'oj'
end

json = '{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}'

Benchmark.ips do |x|
  x.time = 10
  x.report('Oj.load compat') { Oj.load(json, mode: :compat) }
end

Before

$ ruby json_load.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
      Oj.load compat    32.352k i/100ms
Calculating -------------------------------------
      Oj.load compat    325.594k (± 1.8%) i/s    (3.07 μs/i) -      3.268M in  10.039265s

After

$ ruby json_load.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
      Oj.load compat    34.274k i/100ms
Calculating -------------------------------------
      Oj.load compat    341.356k (± 3.6%) i/s    (2.93 μs/i) -      3.427M in  10.056686s

This patch will sort the conditional statements to reduce the conditional branches
by skipping if statements that do not need to be executed depending on the value of `first`.

−       | before   | after    | result
--      | --       | --       | --
Oj.load | 325.594k | 341.356k | 1.048x

### Environment
- Linux
  - Manjaro Linux x86_64
  - Kernel: 6.12.4-1-MANJARO
  - AMD Ryzen 9 8945HS
  - gcc version 14.2.1
  - Ruby 3.4.1

### Code
```ruby
require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
  gem 'oj'
end

json = '{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}'

Benchmark.ips do |x|
  x.time = 10
  x.report('Oj.load compat') { Oj.load(json, mode: :compat) }
end
```

### Before
```
$ ruby json_load.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
      Oj.load compat    32.352k i/100ms
Calculating -------------------------------------
      Oj.load compat    325.594k (± 1.8%) i/s    (3.07 μs/i) -      3.268M in  10.039265s
```

### After
```
$ ruby json_load.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
      Oj.load compat    34.274k i/100ms
Calculating -------------------------------------
      Oj.load compat    341.356k (± 3.6%) i/s    (2.93 μs/i) -      3.427M in  10.056686s
```
@ohler55 ohler55 merged commit e14d18d into ohler55:develop Dec 28, 2024
60 checks passed
@ohler55
Copy link
Owner

ohler55 commented Dec 28, 2024

Thank you. Looks perfect.

@Watson1978 Watson1978 deleted the condition branch December 28, 2024 20:15
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