mirror of
https://github.com/ruby/ruby.git
synced 2025-08-23 13:04:13 +02:00
[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:
parent
186e77209e
commit
64c24f6971
3 changed files with 7 additions and 27 deletions
|
@ -55,14 +55,15 @@ typedef struct FBufferStruct {
|
|||
|
||||
static FBuffer *fbuffer_alloc(unsigned long initial_length);
|
||||
static void fbuffer_free(FBuffer *fb);
|
||||
#ifndef JSON_GENERATOR
|
||||
static void fbuffer_clear(FBuffer *fb);
|
||||
#endif
|
||||
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
|
||||
#ifdef JSON_GENERATOR
|
||||
static void fbuffer_append_long(FBuffer *fb, long number);
|
||||
#endif
|
||||
static void fbuffer_append_char(FBuffer *fb, char newchr);
|
||||
#ifdef JSON_GENERATOR
|
||||
static FBuffer *fbuffer_dup(FBuffer *fb);
|
||||
static VALUE fbuffer_to_s(FBuffer *fb);
|
||||
#endif
|
||||
|
||||
|
@ -86,10 +87,12 @@ static void fbuffer_free(FBuffer *fb)
|
|||
ruby_xfree(fb);
|
||||
}
|
||||
|
||||
#ifndef JSON_GENERATOR
|
||||
static void fbuffer_clear(FBuffer *fb)
|
||||
{
|
||||
fb->len = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
|
||||
|
|
|
@ -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_delim2) fbuffer_free(state->object_delim2);
|
||||
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->object_nl) size += state->object_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;
|
||||
}
|
||||
|
||||
|
@ -648,8 +646,6 @@ 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 *delim2 = FBUFFER_PTR(state->object_delim2);
|
||||
long delim2_len = FBUFFER_LEN(state->object_delim2);
|
||||
long depth = state->depth;
|
||||
int j;
|
||||
|
||||
|
@ -677,7 +673,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
|
|||
}
|
||||
|
||||
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);
|
||||
|
||||
arg->iter++;
|
||||
|
@ -885,15 +883,6 @@ static FBuffer *cState_prepare_buffer(VALUE self)
|
|||
GET_STATE(self);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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->object_nl = fstrndup(origState->object_nl, origState->object_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;
|
||||
}
|
||||
|
||||
|
|
|
@ -55,7 +55,6 @@ typedef struct JSON_Generator_StateStruct {
|
|||
long object_nl_len;
|
||||
char *array_nl;
|
||||
long array_nl_len;
|
||||
FBuffer *object_delim2;
|
||||
long max_nesting;
|
||||
char allow_nan;
|
||||
char ascii_only;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue