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

Improve performance of reading escaped string #948

Merged
merged 1 commit into from
Jan 2, 2025

Conversation

Watson1978
Copy link
Collaborator

This patch will improve the performance of reading escaped string by using a lookup table.

before after result
Oj.load 601.508 654.004 1.087x

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

# https://github.com/miloyip/nativejson-benchmark/blob/master/data/twitter.json
json = File.read('twitter.json')

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    59.000 i/100ms
Calculating -------------------------------------
      Oj.load compat    601.508 (± 1.2%) i/s    (1.66 ms/i) -      6.018k in  10.006395s

After

$ ruby json_load.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
      Oj.load compat    64.000 i/100ms
Calculating -------------------------------------
      Oj.load compat    654.004 (± 1.7%) i/s    (1.53 ms/i) -      6.592k in  10.082170s

This patch will improve the performance of reading escaped string
by using a lookup table.

−       | before  | after   | result
--      | --      | --      | --
Oj.load | 601.508 | 654.004 | 1.087x

### 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

# https://github.com/miloyip/nativejson-benchmark/blob/master/data/twitter.json
json = File.read('twitter.json')

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    59.000 i/100ms
Calculating -------------------------------------
      Oj.load compat    601.508 (± 1.2%) i/s    (1.66 ms/i) -      6.018k in  10.006395s
```

### After
```
$ ruby json_load.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
      Oj.load compat    64.000 i/100ms
Calculating -------------------------------------
      Oj.load compat    654.004 (± 1.7%) i/s    (1.53 ms/i) -      6.592k in  10.082170s
```
@Watson1978 Watson1978 marked this pull request as ready for review January 2, 2025 00:03
@ohler55
Copy link
Owner

ohler55 commented Jan 2, 2025

Nice. Arrigato.

@ohler55 ohler55 merged commit 7388e5b into ohler55:develop Jan 2, 2025
54 checks passed
@Watson1978 Watson1978 deleted the scan_string branch January 2, 2025 19:18
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