Improve performance of Oj.dump with compat/rails mode#675
Merged
ohler55 merged 1 commit intoohler55:developfrom Aug 2, 2021
Merged
Improve performance of Oj.dump with compat/rails mode#675ohler55 merged 1 commit intoohler55:developfrom
Oj.dump with compat/rails mode#675ohler55 merged 1 commit intoohler55:developfrom
Conversation
Oj.dumpwith compat/rails modeOj.dump with compat/rails mode
Collaborator
Author
|
I retrieved a benchmark result on Intel Mac.
Env
BeforeAfter |
This patch introduces `oj_hash_has_key()` (same as rb_hash_has_key)
to reduce `rb_funcall()` calling because it has a overhead.
This patch will improve `Oj.dump` performance as following.
− | before | after | result
-- | -- | -- | --
Oj.dump | 1.949M | 1.966M | −
Oj.dump (compat) | 850.154k | 1.198M | 1.41x
Oj.dump (rails) | 657.383k | 840.051k | 1.28x
### Environment
- MacBook Air (M1, 2020)
- macOS 12.0 beta 3
- Apple M1
- Ruby 3.0.2
### Before
```
Warming up --------------------------------------
Oj.dump 198.379k i/100ms
Oj.dump (compat) 86.466k i/100ms
Oj.dump (rails) 66.760k i/100ms
Calculating -------------------------------------
Oj.dump 1.949M (± 0.3%) i/s - 9.919M in 5.088487s
Oj.dump (compat) 850.154k (± 0.3%) i/s - 4.323M in 5.085367s
Oj.dump (rails) 657.383k (± 0.4%) i/s - 3.338M in 5.077802s
```
### After
```
Warming up --------------------------------------
Oj.dump 198.297k i/100ms
Oj.dump (compat) 120.402k i/100ms
Oj.dump (rails) 84.204k i/100ms
Calculating -------------------------------------
Oj.dump 1.966M (± 0.3%) i/s - 9.915M in 5.044305s
Oj.dump (compat) 1.198M (± 0.2%) i/s - 6.020M in 5.026524s
Oj.dump (rails) 840.051k (± 0.1%) i/s - 4.210M in 5.011848s
```
### Test code
```ruby
require 'benchmark/ips'
require 'oj'
data = {
'short_string': 'a' * 50,
'long_string': 'b' * 255,
'utf8_string': 'あいうえお' * 10
}
Benchmark.ips do |x|
x.report('Oj.dump') { Oj.dump(data) }
x.report('Oj.dump (compat)') { Oj.dump(data, mode: :compat) }
x.report('Oj.dump (rails)') { Oj.dump(data, mode: :rails) }
end
```
Collaborator
Author
|
@ohler55 I would appreciate any feedback about this pull request. Thanks. |
Owner
|
Looks very reasonable to me. Thank you. |
Owner
|
I would not expect a significant change on non-trivial objects. For small it does help. |
This file contains hidden or 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
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 introduces
oj_hash_has_key()(same as rb_hash_has_key)to reduce
rb_funcall()calling because it has a overhead.This patch will improve
Oj.dumpperformance as following.Environment
Before
After
Test code