Skip to content

Commit f0dda86

Browse files
committed
Optimize fbuffer_append_str_repeat
Helps with pretty printting performance: ``` == Encoding activitypub.json (52595 bytes) ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 1.746k i/100ms Calculating ------------------------------------- after 17.481k (± 1.0%) i/s (57.20 μs/i) - 89.046k in 5.094341s Comparison: before: 16038.4 i/s after: 17481.1 i/s - 1.09x faster == Encoding citm_catalog.json (500298 bytes) ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 60.000 i/100ms Calculating ------------------------------------- after 608.157 (± 2.3%) i/s (1.64 ms/i) - 3.060k in 5.034238s Comparison: before: 525.3 i/s after: 608.2 i/s - 1.16x faster == Encoding twitter.json (466906 bytes) ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 160.000 i/100ms Calculating ------------------------------------- after 1.606k (± 0.5%) i/s (622.70 μs/i) - 8.160k in 5.081406s Comparison: before: 1410.3 i/s after: 1605.9 i/s - 1.14x faster ```
1 parent 36fefff commit f0dda86

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)