Skip to content

Commit c6deafb

Browse files
authored
Merge pull request #842 from byroot/opt-repeat
Optimize `fbuffer_append_str_repeat`
2 parents 36fefff + f0dda86 commit c6deafb

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

ext/json/ext/fbuffer/fbuffer.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,17 @@ static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
177177
}
178178
}
179179

180-
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
180+
static inline void fbuffer_append_reserved(FBuffer *fb, const char *newstr, unsigned long len)
181+
{
182+
MEMCPY(fb->ptr + fb->len, newstr, char, len);
183+
fbuffer_consumed(fb, len);
184+
}
185+
186+
static inline void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
181187
{
182188
if (len > 0) {
183189
fbuffer_inc_capa(fb, len);
184-
MEMCPY(fb->ptr + fb->len, newstr, char, len);
185-
fbuffer_consumed(fb, len);
190+
fbuffer_append_reserved(fb, newstr, len);
186191
}
187192
}
188193

@@ -210,13 +215,15 @@ static void fbuffer_append_str(FBuffer *fb, VALUE str)
210215

211216
static void fbuffer_append_str_repeat(FBuffer *fb, VALUE str, size_t repeat)
212217
{
218+
const char *newstr = StringValuePtr(str);
213219
unsigned long len = RSTRING_LEN(str);
214220

215-
size_t total = repeat * len;
216-
fbuffer_inc_capa(fb, total);
217-
221+
fbuffer_inc_capa(fb, repeat * len);
218222
while (repeat) {
219-
fbuffer_append_str(fb, str);
223+
#ifdef JSON_DEBUG
224+
fb->requested = len;
225+
#endif
226+
fbuffer_append_reserved(fb, newstr, len);
220227
repeat--;
221228
}
222229
}

0 commit comments

Comments
 (0)