Skip to content

Commit 6e75be6

Browse files
committedFeb 23, 2024
generator.c: Optimize by combining calls to fbuffer_append
1 parent 8720b46 commit 6e75be6

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed
 

‎ext/json/ext/generator/generator.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE in_string, bool out_
4343
unsigned long in_utf8_len = RSTRING_LEN(in_string);
4444
bool in_is_ascii_only = rb_enc_str_asciionly_p(in_string);
4545

46-
unsigned long pos;
46+
unsigned long beg = 0, pos;
4747

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

9090
/* JSON encoding */
9191
if (should_escape) {
92+
if (pos > beg)
93+
fbuffer_append(out_buffer, &in_utf8_str[beg], pos - beg);
94+
beg = pos + ch_len;
9295
switch (ch) {
9396
case '"': fbuffer_append(out_buffer, "\\\"", 2); break;
9497
case '\\': fbuffer_append(out_buffer, "\\\\", 2); break;
@@ -124,12 +127,12 @@ static void convert_UTF8_to_JSON(FBuffer *out_buffer, VALUE in_string, bool out_
124127
fbuffer_append(out_buffer, scratch, 12);
125128
}
126129
}
127-
} else {
128-
fbuffer_append(out_buffer, &in_utf8_str[pos], ch_len);
129130
}
130131

131132
pos += ch_len;
132133
}
134+
if (beg < in_utf8_len)
135+
fbuffer_append(out_buffer, &in_utf8_str[beg], in_utf8_len - beg);
133136
RB_GC_GUARD(in_string);
134137
}
135138

0 commit comments

Comments
 (0)