Skip to content

Commit 6471710

Browse files
mamebyroot
authored andcommitted
Apply RB_UNLIKELY for less frequently used options
This speeds up `JSON.generate` by about 4% in a benchmark.
1 parent 4c984b2 commit 6471710

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

ext/json/ext/generator/generator.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -642,20 +642,16 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
642642
JSON_Generator_State *state = arg->state;
643643
VALUE Vstate = arg->Vstate;
644644

645-
char *object_nl = state->object_nl;
646-
long object_nl_len = state->object_nl_len;
647-
char *indent = state->indent;
648-
long indent_len = state->indent_len;
649645
long depth = state->depth;
650646
int j;
651647

652648
if (arg->iter > 0) fbuffer_append_char(buffer, ',');
653-
if (object_nl) {
654-
fbuffer_append(buffer, object_nl, object_nl_len);
649+
if (RB_UNLIKELY(state->object_nl)) {
650+
fbuffer_append(buffer, state->object_nl, state->object_nl_len);
655651
}
656-
if (indent) {
652+
if (RB_UNLIKELY(state->indent)) {
657653
for (j = 0; j < depth; j++) {
658-
fbuffer_append(buffer, indent, indent_len);
654+
fbuffer_append(buffer, state->indent, state->indent_len);
659655
}
660656
}
661657

@@ -684,10 +680,6 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
684680

685681
static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
686682
{
687-
char *object_nl = state->object_nl;
688-
long object_nl_len = state->object_nl_len;
689-
char *indent = state->indent;
690-
long indent_len = state->indent_len;
691683
long max_nesting = state->max_nesting;
692684
long depth = ++state->depth;
693685
int j;
@@ -705,11 +697,11 @@ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
705697
rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
706698

707699
depth = --state->depth;
708-
if (object_nl) {
709-
fbuffer_append(buffer, object_nl, object_nl_len);
710-
if (indent) {
700+
if (RB_UNLIKELY(state->object_nl)) {
701+
fbuffer_append(buffer, state->object_nl, state->object_nl_len);
702+
if (RB_UNLIKELY(state->indent)) {
711703
for (j = 0; j < depth; j++) {
712-
fbuffer_append(buffer, indent, indent_len);
704+
fbuffer_append(buffer, state->indent, state->indent_len);
713705
}
714706
}
715707
}
@@ -718,36 +710,32 @@ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
718710

719711
static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
720712
{
721-
char *array_nl = state->array_nl;
722-
long array_nl_len = state->array_nl_len;
723-
char *indent = state->indent;
724-
long indent_len = state->indent_len;
725713
long max_nesting = state->max_nesting;
726714
long depth = ++state->depth;
727715
int i, j;
728716
if (max_nesting != 0 && depth > max_nesting) {
729717
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
730718
}
731719
fbuffer_append_char(buffer, '[');
732-
if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
720+
if (RB_UNLIKELY(state->array_nl)) fbuffer_append(buffer, state->array_nl, state->array_nl_len);
733721
for(i = 0; i < RARRAY_LEN(obj); i++) {
734722
if (i > 0) {
735723
fbuffer_append_char(buffer, ',');
736724
if (RB_UNLIKELY(state->array_nl)) fbuffer_append(buffer, state->array_nl, state->array_nl_len);
737725
}
738-
if (indent) {
726+
if (RB_UNLIKELY(state->indent)) {
739727
for (j = 0; j < depth; j++) {
740-
fbuffer_append(buffer, indent, indent_len);
728+
fbuffer_append(buffer, state->indent, state->indent_len);
741729
}
742730
}
743731
generate_json(buffer, Vstate, state, RARRAY_AREF(obj, i));
744732
}
745733
state->depth = --depth;
746-
if (array_nl) {
747-
fbuffer_append(buffer, array_nl, array_nl_len);
748-
if (indent) {
734+
if (RB_UNLIKELY(state->array_nl)) {
735+
fbuffer_append(buffer, state->array_nl, state->array_nl_len);
736+
if (RB_UNLIKELY(state->indent)) {
749737
for (j = 0; j < depth; j++) {
750-
fbuffer_append(buffer, indent, indent_len);
738+
fbuffer_append(buffer, state->indent, state->indent_len);
751739
}
752740
}
753741
}

0 commit comments

Comments
 (0)