Skip to content

Commit 64ae40a

Browse files
committed
Remove array::has_next
1 parent 6dd3912 commit 64ae40a

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

src/generic/ondemand/array-inl.h

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,22 @@ namespace ondemand {
4141
//
4242

4343
simdjson_really_inline array::array() noexcept = default;
44-
simdjson_really_inline array::array(json_iterator_ref &&_iter, bool has_value) noexcept
45-
: iter{std::forward<json_iterator_ref>(_iter)}, has_next{has_value}, error{SUCCESS}
44+
simdjson_really_inline array::array(json_iterator_ref &&_iter) noexcept
45+
: iter{std::forward<json_iterator_ref>(_iter)}, error{SUCCESS}
4646
{
4747
}
4848
simdjson_really_inline array::array(array &&other) noexcept
49-
: iter{std::forward<array>(other).iter}, has_next{other.has_next}, error{other.error}
49+
: iter{std::forward<array>(other).iter}, error{other.error}
5050
{
5151
}
5252
simdjson_really_inline array &array::operator=(array &&other) noexcept {
5353
iter = std::forward<array>(other).iter;
54-
has_next = other.has_next;
5554
error = other.error;
5655
return *this;
5756
}
5857

5958
simdjson_really_inline array::~array() noexcept {
60-
if (!error && has_next && iter.is_alive()) {
59+
if (iter.is_alive()) {
6160
logger::log_event(*iter, "unfinished", "array");
6261
iter->skip_container();
6362
iter.release();
@@ -67,10 +66,12 @@ simdjson_really_inline array::~array() noexcept {
6766
simdjson_really_inline simdjson_result<array> array::start(json_iterator_ref &&iter) noexcept {
6867
bool has_value;
6968
SIMDJSON_TRY( iter->start_array().get(has_value) );
70-
return array(std::forward<json_iterator_ref>(iter), has_value);
69+
if (!has_value) { iter.release(); }
70+
return array(std::forward<json_iterator_ref>(iter));
7171
}
7272
simdjson_really_inline array array::started(json_iterator_ref &&iter) noexcept {
73-
return array(std::forward<json_iterator_ref>(iter), iter->started_array());
73+
if (!iter->started_array()) { iter.release(); }
74+
return array(std::forward<json_iterator_ref>(iter));
7475
}
7576
simdjson_really_inline array::iterator array::begin() noexcept {
7677
return *this;
@@ -79,31 +80,26 @@ simdjson_really_inline array::iterator array::end() noexcept {
7980
return *this;
8081
}
8182

82-
simdjson_really_inline error_code array::report_error() noexcept {
83-
SIMDJSON_ASSUME(error);
84-
has_next = false;
85-
return error;
86-
}
87-
8883
simdjson_really_inline array::iterator::iterator(array &_a) noexcept : a{&_a} {}
8984

9085
simdjson_really_inline array::iterator::iterator() noexcept = default;
9186
simdjson_really_inline array::iterator::iterator(const array::iterator &_a) noexcept = default;
9287
simdjson_really_inline array::iterator &array::iterator::operator=(const array::iterator &_a) noexcept = default;
9388

9489
simdjson_really_inline simdjson_result<value> array::iterator::operator*() noexcept {
95-
if (a->error) { return a->report_error(); }
90+
if (a->error) { a->iter.release(); return a->error; }
9691
return value::start(a->iter.borrow());
9792
}
9893
simdjson_really_inline bool array::iterator::operator==(const array::iterator &other) noexcept {
9994
return !(*this != other);
10095
}
10196
simdjson_really_inline bool array::iterator::operator!=(const array::iterator &) noexcept {
102-
return a->has_next;
97+
return a->iter.is_alive();
10398
}
10499
simdjson_really_inline array::iterator &array::iterator::operator++() noexcept {
105-
a->error = a->iter->has_next_element().get(a->has_next); // If there's an error, has_next stays true.
106-
if (!a->error && !a->has_next) { a->iter.release(); }
100+
bool has_value;
101+
a->error = a->iter->has_next_element().get(has_value); // If there's an error, has_next stays true.
102+
if (!(a->error || has_value)) { a->iter.release(); }
107103
return *this;
108104
}
109105

src/generic/ondemand/array.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,13 @@ class array {
6161
*/
6262
static simdjson_really_inline array started(json_iterator_ref &&iter) noexcept;
6363

64-
/**
65-
* Report the current error and set finished so it won't be reported again.
66-
*/
67-
simdjson_really_inline error_code report_error() noexcept;
68-
6964
/**
7065
* Internal array creation. Call array::start() or array::started() instead of this.
7166
*
7267
* @param doc The document containing the array. iter->depth must already be incremented to
7368
* reflect the array's depth. The iterator must be just after the opening `[`.
74-
* @param has_value Whether the array has a value (false means empty array).
7569
*/
76-
simdjson_really_inline array(json_iterator_ref &&iter, bool has_value) noexcept;
70+
simdjson_really_inline array(json_iterator_ref &&iter) noexcept;
7771

7872
/**
7973
* Document containing this array.
@@ -82,14 +76,6 @@ class array {
8276
* is first used, and never changes afterwards.
8377
*/
8478
json_iterator_ref iter{};
85-
/**
86-
* Whether we have anything to yield.
87-
*
88-
* PERF NOTE: we hope this will be elided into inline control flow, as it is true for all
89-
* iterations except the last, and compilers with SSA optimization can sometimes do last-iteration
90-
* optimization.
91-
*/
92-
bool has_next{};
9379
/**
9480
* Error, if there is one. Errors are only yielded once.
9581
*

0 commit comments

Comments
 (0)