mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[flori/json] Does not check whether illegal utf-8 if string has ascii only.
## Before
```
$ ruby bench_json_generate.rb
Warming up --------------------------------------
json 25.000 i/100ms
Calculating -------------------------------------
json 250.478 (± 4.8%) i/s - 1.250k in 5.002238s
```
## After
```
$ ruby bench_json_generate.rb
Warming up --------------------------------------
json 32.000 i/100ms
Calculating -------------------------------------
json 360.652 (± 3.6%) i/s - 1.824k in 5.064511s
```
## Test code
```
require 'json'
require 'benchmark/ips'
obj = []
1000.times do |i|
obj << {
:string => "x" * 100,
:utf8 => "あ" * 100
}
end
Benchmark.ips do |x|
x.report "json" do |iter|
count = 0
while count < iter
JSON.generate(obj)
count += 1
end
end
end
```
91a24ecac3
This commit is contained in:
parent
d9e50fcbeb
commit
641136c4af
1 changed files with 12 additions and 8 deletions
|
@ -237,6 +237,7 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
|
||||||
int escape_len;
|
int escape_len;
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
char buf[6] = { '\\', 'u' };
|
char buf[6] = { '\\', 'u' };
|
||||||
|
int ascii_only = rb_enc_str_asciionly_p(string);
|
||||||
|
|
||||||
for (start = 0, end = 0; end < len;) {
|
for (start = 0, end = 0; end < len;) {
|
||||||
p = ptr + end;
|
p = ptr + end;
|
||||||
|
@ -281,7 +282,9 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
unsigned short clen = trailingBytesForUTF8[c] + 1;
|
unsigned short clen = 1;
|
||||||
|
if (!ascii_only) {
|
||||||
|
clen += trailingBytesForUTF8[c];
|
||||||
if (end + clen > len) {
|
if (end + clen > len) {
|
||||||
rb_raise(rb_path2class("JSON::GeneratorError"),
|
rb_raise(rb_path2class("JSON::GeneratorError"),
|
||||||
"partial character in source, but hit end");
|
"partial character in source, but hit end");
|
||||||
|
@ -290,6 +293,7 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
|
||||||
rb_raise(rb_path2class("JSON::GeneratorError"),
|
rb_raise(rb_path2class("JSON::GeneratorError"),
|
||||||
"source sequence is illegal/malformed utf-8");
|
"source sequence is illegal/malformed utf-8");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
end += clen;
|
end += clen;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue