diff options
author | Yusuke Endoh <[email protected]> | 2023-12-27 16:32:47 +0900 |
---|---|---|
committer | git <[email protected]> | 2024-10-17 08:54:48 +0000 |
commit | 233f63c7fb491bddfbd5b201ba5ef1bb212afdb0 (patch) | |
tree | a09763f34252b81d2704974480f1eaa35d8c4350 /ext/json | |
parent | 0b4257efa3bd01fceefa51f2eef14d2c38e8fab8 (diff) |
[ruby/json] Use `RB_ENCODING_GET` instead of `rb_enc_get` to improve performance
This speeds up `JSON.generate` by about 12% in a benchmark.
https://github.com/ruby/json/commit/4329e30826
Diffstat (limited to 'ext/json')
-rw-r--r-- | ext/json/generator/generator.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c index 487fc68f4d..7d40ba4dc1 100644 --- a/ext/json/generator/generator.c +++ b/ext/json/generator/generator.c @@ -742,17 +742,19 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St fbuffer_append_char(buffer, ']'); } -static int enc_utf8_compatible_p(rb_encoding *enc) +static int usascii_encindex, utf8_encindex; + +static int enc_utf8_compatible_p(int enc_idx) { - if (enc == rb_usascii_encoding()) return 1; - if (enc == rb_utf8_encoding()) return 1; + if (enc_idx == usascii_encindex) return 1; + if (enc_idx == utf8_encindex) return 1; return 0; } static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj) { fbuffer_append_char(buffer, '"'); - if (!enc_utf8_compatible_p(rb_enc_get(obj))) { + if (!enc_utf8_compatible_p(RB_ENCODING_GET(obj))) { obj = rb_str_export_to_enc(obj, rb_utf8_encoding()); } convert_UTF8_to_JSON(buffer, obj, state->ascii_only, state->script_safe); @@ -1479,4 +1481,7 @@ void Init_generator(void) i_match = rb_intern("match"); i_keys = rb_intern("keys"); i_dup = rb_intern("dup"); + + usascii_encindex = rb_usascii_encindex(); + utf8_encindex = rb_utf8_encindex(); } |