-
-
Notifications
You must be signed in to change notification settings - Fork 254
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
Use rb_utf8_str_new/rb_utf8_str_new_cstr to create UTF8 string #950
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This patch will use rb_utf8_str_new/rb_utf8_str_new_cstr API to create UTF8 string. Seems it has slightly better performance. − | before | after | result -- | -- | -- | -- Oj.load | 654.004 | 670.792 | 1.025x ### 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 64.000 i/100ms Calculating ------------------------------------- Oj.load compat 654.004 (± 1.7%) i/s (1.53 ms/i) - 6.592k in 10.082170s ``` ### After ``` $ ruby json_load.rb Warming up -------------------------------------- Oj.load compat 65.000 i/100ms Calculating ------------------------------------- Oj.load compat 670.792 (± 1.6%) i/s (1.49 ms/i) - 6.760k in 10.080319s ```
Watson1978
commented
Jan 4, 2025
volatile VALUE rs = rb_str_new(sw->sw.out.buf, size); | ||
|
||
// Oddly enough, when pushing ASCII characters with UTF-8 encoding or | ||
// even ASCII-8BIT does not change the output encoding. Pushing any | ||
// non-ASCII no matter what the encoding changes the output encoding | ||
// to ASCII-8BIT if it the string is not forced to UTF-8 here. | ||
rs = oj_encode(rs); | ||
volatile VALUE rs = rb_utf8_str_new(sw->sw.out.buf, size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there should be no problem so that the tests was passed added 73595e6
Hmm, It's still lagging behind json gem... require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'oj'
gem 'json'
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('JSON.parse') { JSON.parse(json) }
x.report('Oj.load compat') { Oj.load(json, mode: :compat) }
x.compare!
end
|
ohler55
approved these changes
Jan 5, 2025
The biggest performance bottleneck is the call to the objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch will use rb_utf8_str_new/rb_utf8_str_new_cstr API to create UTF8 string.
Seems it has slightly better performance.
Environment
Code
Before
After