@@ -66,11 +66,12 @@ simdjson_inline uint8_t* put_header(uint8_t* p, const header& h) {
6666
6767// Puts the smallest headers in place in the document [begin, end), moving
6868// back what follows them; returns the new end.
69- uint8_t * compact (uint8_t * begin, uint8_t * end, const std::vector< header>& headers) {
70- if (headers. empty () ) return end;
69+ uint8_t * compact (uint8_t * begin, uint8_t * end, const header* headers, size_t count ) {
70+ if (count == 0 ) return end;
7171 uint8_t * out = begin + headers[0 ].offset ;
7272 const uint8_t * in = out;
73- for (const header& h : headers) {
73+ for (size_t i = 0 ; i < count; i++) {
74+ const header& h = headers[i];
7475 const uint8_t * at = begin + h.offset ;
7576 const size_t n = size_t (at - in);
7677 std::memmove (out, in, n);
@@ -93,10 +94,10 @@ class encoder {
9394 template <typename document>
9495 error_code write (document& doc, uint8_t * out, uint8_t ** end) {
9596 begin_ = out_ = out;
96- headers_. clear () ;
97+ count_ = 0 ;
9798 error_code e = any (doc);
9899 if (e) return e;
99- *end = compact (begin_, out_, headers_);
100+ *end = compact (begin_, out_, headers_. data (), count_ );
100101 return SUCCESS ;
101102 }
102103
@@ -164,9 +165,8 @@ class encoder {
164165 const size_t h = open (false );
165166 uint64_t count = 0 ;
166167 for (auto child : a) {
167- ondemand::value v;
168- SIMDJSON_TRY (child.get (v));
169- SIMDJSON_TRY (element (v));
168+ SIMDJSON_TRY (child.error ());
169+ SIMDJSON_TRY (element (in_place (child)));
170170 count++;
171171 }
172172 return close (h, count);
@@ -176,18 +176,30 @@ class encoder {
176176 const size_t h = open (true );
177177 uint64_t count = 0 ;
178178 for (auto f : o) {
179- ondemand::field field ;
180- SIMDJSON_TRY ( std::move (f). get (field) );
179+ SIMDJSON_TRY (f. error ()) ;
180+ ondemand::field& field = in_place (f );
181181 SIMDJSON_TRY (string (field.key ()));
182182 SIMDJSON_TRY (element (field.value ()));
183183 count++;
184184 }
185185 return close (h, count);
186186 }
187187
188+ // The value in a result (which has no error), without a copy: GCC copies
189+ // results through the stack with stores that loads cannot forward from.
190+ template <typename T>
191+ static simdjson_inline T& in_place (simdjson_result<T>& r) {
192+ return const_cast <T&>(r.value_unsafe ());
193+ }
194+
188195 simdjson_inline size_t open (bool is_map) {
189- const size_t h = headers_.size ();
190- headers_.push_back (header{size_t (out_ - begin_), 0 , is_map});
196+ // We write the fields in place, rather than push a header built on the
197+ // stack (GCC would copy it with a load that cannot be forwarded to).
198+ const size_t h = count_++;
199+ if (simdjson_unlikely (h == headers_.size ())) headers_.resize (headers_.empty () ? 64 : 2 * h);
200+ header& slot = headers_[h];
201+ slot.offset = size_t (out_ - begin_);
202+ slot.is_map = is_map;
191203 out_ += header_room;
192204 return h;
193205 }
@@ -280,9 +292,10 @@ class encoder {
280292 simdjson_inline error_code string (ondemand::raw_json_string s) {
281293 uint8_t * const at = out_;
282294 uint8_t * end = at + 1 ;
283- std::string_view v;
284- SIMDJSON_TRY (parser_.unescape (s, end).get (v));
285- const size_t n = v.size ();
295+ // unescape moves end past the string: we take only the error (copying
296+ // the whole result costs GCC a store-forwarding stall).
297+ SIMDJSON_TRY (parser_.unescape (s, end).error ());
298+ const size_t n = size_t (end - (at + 1 ));
286299 if (simdjson_likely (n < 32 )) {
287300 *at = uint8_t (0xa0 | n);
288301 } else if (n <= 0xff ) {
@@ -307,7 +320,8 @@ class encoder {
307320 }
308321
309322 ondemand::parser& parser_;
310- std::vector<header>& headers_;
323+ std::vector<header>& headers_; // the first count_ are in use
324+ size_t count_{0 };
311325 const msgpack_options& options_;
312326 uint8_t * begin_{nullptr };
313327 uint8_t * out_{nullptr };
0 commit comments