Skip to content

Commit

Permalink
generator.c: Optimize by combining calls to fbuffer_append
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeShu committed Feb 23, 2024
1 parent 8720b46 commit 6e75be6
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE in_string, bool out_
unsigned long in_utf8_len = RSTRING_LEN(in_string);
bool in_is_ascii_only = rb_enc_str_asciionly_p(in_string);

unsigned long pos;
unsigned long beg = 0, pos;

for (pos = 0; pos < in_utf8_len;) {
uint32_t ch;
Expand Down Expand Up @@ -89,6 +89,9 @@ static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE in_string, bool out_

/* JSON encoding */
if (should_escape) {
if (pos > beg)
fbuffer_append(out_buffer, &in_utf8_str[beg], pos - beg);
beg = pos + ch_len;
switch (ch) {
case '"': fbuffer_append(out_buffer, "\\\"", 2); break;
case '\\': fbuffer_append(out_buffer, "\\\\", 2); break;
Expand Down Expand Up @@ -124,12 +127,12 @@ static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE in_string, bool out_
fbuffer_append(out_buffer, scratch, 12);
}
}
} else {
fbuffer_append(out_buffer, &in_utf8_str[pos], ch_len);
}

pos += ch_len;
}
if (beg < in_utf8_len)
fbuffer_append(out_buffer, &in_utf8_str[beg], in_utf8_len - beg);
RB_GC_GUARD(in_string);
}

Expand Down

0 comments on commit 6e75be6

Please sign in to comment.