Skip to content

Commit 91a24ec

Browse files
Watson1978flori
authored andcommitted
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 ```
1 parent c34d01f commit 91a24ec

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

ext/json/ext/generator/generator.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
237237
int escape_len;
238238
unsigned char c;
239239
char buf[6] = { '\\', 'u' };
240+
int ascii_only = rb_enc_str_asciionly_p(string);
240241

241242
for (start = 0, end = 0; end < len;) {
242243
p = ptr + end;
@@ -281,14 +282,17 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
281282
break;
282283
default:
283284
{
284-
unsigned short clen = trailingBytesForUTF8[c] + 1;
285-
if (end + clen > len) {
286-
rb_raise(rb_path2class("JSON::GeneratorError"),
287-
"partial character in source, but hit end");
288-
}
289-
if (!isLegalUTF8((UTF8 *) p, clen)) {
290-
rb_raise(rb_path2class("JSON::GeneratorError"),
291-
"source sequence is illegal/malformed utf-8");
285+
unsigned short clen = 1;
286+
if (!ascii_only) {
287+
clen += trailingBytesForUTF8[c];
288+
if (end + clen > len) {
289+
rb_raise(rb_path2class("JSON::GeneratorError"),
290+
"partial character in source, but hit end");
291+
}
292+
if (!isLegalUTF8((UTF8 *) p, clen)) {
293+
rb_raise(rb_path2class("JSON::GeneratorError"),
294+
"source sequence is illegal/malformed utf-8");
295+
}
292296
}
293297
end += clen;
294298
}

0 commit comments

Comments
 (0)