Skip to content

Commit 500f0ea

Browse files
committed
parser.c: Mark some paths as unreacheable
Generate very sligthly better code (one less instruction...). Also extract `json_on_duplicate_key` as `NOINLINE`.
1 parent 216e0ff commit 500f0ea

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

ext/json/ext/parser/parser.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,27 @@ NORETURN(static) void raise_duplicate_key_error(JSON_ParserState *state, VALUE d
10931093
rb_exc_raise(parse_error_new(message, line, column));
10941094
}
10951095

1096+
NOINLINE(static) void json_on_duplicate_key(JSON_ParserState *state, JSON_ParserConfig *config, size_t count, const VALUE *pairs)
1097+
{
1098+
switch (config->on_duplicate_key) {
1099+
case JSON_IGNORE:
1100+
return;
1101+
1102+
case JSON_DEPRECATED:
1103+
// Only emit the first few deprecations to avoid spamming.
1104+
if (state->emitted_deprecations < 5) {
1105+
emit_duplicate_key_warning(state, json_find_duplicated_key(count, pairs));
1106+
state->emitted_deprecations++;
1107+
}
1108+
return;
1109+
1110+
case JSON_RAISE:
1111+
raise_duplicate_key_error(state, json_find_duplicated_key(count, pairs));
1112+
return;
1113+
}
1114+
UNREACHABLE;
1115+
}
1116+
10961117
static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfig *config, size_t count)
10971118
{
10981119
size_t entries_count = count / 2;
@@ -1101,21 +1122,7 @@ static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfi
11011122
rb_hash_bulk_insert(count, pairs, object);
11021123

11031124
if (RB_UNLIKELY(RHASH_SIZE(object) < entries_count)) {
1104-
switch (config->on_duplicate_key) {
1105-
case JSON_IGNORE:
1106-
break;
1107-
case JSON_DEPRECATED:
1108-
// Only emit the first few deprecations to avoid spamming.
1109-
if (state->emitted_deprecations < 5) {
1110-
emit_duplicate_key_warning(state, json_find_duplicated_key(count, pairs));
1111-
state->emitted_deprecations++;
1112-
}
1113-
1114-
break;
1115-
case JSON_RAISE:
1116-
raise_duplicate_key_error(state, json_find_duplicated_key(count, pairs));
1117-
break;
1118-
}
1125+
json_on_duplicate_key(state, config, count, pairs);
11191126
}
11201127

11211128
rvalue_stack_pop(state->value_stack, count);
@@ -1414,18 +1421,18 @@ static inline long json_frame_entry_count(const json_frame *frame, const rvalue_
14141421
// after a container close is the freshly re-exposed parent.
14151422
static inline void json_value_completed(json_frame *frame)
14161423
{
1417-
// TODO: consider a lookup table?
14181424
switch (frame->type) {
14191425
case JSON_FRAME_ROOT:
14201426
frame->phase = JSON_PHASE_DONE;
1421-
break;
1427+
return;
14221428
case JSON_FRAME_ARRAY:
14231429
frame->phase = JSON_PHASE_ARRAY_COMMA;
1424-
break;
1430+
return;
14251431
case JSON_FRAME_OBJECT:
14261432
frame->phase = JSON_PHASE_OBJECT_COMMA;
1427-
break;
1433+
return;
14281434
}
1435+
UNREACHABLE;
14291436
}
14301437

14311438
// Parse an arbitrary JSON value iteratively. This is a state machine driven

0 commit comments

Comments
 (0)