@@ -72,13 +72,13 @@ inline void document_stream::start() noexcept {
7272
7373 // Always run the first stage 1 parse immediately
7474 batch_start = 0 ;
75- run_stage1 (parser, batch_start);
75+ error = run_stage1 (parser, batch_start);
7676 if (error) { return ; }
7777
7878#ifdef SIMDJSON_THREADS_ENABLED
7979 if (next_batch_start () < len) {
8080 // Kick off the first thread if needed
81- error = thread_parser .ensure_capacity (batch_size);
81+ error = stage1_thread_parser .ensure_capacity (batch_size);
8282 if (error) { return ; }
8383 start_stage1_thread ();
8484 if (error) { return ; }
@@ -102,7 +102,7 @@ inline void document_stream::next() noexcept {
102102#ifdef SIMDJSON_THREADS_ENABLED
103103 load_from_stage1_thread ();
104104#else
105- run_stage1 (parser, batch_start);
105+ error = run_stage1 (parser, batch_start);
106106#endif
107107 if (error) { continue ; } // If the error was EMPTY, we may want to load another batch.
108108
@@ -115,13 +115,13 @@ inline size_t document_stream::next_batch_start() const noexcept {
115115 return batch_start + parser.implementation ->structural_indexes [parser.implementation ->n_structural_indexes ];
116116}
117117
118- inline void document_stream::run_stage1 (dom::parser &p, size_t _batch_start) noexcept {
118+ inline error_code document_stream::run_stage1 (dom::parser &p, size_t _batch_start) noexcept {
119119 // If this is the final batch, pass partial = false
120120 size_t remaining = len - _batch_start;
121121 if (remaining <= batch_size) {
122- error = p.implementation ->stage1 (&buf[_batch_start], remaining, false );
122+ return p.implementation ->stage1 (&buf[_batch_start], remaining, false );
123123 } else {
124- error = p.implementation ->stage1 (&buf[_batch_start], batch_size, true );
124+ return p.implementation ->stage1 (&buf[_batch_start], batch_size, true );
125125 }
126126}
127127
@@ -132,10 +132,9 @@ inline void document_stream::load_from_stage1_thread() noexcept {
132132
133133 // Swap to the parser that was loaded up in the thread. Make sure the parser has
134134 // enough memory to swap to, as well.
135- error = parser.ensure_capacity (batch_size);
136- if (error) { return error; }
137135 std::swap (parser, stage1_thread_parser);
138- if (stage1_thread_error) { return stage1_thread_error; }
136+ error = stage1_thread_error;
137+ if (error) { return ; }
139138
140139 // If there's anything left, start the stage 1 thread!
141140 if (next_batch_start () < len) {
@@ -149,11 +148,9 @@ inline void document_stream::start_stage1_thread() noexcept {
149148 // there is only one thread that may write to this value
150149 // TODO this is NOT exception-safe.
151150 this ->stage1_thread_error = UNINITIALIZED; // In case something goes wrong, make sure it's an error
152- stage1_thread = std::thread ([this ] {
153- this ->stage1_thread_error = this ->thread_parser .ensure_capacity (this ->batch_size );
154- if (!this ->stage1_thread_error ) {
155- this ->stage1_thread_error = run_stage1 (this ->stage1_thread_parser , this ->next_batch_start ());
156- }
151+ size_t _next_batch_start = this ->next_batch_start ();
152+ stage1_thread = std::thread ([this , _next_batch_start] {
153+ this ->stage1_thread_error = run_stage1 (this ->stage1_thread_parser , _next_batch_start);
157154 });
158155}
159156
0 commit comments