Skip to content

Commit cfbe356

Browse files
samyronbyroot
andcommitted
Force ensure_valid_encoding to be inlined.
And move the encoding convertion logic in another function with NOINLINE. The overwelming majority of strings are correctly encoded, so we want to inline the very cheap check, however we don't want to inline the much larger piece of code required to re-encode the string. Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
1 parent 4ef7a45 commit cfbe356

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

ext/json/ext/generator/generator.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,8 @@ static inline bool valid_json_string_p(VALUE str)
842842
return false;
843843
}
844844

845-
static inline VALUE ensure_valid_encoding(struct generate_json_data *data, VALUE str, bool as_json_called, bool is_key)
845+
NOINLINE(static) VALUE convert_invalid_encoding(struct generate_json_data *data, VALUE str, bool as_json_called, bool is_key)
846846
{
847-
if (RB_LIKELY(valid_json_string_p(str))) {
848-
return str;
849-
}
850-
851847
if (!as_json_called && data->state->strict && RTEST(data->state->as_json)) {
852848
VALUE coerced_str = json_call_as_json(data->state, str, Qfalse);
853849
if (coerced_str != str) {
@@ -883,6 +879,16 @@ static inline VALUE ensure_valid_encoding(struct generate_json_data *data, VALUE
883879
return rb_rescue(encode_json_string_try, str, encode_json_string_rescue, str);
884880
}
885881

882+
ALWAYS_INLINE(static) VALUE ensure_valid_encoding(struct generate_json_data *data, VALUE str, bool as_json_called, bool is_key)
883+
{
884+
if (RB_LIKELY(valid_json_string_p(str))) {
885+
return str;
886+
}
887+
else {
888+
return convert_invalid_encoding(data, str, as_json_called, is_key);
889+
}
890+
}
891+
886892
static void raw_generate_json_string(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
887893
{
888894
fbuffer_append_char(buffer, '"');

0 commit comments

Comments
 (0)