8000 Remove computed GOTO · JavaScriptExpert/simdjson@8e69103 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e69103

Browse files
committed
Remove computed GOTO
1 parent 2f67dab commit 8e69103

File tree

3 files changed

+24
-44
lines changed

3 files changed

+24
-44
lines changed

src/generic/dom_parser_implementation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class dom_parser_implementation final : public internal::dom_parser_implementati
2020
public:
2121
/** Tape location of each open { or [ */
2222
std::unique_ptr<scope_descriptor[]> containing_scope{};
23-
/** Return address of each open { or [ */
24-
std::unique_ptr<ret_address_t[]> ret_address{};
23+
/** Whether each open container is a [ or { */
24+
std::unique_ptr<bool[]> is_array{};
2525
/** Buffer passed to stage 1 */
2626
const uint8_t *buf{};
2727
/** Length passed to stage 1 */

src/generic/stage2/allocate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace allocate {
88
//
99
really_inline error_code set_max_depth(dom_parser_implementation &parser, size_t max_depth) {
1010
parser.containing_scope.reset(new (std::nothrow) scope_descriptor[max_depth]);
11-
parser.ret_address.reset(new (std::nothrow) ret_address_t[max_depth]);
11+
parser.is_array.reset(new (std::nothrow) bool[max_depth]);
1212

13-
if (!parser.ret_address || !parser.containing_scope) {
13+
if (!parser.is_array || !parser.containing_scope) {
1414
return MEMALLOC;
1515
}
1616
return SUCCESS;

src/generic/stage2/structural_parser.h

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@ namespace { // Make everything here private
1212
namespace SIMDJSON_IMPLEMENTATION {
1313
namespace stage2 {
1414

15-
#ifdef SIMDJSON_USE_COMPUTED_GOTO
16-
#define INIT_ADDRESSES() { &&array_continue, &&finish, &&object_continue }
17-
#define CONTINUE(address) { goto *(address); }
18-
#else // SIMDJSON_USE_COMPUTED_GOTO
19-
#define INIT_ADDRESSES() { 0, 1, 2 };
20-
#define CONTINUE(address) \
21-
{ \
22-
switch(address) { \
23-
case 0: goto array_continue; \
24-
case 1: goto finish; \
25-
case 2: goto object_continue; \
26-
} \
27-
}
28-
#endif // SIMDJSON_USE_COMPUTED_GOTO
29-
30-
struct unified_machine_addresses {
31-
ret_address_t array_continue;
32-
ret_address_t finish;
33-
ret_address_t object_continue;
34-
};
35-
3615
struct structural_parser : structural_iterator {
3716
/** Lets you append to the tape */
3817
tape_writer tape;
@@ -48,30 +27,30 @@ struct structural_parser : structural_iterator {
4827
current_string_buf_loc{parser.doc->string_buf.get()} {
4928
}
5029

51-
WARN_UNUSED really_inline bool start_scope(ret_address_t continue_state) {
30+
WARN_UNUSED really_inline bool start_scope(bool parent_is_array) {
5231
parser.containing_scope[depth].tape_index = next_tape_index();
5332
parser.containing_scope[depth].count = 0;
5433
tape.skip(); // We don't actually *write* the start element until the end.
55-
parser.ret_address[depth] = continue_state;
34+
parser.is_array[depth] = parent_is_array;
5635
depth++;
5736
bool exceeded_max_depth = depth >= parser.max_depth();
5837
if (exceeded_max_depth) { log_error("Exceeded max depth!"); }
5938
return exceeded_max_depth;
6039
}
6140

62-
WARN_UNUSED really_inline bool start_document(ret_address_t continue_state) {
41+
WARN_UNUSED really_inline bool start_document() {
6342
log_start_value("document");
64-
return start_scope(continue_state);
43+
return start_scope(false);
6544
}
6645

67-
WARN_UNUSED really_inline bool start_object(ret_address_t continue_state) {
46+
WARN_UNUSED really_inline bool start_object(bool parent_is_array) {
6847
log_start_value("object");
69-
return start_scope(continue_state);
48+
return start_scope(parent_is_array);
7049
}
7150

72-
WARN_UNUSED really_inline bool start_array(ret_address_t continue_state) {
51+
WARN_UNUSED really_inline bool start_array(bool parent_is_array) {
7352
log_start_value("array");
74-
return start_scope(continue_state);
53+
return start_scope(parent_is_array);
7554
}
7655

7756
// this function is responsible for annotating the start of the scope
@@ -277,15 +256,15 @@ struct structural_parser : structural_iterator {
277256
parser.error = UNINITIALIZED;
278257
}
279258

280-
WARN_UNUSED really_inline error_code start(ret_address_t finish_state) {
259+
WARN_UNUSED really_inline error_code start() {
281260
// If there are no structurals left, return EMPTY
282261
if (at_end(parser.n_structural_indexes)) {
283262
return parser.error = EMPTY;
284263
}
285264

286265
init();
287266
// Push the root scope (there is always at least one scope)
288-
if (start_document(finish_state)) {
267+
if (start_document()) {
289268
return parser.error = DEPTH_ERROR;
290269
}
291270
return SUCCESS;
@@ -317,20 +296,19 @@ struct structural_parser : structural_iterator {
317296
template<bool STREAMING>
318297
WARN_UNUSED static error_code parse_structurals(dom_parser_implementation &dom_parser, dom::document &doc) noexcept {
319298
dom_parser.doc = &doc;
320-
static constexpr stage2::unified_machine_addresses addresses = INIT_ADDRESSES();
321299
stage2::structural_parser parser(dom_parser, STREAMING ? dom_parser.next_structural_index : 0);
322-
error_code result = parser.start(addresses.finish);
300+
error_code result = parser.start();
323301
if (result) { return result; }
324302

325303
//
326304
// Read first value
327305
//
328306
switch (parser.current_char()) {
329307
case '{':
330-
if ( parser.start_object(addresses.finish) ) { goto error; };
308+
if ( parser.start_object(false) ) { goto error; };
331309
goto object_begin;
332310
case '[':
333-
if ( parser.start_array(addresses.finish) ) { goto error; }
311+
if ( parser.start_array(false) ) { goto error; }
334312
// Make sure the outer array is closed before continuing; otherwise, there are ways we could get
335313
// into memory corruption. See https://github.com/simdjson/simdjson/issues/906
336314
if (!STREAMING) {
@@ -377,8 +355,8 @@ WARN_UNUSED static error_code parse_structurals(dom_parser_implementation &dom_p
377355
object_key_state:
378356
if (parser.advance_char() != ':' ) { parser.log_error("Missing colon after key in object"); goto error; }
379357
switch (parser.advance_char()) {
380-
case '{': if ( parser.start_object(addresses.object_continue) ) { goto error; } else { goto object_begin; }
381-
case '[': if ( parser.start_array(addresses.object_continue) ) { goto error; } else { goto array_begin; }
358+
case '{': if ( parser.start_object(false) ) { goto error; } else { goto object_begin; }
359+
case '[': if ( parser.start_array(false) ) { goto error; } else { goto array_begin; }
382360
case '"': if ( parser.parse_string() ) { goto error; }; break;
383361
case 't': if ( parser.parse_true_atom() ) { goto error; }; break;
384362
case 'f': if ( parser.parse_false_atom() ) { goto error; }; break;
@@ -408,7 +386,9 @@ WARN_UNUSED static error_code parse_structurals(dom_parser_implementation &dom_p
408386
}
409387

410388
scope_end:
411-
CONTINUE( parser.parser.ret_address[parser.depth] );
389+
if (parser.depth == 1) { goto finish; }
390+
if (parser.parser.is_array[parser.depth]) { goto array_continue; }
391+
goto object_continue;
412392

413393
//
414394
// Array parser states
@@ -423,8 +403,8 @@ WARN_UNUSED static error_code parse_structurals(dom_parser_implementation &dom_p
423403

424404
main_array_switch:
425405
switch (parser.advance_char()) {
426-
case '{': if ( parser.start_object(addresses.array_continue) ) { goto error; } else { goto object_begin; }
427-
case '[': if ( parser.start_array(addresses.array_continue) ) { goto error; } else { goto array_begin; }
406+
case '{': if ( parser.start_object(true) ) { goto error; } else { goto object_begin; }
407+
case '[': if ( parser.start_array(true) ) { goto error; } else { goto array_begin; }
428408
case '"': if ( parser.parse_string() ) { goto error; }; break;
429409
case 't': if ( parser.parse_true_atom() ) { goto error; }; break;
430410
case 'f': if ( parser.parse_false_atom() ) { goto error; }; break;

0 commit comments

Comments
 (0)
0