Skip to content

Commit af3cd2e

Browse files
author
Daniel Lemire
committed
reflect cpp does not build under LLVM 19, disable
1 parent d6cf29b commit af3cd2e

6 files changed

Lines changed: 97 additions & 63 deletions

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CPMAddPackage("gh:nlohmann/json@3.10.5")
2424
CPMAddPackage(
2525
NAME reflect-cpp
2626
GITHUB_REPOSITORY getml/reflect-cpp
27-
GIT_TAG main
27+
GIT_TAG v0.13.0
2828
)
2929

3030
# Optionally, specify the C++ compiler directly (here we use clang++)

benchmarks/src/CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,22 @@ add_executable(SerializationTwitterBenchmark benchmark_serialization_twitter.cpp
5656
target_compile_options(SerializationBenchmark PRIVATE -freflection -stdlib=libc++ -std=c++26)
5757
target_compile_options(SerializationTwitterBenchmark PRIVATE -freflection -stdlib=libc++ -std=c++26)
5858

59-
#find_package(CURL REQUIRED)
6059

6160
# Link sanitizers and libraries
62-
target_link_libraries(SerializationTwitterBenchmark PRIVATE nlohmann_json::nlohmann_json simdjson::simdjson reflectcpp)
61+
target_link_libraries(SerializationTwitterBenchmark PRIVATE nlohmann_json::nlohmann_json simdjson::simdjson )
62+
63+
# cpp-reflect may fail to build on LLVM 19 due to this issue
64+
# https://github.com/hanickadot/compile-time-regular-expressions/issues/307
65+
option(SIMDJSON_BENCH_CPP_REFLECT "include cpp reflect in the benchmarks" OFF)
66+
if(SIMDJSON_BENCH_CPP_REFLECT)
67+
message(STATUS "Including reflect-cpp in the benchmarks.")
68+
add_library(benchmark_reflect_serialization_twitter STATIC benchmark_reflect_serialization_twitter.cpp)
69+
target_link_libraries(benchmark_reflect_serialization_twitter PRIVATE reflectcpp)
70+
target_link_libraries(SerializationTwitterBenchmark PRIVATE benchmark_reflect_serialization_twitter)
71+
target_compile_definitions(benchmark_reflect_serialization_twitter PRIVATE SIMDJSON_BENCH_CPP_REFLECT=1)
72+
endif()
73+
74+
6375
target_link_libraries(SerializationBenchmark PRIVATE simdjson-serial nlohmann_json::nlohmann_json simdjson::simdjson)
6476

6577
target_compile_definitions(SerializationTwitterBenchmark PRIVATE JSON_FILE="${CMAKE_CURRENT_SOURCE_DIR}/data/twitter.json")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef BENCHMARK_HELPER_HPP
2+
#define BENCHMARK_HELPER_HPP
3+
#include "event_counter.h"
4+
5+
inline event_collector& get_collector() {
6+
static event_collector collector;
7+
return collector;
8+
}
9+
10+
template <class function_type>
11+
event_aggregate bench(const function_type &function, size_t min_repeat = 10,
12+
size_t min_time_ns = 1000000000,
13+
size_t max_repeat = 100000) {
14+
event_collector& collector = get_collector();
15+
event_aggregate aggregate{};
16+
size_t N = min_repeat;
17+
if (N == 0) {
18+
N = 1;
19+
}
20+
for (size_t i = 0; i < N; i++) {
21+
std::atomic_thread_fence(std::memory_order_acquire);
22+
collector.start();
23+
function();
24+
std::atomic_thread_fence(std::memory_order_release);
25+
event_count allocate_count = collector.end();
26+
aggregate << allocate_count;
27+
if ((i + 1 == N) && (aggregate.total_elapsed_ns() < min_time_ns) &&
28+
(N < max_repeat)) {
29+
N *= 10;
30+
}
31+
}
32+
return aggregate;
33+
}
34+
35+
// Source of the 2 functions below:
36+
// https://github.com/simdutf/simdutf/blob/master/benchmarks/base64/benchmark_base64.cpp
37+
inline void pretty_print(size_t strings, size_t bytes, std::string name, event_aggregate agg) {
38+
event_collector& collector = get_collector();
39+
printf("%-60s : ", name.c_str());
40+
printf(" %5.2f MB/s ", bytes * 1000 / agg.elapsed_ns());
41+
printf(" %5.2f Ms/s ", strings * 1000 / agg.elapsed_ns());
42+
if (collector.has_events()) {
43+
printf(" %5.2f GHz ", agg.cycles() / agg.elapsed_ns());
44+
printf(" %5.2f c/b ", agg.cycles() / bytes);
45+
printf(" %5.2f i/b ", agg.instructions() / bytes);
46+
printf(" %5.2f i/c ", agg.instructions() / agg.cycles());
47+
}
48+
printf("\n");
49+
}
50+
#endif
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "benchmark_reflect_serialization_twitter.hpp"
2+
#include "benchmark_helper.hpp"
3+
4+
#include <rfl.hpp>
5+
#include <rfl/json.hpp>
6+
void bench_reflect_cpp(TwitterData& data) {
7+
std::string output = rfl::json::write(data);
8+
size_t output_volume = output.size();
9+
printf("# output volume: %zu bytes\n", output_volume);
10+
11+
volatile size_t measured_volume = 0;
12+
pretty_print(
13+
1, output_volume, "bench_reflect_cpp",
14+
bench([&data, &measured_volume, &output_volume] () {
15+
std::string output = rfl::json::write(data);
16+
measured_volume = output.size();
17+
if(measured_volume != output_volume) { printf("mismatch\n"); }
18+
})
19+
);
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef BENCHMARK_REFLECT_SERIALIZATION_TWITTER_HPP
2+
#define BENCHMARK_REFLECT_SERIALIZATION_TWITTER_HPP
3+
4+
#include "twitter_data.hpp"
5+
void bench_reflect_cpp(TwitterData& data);
6+
#endif

benchmarks/src/benchmark_serialization_twitter.cpp

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "../../src/from_json.hpp"
22
#include "twitter_data.hpp"
33
#include "nlohmann_twitter_data.hpp"
4-
#include "event_counter.h"
54
#include <cassert>
65
#include <cstdlib>
76
#include <ctime>
@@ -12,49 +11,10 @@
1211
#include <simdjson.h>
1312
#include <fstream>
1413
#include <nlohmann/json.hpp>
15-
#include <rfl.hpp>
16-
#include <rfl/json.hpp>
17-
18-
event_collector collector;
19-
20-
template <class function_type>
21-
event_aggregate bench(const function_type &function, size_t min_repeat = 10,
22-
size_t min_time_ns = 1000000000,
23-
size_t max_repeat = 100000) {
24-
event_aggregate aggregate{};
25-
size_t N = min_repeat;
26-
if (N == 0) {
27-
N = 1;
28-
}
29-
for (size_t i = 0; i < N; i++) {
30-
std::atomic_thread_fence(std::memory_order_acquire);
31-
collector.start();
32-
function();
33-
std::atomic_thread_fence(std::memory_order_release);
34-
event_count allocate_count = collector.end();
35-
aggregate << allocate_count;
36-
if ((i + 1 == N) && (aggregate.total_elapsed_ns() < min_time_ns) &&
37-
(N < max_repeat)) {
38-
N *= 10;
39-
}
40-
}
41-
return aggregate;
42-
}
43-
44-
// Source of the 2 functions below:
45-
// https://github.com/simdutf/simdutf/blob/master/benchmarks/base64/benchmark_base64.cpp
46-
void pretty_print(size_t strings, size_t bytes, std::string name, event_aggregate agg) {
47-
printf("%-60s : ", name.c_str());
48-
printf(" %5.2f MB/s ", bytes * 1000 / agg.elapsed_ns());
49-
printf(" %5.2f Ms/s ", strings * 1000 / agg.elapsed_ns());
50-
if (collector.has_events()) {
51-
printf(" %5.2f GHz ", agg.cycles() / agg.elapsed_ns());
52-
printf(" %5.2f c/b ", agg.cycles() / bytes);
53-
printf(" %5.2f i/b ", agg.instructions() / bytes);
54-
printf(" %5.2f i/c ", agg.instructions() / agg.cycles());
55-
}
56-
printf("\n");
57-
}
14+
#include "benchmark_helper.hpp"
15+
#if SIMDJSON_BENCH_CPP_REFLECT
16+
#include "benchmark_reflect_serialization_twitter.hpp"
17+
#endif
5818

5919
template <class T>
6020
void bench_fast_simpler(T &data) {
@@ -92,21 +52,6 @@ void bench_nlohmann(TwitterData &data) {
9252
);
9353
}
9454

95-
void bench_reflect_cpp(TwitterData& data) {
96-
std::string output = rfl::json::write(data);
97-
size_t output_volume = output.size();
98-
printf("# output volume: %zu bytes\n", output_volume);
99-
100-
volatile size_t measured_volume = 0;
101-
pretty_print(
102-
1, output_volume, "bench_reflect_cpp",
103-
bench([&data, &measured_volume, &output_volume] () {
104-
std::string output = rfl::json::write(data);
105-
measured_volume = output.size();
106-
if(measured_volume != output_volume) { printf("mismatch\n"); }
107-
})
108-
);
109-
}
11055

11156
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
11257
((std::string*)userp)->append((char*)contents, size * nmemb);
@@ -233,7 +178,8 @@ int main()
233178
experimental_json_builder::from_json(json_value, my_struct);
234179
bench_fast_simpler(my_struct);
235180
bench_nlohmann(my_struct);
181+
#if SIMDJSON_BENCH_CPP_REFLECT
236182
bench_reflect_cpp(my_struct);
237-
183+
#endif
238184
return EXIT_SUCCESS;
239185
}

0 commit comments

Comments
 (0)