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

Simplify the loop for parsing Integer string #792

Merged
merged 1 commit into from
Jul 23, 2022

Conversation

Watson1978
Copy link
Collaborator

There are several branches in the loop that parse Integer string.
In order to speed up parsing, this patch will move unwanted branches out of the loop.

before after result
integer 4.860M 5.088M 1.045x
float 4.845M 5.084M 1.049x

Environment

  • Linux
    • Manjaro Linux x86_64
    • Kernel: 5.18.12-3-MANJARO
    • AMD Ryzen 7 5700G
    • gcc version 12.1.0
    • Ruby 3.1.2

Before

Warming up --------------------------------------
     Oj.load integer   488.037k i/100ms
       Oj.load float   485.863k i/100ms
Calculating -------------------------------------
     Oj.load integer      4.860M (± 0.1%) i/s -     48.804M in  10.042684s
       Oj.load float      4.845M (± 0.3%) i/s -     48.586M in  10.028592s

After

Warming up --------------------------------------
     Oj.load integer   508.044k i/100ms
       Oj.load float   508.281k i/100ms
Calculating -------------------------------------
     Oj.load integer      5.088M (± 0.2%) i/s -     51.312M in  10.085310s
       Oj.load float      5.084M (± 0.2%) i/s -     51.336M in  10.098332s

Test code

require 'benchmark/ips'
require 'oj'

Benchmark.ips do |x|
  x.warmup = 3
  x.time = 10

  json_integer = Oj.dump(2 ** 30)
  x.report('Oj.load integer') do |times|
    i = 0
    while i < times
      Oj.load(json_integer)
      i += 1
    end
  end

  json_float = Oj.dump(1234.123456789)
  x.report('Oj.load float') do |times|
    i = 0
    while i < times
      Oj.load(json_integer)
      i += 1
    end
  end
end

There are several branches in the loop that parse Integer string.
In order to speed up parsing, this patch will move unwanted branches out of the loop.

−       | before | after  | result
--       | --     | --     | --
integer  | 4.860M | 5.088M | 1.045x
float    | 4.845M | 5.084M | 1.049x

### Environment
- Linux
  - Manjaro Linux x86_64
  - Kernel: 5.18.12-3-MANJARO
  - AMD Ryzen 7 5700G
  - gcc version 12.1.0
  - Ruby 3.1.2

### Before
```
Warming up --------------------------------------
     Oj.load integer   488.037k i/100ms
       Oj.load float   485.863k i/100ms
Calculating -------------------------------------
     Oj.load integer      4.860M (± 0.1%) i/s -     48.804M in  10.042684s
       Oj.load float      4.845M (± 0.3%) i/s -     48.586M in  10.028592s
```

### After
```
Warming up --------------------------------------
     Oj.load integer   508.044k i/100ms
       Oj.load float   508.281k i/100ms
Calculating -------------------------------------
     Oj.load integer      5.088M (± 0.2%) i/s -     51.312M in  10.085310s
       Oj.load float      5.084M (± 0.2%) i/s -     51.336M in  10.098332s
```

### Test code
```ruby
require 'benchmark/ips'
require 'oj'

Benchmark.ips do |x|
  x.warmup = 3
  x.time = 10

  json_integer = Oj.dump(2 ** 30)
  x.report('Oj.load integer') do |times|
    i = 0
    while i < times
      Oj.load(json_integer)
      i += 1
    end
  end

  json_float = Oj.dump(1234.123456789)
  x.report('Oj.load float') do |times|
    i = 0
    while i < times
      Oj.load(json_integer)
      i += 1
    end
  end
end
```
@ohler55 ohler55 merged commit 566847e into ohler55:develop Jul 23, 2022
@Watson1978 Watson1978 deleted the read_num branch July 24, 2022 05:36
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