Skip to content

Commit d178e08

Browse files
committed
Stop caching current structural, keep current index around instead of
next
1 parent 5f00b37 commit d178e08

3 files changed

Lines changed: 26 additions & 28 deletions

File tree

src/generic/stage2/logger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ namespace logger {
6262
}
6363
printf("| %c ", printable_char(structurals.at_beginning() ? ' ' : structurals.current_char()));
6464
printf("| %c ", printable_char(structurals.peek_char()));
65-
printf("| %5u ", structurals.parser.structural_indexes[*structurals.next_structural]);
65+
printf("| %5u ", structurals.parser.structural_indexes[*(structurals.current_structural+1)]);
6666
printf("| %-*s ", LOG_DETAIL_LEN, detail);
67-
printf("| %*u ", LOG_INDEX_LEN, *(structurals.next_structural-1));
67+
printf("| %*u ", LOG_INDEX_LEN, *structurals.current_structural);
6868
printf("|\n");
6969
}
7070
}

src/generic/stage2/structural_iterator.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,33 @@ namespace stage2 {
33
class structural_iterator {
44
public:
55
const uint8_t* const buf;
6-
uint32_t *next_structural;
7-
uint8_t c{0}; // used to track the (structural) character we are looking at
6+
uint32_t *current_structural;
87
dom_parser_implementation &parser;
98

10-
really_inline structural_iterator(dom_parser_implementation &_parser, size_t next_structural_index)
9+
// Start a structural
10+
really_inline structural_iterator(dom_parser_implementation &_parser, size_t start_structural_index)
1111
: buf{_parser.buf},
12-
next_structural{&_parser.structural_indexes[next_structural_index]},
12+
current_structural{&_parser.structural_indexes[start_structural_index]},
1313
parser{_parser} {
1414
}
15-
really_inline char advance_char() {
16-
c = buf[*next_structural];
17-
next_structural++;
18-
return c;
15+
// Get the buffer position of the current structural character
16+
really_inline const uint8_t* current() {
17+
return &buf[*current_structural];
1918
}
19+
// Get the current structural character
2020
really_inline char current_char() {
21-
return c;
21+
return buf[*current_structural];
2222
}
23+
// Get the next structural character without advancing
2324
really_inline char peek_char() {
24-
return buf[*next_structural];
25+
return buf[*(current_structural+1)];
2526
}
26-
really_inline const uint8_t* current() {
27-
return &buf[current_structural_index()];
27+
really_inline char advance_char() {
28+
current_structural++;
29+
return buf[*current_structural];
2830
}
2931
really_inline size_t remaining_len() {
30-
return parser.len - current_structural_index();
31-
}
32-
really_inline uint32_t current_structural_index() {
33-
return *(next_structural-1);
32+
return parser.len - *current_structural;
3433
}
3534
template<typename F>
3635
really_inline bool with_space_terminated_copy(const F& f) {
@@ -53,18 +52,18 @@ class structural_iterator {
5352
}
5453
memcpy(copy, buf, parser.len);
5554
memset(copy + parser.len, ' ', SIMDJSON_PADDING);
56-
bool result = f(reinterpret_cast<const uint8_t*>(copy), current_structural_index());
55+
bool result = f(reinterpret_cast<const uint8_t*>(copy), *current_structural);
5756
free(copy);
5857
return result;
5958
}
6059
really_inline bool past_end(uint32_t n_structural_indexes) {
61-
return next_structural > &parser.structural_indexes[n_structural_indexes];
60+
return current_structural >= &parser.structural_indexes[n_structural_indexes];
6261
}
6362
really_inline bool at_end(uint32_t n_structural_indexes) {
64-
return next_structural == &parser.structural_indexes[n_structural_indexes];
63+
return current_structural == &parser.structural_indexes[n_structural_indexes];
6564
}
6665
really_inline bool at_beginning() {
67-
return next_structural == &parser.structural_indexes[0];
66+
return current_structural == parser.structural_indexes.get();
6867
}
6968
};
7069

src/generic/stage2/structural_parser.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ struct number_writer {
7272
struct structural_parser : structural_iterator {
7373
/** Next write location in the string buf for stage 2 parsing */
7474
uint8_t *current_string_buf_loc{};
75+
/** Current depth (nested objects and arrays) */
7576
uint32_t depth;
7677

7778
// For non-streaming, to pass an explicit 0 as next_structural, which enables optimizations
78-
really_inline structural_parser(dom_parser_implementation &_parser, uint32_t _next_structural)
79-
: structural_iterator(_parser, _next_structural),
79+
really_inline structural_parser(dom_parser_implementation &_parser, uint32_t start_structural_index)
80+
: structural_iterator(_parser, start_structural_index),
8081
depth{0} {
8182
}
8283

@@ -232,7 +233,7 @@ struct structural_parser : structural_iterator {
232233

233234
WARN_UNUSED really_inline error_code finish() {
234235
end_document();
235-
parser.next_structural_index = uint32_t(next_structural - &parser.structural_indexes[0]);
236+
parser.next_structural_index = uint32_t(current_structural + 1 - &parser.structural_indexes[0]);
236237

237238
if (depth != 0) {
238239
log_error("Unclosed objects or arrays!");
@@ -283,6 +284,7 @@ struct structural_parser : structural_iterator {
283284
}
284285

285286
really_inline void init() {
287+
log_start();
286288
current_string_buf_loc = parser.doc->string_buf.get();
287289
parser.current_loc = 0;
288290
parser.error = UNINITIALIZED;
@@ -294,10 +296,7 @@ struct structural_parser : structural_iterator {
294296
return parser.error = EMPTY;
295297
}
296298

297-
log_start();
298299
init();
299-
// Advance to the first character as soon as possible
300-
advance_char();
301300
// Push the root scope (there is always at least one scope)
302301
if (start_document(finish_state)) {
303302
return parser.error = DEPTH_ERROR;

0 commit comments

Comments
 (0)