Skip to content

Commit

Permalink
Stop prebuilding object_delim
Browse files Browse the repository at this point in the history
This speeds up `JSON.generate` by about 4% in a benchmark
  • Loading branch information
mame authored and byroot committed Oct 17, 2024
1 parent 445de6e commit ed47a10
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 13 deletions.
13 changes: 1 addition & 12 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ static void State_free(void *ptr)
if (state->space_before) ruby_xfree(state->space_before);
if (state->object_nl) ruby_xfree(state->object_nl);
if (state->array_nl) ruby_xfree(state->array_nl);
if (state->object_delim) fbuffer_free(state->object_delim);
if (state->object_delim2) fbuffer_free(state->object_delim2);
ruby_xfree(state);
}
Expand All @@ -435,7 +434,6 @@ static size_t State_memsize(const void *ptr)
if (state->space_before) size += state->space_before_len + 1;
if (state->object_nl) size += state->object_nl_len + 1;
if (state->array_nl) size += state->array_nl_len + 1;
if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
return size;
}
Expand Down Expand Up @@ -650,14 +648,12 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
long object_nl_len = state->object_nl_len;
char *indent = state->indent;
long indent_len = state->indent_len;
char *delim = FBUFFER_PTR(state->object_delim);
long delim_len = FBUFFER_LEN(state->object_delim);
char *delim2 = FBUFFER_PTR(state->object_delim2);
long delim2_len = FBUFFER_LEN(state->object_delim2);
long depth = state->depth;
int j;

if (arg->iter > 0) fbuffer_append(buffer, delim, delim_len);
if (arg->iter > 0) fbuffer_append_char(buffer, ',');
if (object_nl) {
fbuffer_append(buffer, object_nl, object_nl_len);
}
Expand Down Expand Up @@ -889,12 +885,6 @@ static FBuffer *cState_prepare_buffer(VALUE self)
GET_STATE(self);
buffer = fbuffer_alloc(state->buffer_initial_length);

if (state->object_delim) {
fbuffer_clear(state->object_delim);
} else {
state->object_delim = fbuffer_alloc(16);
}
fbuffer_append_char(state->object_delim, ',');
if (state->object_delim2) {
fbuffer_clear(state->object_delim2);
} else {
Expand Down Expand Up @@ -1016,7 +1006,6 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
if (origState->object_delim) objState->object_delim = fbuffer_dup(origState->object_delim);
if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
return obj;
}
Expand Down
1 change: 0 additions & 1 deletion ext/json/ext/generator/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ typedef struct JSON_Generator_StateStruct {
long object_nl_len;
char *array_nl;
long array_nl_len;
FBuffer *object_delim;
FBuffer *object_delim2;
long max_nesting;
char allow_nan;
Expand Down

0 comments on commit ed47a10

Please sign in to comment.