diff options
author | Watson <watson1978@gmail.com> | 2018-03-02 00:51:30 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-10-14 19:54:48 +0900 |
commit | 40724d7d10e9902fcdc4149326acbfe4dd95d3c7 (patch) | |
tree | b99d5aec6ce05b3e36e4869581b12a3979cc53c3 /ext | |
parent | 641136c4af89ec2bc1f2c80aeefc7a38f3bc48df (diff) |
[flori/json] Convert String encoding using `rb_str_encode()`
`rb_funcall` might be slightly heavy to call the Ruby method.
This patch will convert String encoding using `rb_str_encode()` instead of `rb_funcall()`.
## Before
```
$ ruby bench_json_generate.rb
Warming up --------------------------------------
json 78.000 i/100ms
Calculating -------------------------------------
json 789.781 (± 2.7%) i/s - 3.978k in 5.041043s
```
## After
```
$ ruby bench_json_generate.rb
Warming up --------------------------------------
json 129.000 i/100ms
Calculating -------------------------------------
json 1.300k (± 2.3%) i/s - 6.579k in 5.064656s
```
## Code
```
require 'json'
require 'benchmark/ips'
obj = []
1000.times do |i|
obj << {
"id" => i,
:age => 42,
}
end
Benchmark.ips do |x|
x.report "json" do |iter|
count = 0
while count < iter
JSON.generate(obj)
count += 1
end
end
end
```
https://github.com/flori/json/commit/9ae6d2969c
Diffstat (limited to 'ext')
-rw-r--r-- | ext/json/generator/generator.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c index cdf55c5ba4..e8b1476e31 100644 --- a/ext/json/generator/generator.c +++ b/ext/json/generator/generator.c @@ -810,7 +810,7 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S { fbuffer_append_char(buffer, '"'); #ifdef HAVE_RUBY_ENCODING_H - obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8); + obj = rb_str_encode(obj, CEncoding_UTF_8, 0, Qnil); #endif if (state->ascii_only) { convert_UTF8_to_JSON_ASCII(buffer, obj); |