Skip to content

Commit 6dcac93

Browse files
committed
Restore the GCC fixes, the benchmark warm-up and the CMake fix
e80f20b was committed from a checkout in which these files had been reverted to 96bc30b: it held only the README (which describes these changes) and a stray .pyc. Restore the code that the README's x86 numbers come from: the array and object loops inlined, values taken in place in simdjson results, headers written in place; the warm-up of the benchmarks; and FetchContent_MakeAvailable for the competition. Ignore Python caches.
1 parent e80f20b commit 6dcac93

7 files changed

Lines changed: 52 additions & 23 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
build/
22
build-*/
33
singleheader/
4+
__pycache__/
5+
*.pyc

‎CMakeLists.txt‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmake_minimum_required(VERSION 3.16)
2+
# (SIMDJSON_MSGPACK_COMPETITION needs CMake 3.18.)
23
project(simdjson_msgpack VERSION 0.1.0 LANGUAGES CXX)
34

45
set(CMAKE_CXX_STANDARD 17)
@@ -48,22 +49,21 @@ if(SIMDJSON_MSGPACK_BENCHMARKS)
4849
add_executable(bench benchmarks/bench.cpp)
4950
target_link_libraries(bench PRIVATE simdjson_msgpack)
5051
if(SIMDJSON_MSGPACK_COMPETITION)
52+
# Header-only (or without a CMakeLists.txt we want): SOURCE_SUBDIR names a
53+
# directory that does not exist, so that FetchContent only downloads them.
5154
FetchContent_Declare(nlohmann_json
5255
URL https://github.com/nlohmann/json/releases/download/v3.12.0/json.tar.xz
53-
DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
56+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE SOURCE_SUBDIR none)
5457
FetchContent_Declare(yyjson
5558
URL https://github.com/ibireme/yyjson/archive/refs/tags/0.12.0.tar.gz
5659
DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
5760
FetchContent_Declare(rapidjson
5861
URL https://github.com/Tencent/rapidjson/archive/24b5e7a8b27f42fa16b96fc70aade9106cf7102f.tar.gz
59-
DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
62+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE SOURCE_SUBDIR none)
6063
FetchContent_Declare(mpack
6164
URL https://github.com/ludocode/mpack/releases/download/v1.1.1/mpack-amalgamation-1.1.1.tar.gz
62-
DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
63-
FetchContent_MakeAvailable(yyjson)
64-
FetchContent_Populate(nlohmann_json) # header only
65-
FetchContent_Populate(rapidjson) # header only
66-
FetchContent_Populate(mpack)
65+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE SOURCE_SUBDIR none)
66+
FetchContent_MakeAvailable(yyjson nlohmann_json rapidjson mpack)
6767
add_library(mpack STATIC ${mpack_SOURCE_DIR}/src/mpack/mpack.c)
6868
target_include_directories(mpack PUBLIC ${mpack_SOURCE_DIR}/src/mpack)
6969
target_compile_definitions(mpack PUBLIC MPACK_EXTENSIONS=1)

‎benchmarks/bench.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ int main(int argc, char** argv) {
3737
return EXIT_FAILURE;
3838
}
3939
std::printf("%-28s %10s %10s %14s %14s\n", "file", "JSON MB", "msgpack MB", "to msgpack", "to JSON");
40+
warm_up();
4041
for (const char* file : files) {
4142
simdjson::padded_string json;
4243
if (simdjson::padded_string::load(file).get(json)) {

‎benchmarks/bench_competition.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ int main(int argc, char** argv) {
516516
std::fprintf(stderr, "usage: %s [--rounds N] file.json...\n", argv[0]);
517517
return EXIT_FAILURE;
518518
}
519+
warm_up();
519520
for (const char* file : files) {
520521
simdjson::padded_string json;
521522
if (simdjson::padded_string::load(file).get(json)) {

‎benchmarks/timing.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <algorithm>
66
#include <chrono>
77
#include <cstdio>
8+
#include <cstdint>
89
#include <cstdlib>
910
#include <string>
1011

@@ -36,6 +37,16 @@ double best_time(int rounds, F fn) {
3637
return best;
3738
}
3839

40+
// Keeps the core busy for about a second, so that the first measures do not
41+
// run at a low frequency.
42+
inline void warm_up() {
43+
auto t0 = std::chrono::steady_clock::now();
44+
volatile uint64_t x = 1;
45+
while (std::chrono::steady_clock::now() - t0 < std::chrono::seconds(1)) {
46+
for (int i = 0; i < 1000; i++) x = x * 6364136223846793005ULL + 1;
47+
}
48+
}
49+
3950
inline std::string basename_of(const char* path) {
4051
std::string p(path);
4152
size_t slash = p.find_last_of("/\\");

‎src/to_msgpack.cpp‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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};
-40.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)