[ruby/json] Stop prebuilding object_delim2

Also, remove static functions that are no longer used.

This speeds up `JSON.generate` by about 5% in a benchmark.

4c984b2017
This commit is contained in:
Yusuke Endoh 2023-12-27 15:57:02 +09:00 committed by git
parent 186e77209e
commit 64c24f6971
3 changed files with 7 additions and 27 deletions

View file

@ -55,14 +55,15 @@ typedef struct FBufferStruct {
static FBuffer *fbuffer_alloc(unsigned long initial_length); static FBuffer *fbuffer_alloc(unsigned long initial_length);
static void fbuffer_free(FBuffer *fb); static void fbuffer_free(FBuffer *fb);
#ifndef JSON_GENERATOR
static void fbuffer_clear(FBuffer *fb); static void fbuffer_clear(FBuffer *fb);
#endif
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len); static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
#ifdef JSON_GENERATOR #ifdef JSON_GENERATOR
static void fbuffer_append_long(FBuffer *fb, long number); static void fbuffer_append_long(FBuffer *fb, long number);
#endif #endif
static void fbuffer_append_char(FBuffer *fb, char newchr); static void fbuffer_append_char(FBuffer *fb, char newchr);
#ifdef JSON_GENERATOR #ifdef JSON_GENERATOR
static FBuffer *fbuffer_dup(FBuffer *fb);
static VALUE fbuffer_to_s(FBuffer *fb); static VALUE fbuffer_to_s(FBuffer *fb);
#endif #endif
@ -86,10 +87,12 @@ static void fbuffer_free(FBuffer *fb)
ruby_xfree(fb); ruby_xfree(fb);
} }
#ifndef JSON_GENERATOR
static void fbuffer_clear(FBuffer *fb) static void fbuffer_clear(FBuffer *fb)
{ {
fb->len = 0; fb->len = 0;
} }
#endif
static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested) static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
{ {
@ -168,16 +171,6 @@ static void fbuffer_append_long(FBuffer *fb, long number)
fbuffer_append(fb, buf, len); fbuffer_append(fb, buf, len);
} }
static FBuffer *fbuffer_dup(FBuffer *fb)
{
unsigned long len = fb->len;
FBuffer *result;
result = fbuffer_alloc(len);
fbuffer_append(result, FBUFFER_PAIR(fb));
return result;
}
static VALUE fbuffer_to_s(FBuffer *fb) static VALUE fbuffer_to_s(FBuffer *fb)
{ {
VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb)); VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));

View file

@ -421,7 +421,6 @@ static void State_free(void *ptr)
if (state->space_before) ruby_xfree(state->space_before); if (state->space_before) ruby_xfree(state->space_before);
if (state->object_nl) ruby_xfree(state->object_nl); if (state->object_nl) ruby_xfree(state->object_nl);
if (state->array_nl) ruby_xfree(state->array_nl); if (state->array_nl) ruby_xfree(state->array_nl);
if (state->object_delim2) fbuffer_free(state->object_delim2);
ruby_xfree(state); ruby_xfree(state);
} }
@ -434,7 +433,6 @@ static size_t State_memsize(const void *ptr)
if (state->space_before) size += state->space_before_len + 1; if (state->space_before) size += state->space_before_len + 1;
if (state->object_nl) size += state->object_nl_len + 1; if (state->object_nl) size += state->object_nl_len + 1;
if (state->array_nl) size += state->array_nl_len + 1; if (state->array_nl) size += state->array_nl_len + 1;
if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
return size; return size;
} }
@ -648,8 +646,6 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
long object_nl_len = state->object_nl_len; long object_nl_len = state->object_nl_len;
char *indent = state->indent; char *indent = state->indent;
long indent_len = state->indent_len; long indent_len = state->indent_len;
char *delim2 = FBUFFER_PTR(state->object_delim2);
long delim2_len = FBUFFER_LEN(state->object_delim2);
long depth = state->depth; long depth = state->depth;
int j; int j;
@ -677,7 +673,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
} }
generate_json_string(buffer, Vstate, state, key_to_s); generate_json_string(buffer, Vstate, state, key_to_s);
fbuffer_append(buffer, delim2, delim2_len); if (RB_UNLIKELY(state->space_before)) fbuffer_append(buffer, state->space_before, state->space_before_len);
fbuffer_append_char(buffer, ':');
if (RB_UNLIKELY(state->space)) fbuffer_append(buffer, state->space, state->space_len);
generate_json(buffer, Vstate, state, val); generate_json(buffer, Vstate, state, val);
arg->iter++; arg->iter++;
@ -885,15 +883,6 @@ static FBuffer *cState_prepare_buffer(VALUE self)
GET_STATE(self); GET_STATE(self);
buffer = fbuffer_alloc(state->buffer_initial_length); buffer = fbuffer_alloc(state->buffer_initial_length);
if (state->object_delim2) {
fbuffer_clear(state->object_delim2);
} else {
state->object_delim2 = fbuffer_alloc(16);
}
if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
fbuffer_append_char(state->object_delim2, ':');
if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
return buffer; return buffer;
} }
@ -1006,7 +995,6 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
objState->space_before = fstrndup(origState->space_before, origState->space_before_len); objState->space_before = fstrndup(origState->space_before, origState->space_before_len);
objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len); objState->object_nl = fstrndup(origState->object_nl, origState->object_nl_len);
objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len); objState->array_nl = fstrndup(origState->array_nl, origState->array_nl_len);
if (origState->object_delim2) objState->object_delim2 = fbuffer_dup(origState->object_delim2);
return obj; return obj;
} }

View file

@ -55,7 +55,6 @@ typedef struct JSON_Generator_StateStruct {
long object_nl_len; long object_nl_len;
char *array_nl; char *array_nl;
long array_nl_len; long array_nl_len;
FBuffer *object_delim2;
long max_nesting; long max_nesting;
char allow_nan; char allow_nan;
char ascii_only; char ascii_only;