Skip to content

Commit 4c984b2

Browse files
mamebyroot
authored andcommitted
Stop prebuilding object_delim2
Also, remove static functions that are no longer used. This speeds up `JSON.generate` by about 5% in a benchmark.
1 parent ed47a10 commit 4c984b2

File tree

3 files changed

+7
-27
lines changed

3 files changed

+7
-27
lines changed

ext/json/ext/fbuffer/fbuffer.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ typedef struct FBufferStruct {
5555

5656
static FBuffer *fbuffer_alloc(unsigned long initial_length);
5757
static void fbuffer_free(FBuffer *fb);
58+
#ifndef JSON_GENERATOR
5859
static void fbuffer_clear(FBuffer *fb);
60+
#endif
5961
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
6062
#ifdef JSON_GENERATOR
6163
static void fbuffer_append_long(FBuffer *fb, long number);
6264
#endif
6365
static void fbuffer_append_char(FBuffer *fb, char newchr);
6466
#ifdef JSON_GENERATOR
65-
static FBuffer *fbuffer_dup(FBuffer *fb);
6667
static VALUE fbuffer_to_s(FBuffer *fb);
6768
#endif
6869

@@ -86,10 +87,12 @@ static void fbuffer_free(FBuffer *fb)
8687
ruby_xfree(fb);
8788
}
8889

90+
#ifndef JSON_GENERATOR
8991
static void fbuffer_clear(FBuffer *fb)
9092
{
9193
fb->len = 0;
9294
}
95+
#endif
9396

9497
static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
9598
{
@@ -168,16 +171,6 @@ static void fbuffer_append_long(FBuffer *fb, long number)
168171
fbuffer_append(fb, buf, len);
169172
}
170173

171-
static FBuffer *fbuffer_dup(FBuffer *fb)
172-
{
173-
unsigned long len = fb->len;
174-
FBuffer *result;
175-
176-
result = fbuffer_alloc(len);
177-
fbuffer_append(result, FBUFFER_PAIR(fb));
178-
return result;
179-
}
180-
181174
static VALUE fbuffer_to_s(FBuffer *fb)
182175
{
183176
VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));

ext/json/ext/generator/generator.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ static void State_free(void *ptr)
421421
if (state->space_before) ruby_xfree(state->space_before);
422422
if (state->object_nl) ruby_xfree(state->object_nl);
423423
if (state->array_nl) ruby_xfree(state->array_nl);
424-
if (state->object_delim2) fbuffer_free(state->object_delim2);
425424
ruby_xfree(state);
426425
}
427426

@@ -434,7 +433,6 @@ static size_t State_memsize(const void *ptr)
434433
if (state->space_before) size += state->space_before_len + 1;
435434
if (state->object_nl) size += state->object_nl_len + 1;
436435
if (state->array_nl) size += state->array_nl_len + 1;
437-
if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
438436
return size;
439437
}
440438

@@ -648,8 +646,6 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
648646
long object_nl_len = state->object_nl_len;
649647
char *indent = state->indent;
650648
long indent_len = state->indent_len;
651-
char *delim2 = FBUFFER_PTR(state->object_delim2);
652-
long delim2_len = FBUFFER_LEN(state->object_delim2);
653649
long depth = state->depth;
654650
int j;
655651

@@ -677,7 +673,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
677673
}
678674

679675
generate_json_string(buffer, Vstate, state, key_to_s);
680-
fbuffer_append(buffer, delim2, delim2_len);
676+
if (RB_UNLIKELY(state->space_before)) fbuffer_append(buffer, state->space_before, state->space_before_len);
677+
fbuffer_append_char(buffer, ':');
678+
if (RB_UNLIKELY(state->space)) fbuffer_append(buffer, state->space, state->space_len);
681679
generate_json(buffer, Vstate, state, val);
682680

683681
arg->iter++;
@@ -885,15 +883,6 @@ static FBuffer *cState_prepare_buffer(VALUE self)
885883
GET_STATE(self);
886884
buffer = fbuffer_alloc(state->buffer_initial_length);
887885

888-
if (state->object_delim2) {
889-
fbuffer_clear(state->object_delim2);
890-
} else {
891-
state->object_delim2 = fbuffer_alloc(16);
892-
}
893-
if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
894-
fbuffer_append_char(state->object_delim2, ':');
895-
if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
896-
897886
return buffer;
898887
}
899888

@@ -1006,7 +995,6 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
1006995
objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
1007996
objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
1008997
objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
1009-
if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
1010998
return obj;
1011999
}
10121000

ext/json/ext/generator/generator.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ typedef struct JSON_Generator_StateStruct {
5555
long object_nl_len;
5656
char *array_nl;
5757
long array_nl_len;
58-
FBuffer *object_delim2;
5958
long max_nesting;
6059
char allow_nan;
6160
char ascii_only;

0 commit comments

Comments
 (0)