Use memcpy() of standard C library#742
Merged
ohler55 merged 1 commit intoohler55:developfrom Jan 15, 2022
Merged
Conversation
Ruby has its own implementation of ruby_nonempty_memcpy(), which is used when memcpy() is called.
```c
static inline void *
ruby_nonempty_memcpy(void *dest, const void *src, size_t n)
{
if (n) {
return memcpy(dest, src, n);
}
else {
return dest;
}
}
RBIMPL_SYMBOL_EXPORT_END()
#undef memcpy
#define memcpy ruby_nonempty_memcpy
```
https://github.com/ruby/ruby/blob/master/include/ruby/internal/memory.h
It has an unnecessary `if` statement and it has some overhead.
Similar: ohler55#735
− | before | after | result
-- | -- | -- | --
Oj.dump (macOS) | 7.839k | 8.000k | 1.021x
Oj.dump (Linux) | 9.140k | 10.043k | 1.099x
### Environment
- macOS
- macOS 12.1
- Apple M1 Max
- Apple clang version 13.0.0 (clang-1300.0.29.30)
- Ruby 3.1.0
- Linux
- Zorin OS 16
- AMD Ryzen 7 5700G
- gcc version 11.1.0
- Ruby 3.1.0
### macOS
#### Before
```
Warming up --------------------------------------
Oj.dump 783.000 i/100ms
Calculating -------------------------------------
Oj.dump 7.839k (± 0.9%) i/s - 118.233k in 15.084888s
```
#### After
```
Warming up --------------------------------------
Oj.dump 803.000 i/100ms
Calculating -------------------------------------
Oj.dump 8.000k (± 0.8%) i/s - 120.450k in 15.057950s
```
### Linux
#### Before
```
Warming up --------------------------------------
Oj.dump 887.000 i/100ms
Calculating -------------------------------------
Oj.dump 9.140k (± 0.9%) i/s - 137.485k in 15.043569s
```
#### After
```
Warming up --------------------------------------
Oj.dump 972.000 i/100ms
Calculating -------------------------------------
Oj.dump 10.043k (± 0.9%) i/s - 150.660k in 15.002236s
```
### Test code
```ruby
require 'benchmark/ips'
require 'oj'
data = (0..10000).to_a
Benchmark.ips do |x|
x.time = 15
x.report('Oj.dump') { Oj.dump(data, mode: :compat) }
end
```
Collaborator
Author
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.

Ruby has its own implementation of ruby_nonempty_memcpy(), which is used when memcpy() is called.
https://github.com/ruby/ruby/blob/master/include/ruby/internal/memory.h
It has an unnecessary
ifstatement and it has some overhead.Similar: #735
Environment
macOS
Before
After
Linux
Before
After
Test code