Skip to content

Commit d4a37f6

Browse files
committed
Enable conversion warnings on Linux and Windows
1 parent a5c9c31 commit d4a37f6

37 files changed

+242
-229
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ set(THREADS_PREFER_PTHREAD_FLAG ON)
5252
add_library(simdjson-flags INTERFACE)
5353
if(MSVC)
5454
target_compile_options(simdjson-flags INTERFACE /nologo /D_CRT_SECURE_NO_WARNINGS)
55-
target_compile_options(simdjson-flags INTERFACE /WX /W3 /wd4267 /wd4244)
55+
target_compile_options(simdjson-flags INTERFACE /WX /W3)
5656
else()
5757
target_compile_options(simdjson-flags INTERFACE -fPIC)
58-
target_compile_options(simdjson-flags INTERFACE -Werror -Wall -Wextra -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self)
58+
target_compile_options(simdjson-flags INTERFACE -Werror -Wall -Wextra -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion)
5959
endif()
6060

6161
# Optional flags

benchmark/benchmark.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ uint64_t global_rdtsc_overhead = (uint64_t)UINT64_MAX;
6060
do { \
6161
uint64_t cycles_start, cycles_final, cycles_diff; \
6262
uint64_t min_diff = UINT64_MAX; \
63-
for (int i = 0; i < repeat; i++) { \
63+
for (decltype(repeat) i = 0; i < repeat; i++) { \
6464
__asm volatile("" ::: /* pretend to clobber */ "memory"); \
6565
RDTSC_START(cycles_start); \
6666
test; \
@@ -73,7 +73,7 @@ uint64_t global_rdtsc_overhead = (uint64_t)UINT64_MAX;
7373
} while (0)
7474

7575
double diff(timespec start, timespec end) {
76-
return ((end.tv_nsec + 1000000000 * end.tv_sec) -
76+
return static_cast<double>((end.tv_nsec + 1000000000 * end.tv_sec) -
7777
(start.tv_nsec + 1000000000 * start.tv_sec)) /
7878
1000000000.0;
7979
}
@@ -100,7 +100,7 @@ double diff(timespec start, timespec end) {
100100
uint64_t sum_diff = 0; \
101101
double sumclockdiff = 0; \
102102
struct timespec time1, time2; \
103-
for (int i = 0; i < repeat; i++) { \
103+
for (decltype(repeat) i = 0; i < repeat; i++) { \
104104
pre; \
105105
__asm volatile("" ::: /* pretend to clobber */ "memory"); \
106106
clock_gettime(CLOCK_REALTIME, &time1); \
@@ -121,12 +121,12 @@ double diff(timespec start, timespec end) {
121121
sum_diff += cycles_diff; \
122122
} \
123123
uint64_t S = size; \
124-
float cycle_per_op = (min_diff) / (double)S; \
125-
float avg_cycle_per_op = (sum_diff) / ((double)S * repeat); \
124+
double cycle_per_op = static_cast<double>(min_diff) / static_cast<double>(S); \
125+
double avg_cycle_per_op = static_cast<double>(sum_diff) / (static_cast<double>(S) * static_cast<double>(repeat)); \
126126
double avg_gb_per_s = \
127-
((double)S * repeat) / ((sumclockdiff)*1000.0 * 1000.0 * 1000.0); \
127+
(static_cast<double>(S) * static_cast<double>(repeat)) / ((sumclockdiff)*1000.0 * 1000.0 * 1000.0); \
128128
double max_gb_per_s = \
129-
((double)S) / ((min_sumclockdiff)*1000.0 * 1000.0 * 1000.0); \
129+
static_cast<double>(S) / (min_sumclockdiff * 1000.0 * 1000.0 * 1000.0); \
130130
if (verbose) \
131131
printf(" %7.3f %s per input byte (best) ", cycle_per_op, unitname); \
132132
if (verbose) \
@@ -137,7 +137,7 @@ double diff(timespec start, timespec end) {
137137
if (verbose) \
138138
printf(" %13.0f documents/s (best)", 1.0/min_sumclockdiff); \
139139
if (verbose) \
140-
printf(" %13.0f documents/s (avg)", 1.0/(sumclockdiff/repeat)); \
140+
printf(" %13.0f documents/s (avg)", 1.0/(sumclockdiff/static_cast<double>(repeat))); \
141141
if (!verbose) \
142142
printf(" %20.3f %20.3f %20.3f %20.3f", cycle_per_op, \
143143
avg_cycle_per_op - cycle_per_op, max_gb_per_s, \
@@ -170,8 +170,8 @@ double diff(timespec start, timespec end) {
170170
sum_diff += cycles_diff; \
171171
} \
172172
uint64_t S = size; \
173-
float cycle_per_op = (min_diff) / (double)S; \
174-
float avg_cycle_per_op = (sum_diff) / ((double)S * repeat); \
173+
double cycle_per_op = static_cast<double>(min_diff) / static_cast<double>(S); \
174+
double avg_cycle_per_op = static_cast<double>(sum_diff) / (static_cast<double>(S) * static_cast<double>(repeat)); \
175175
if (verbose) \
176176
printf(" %.3f %s per input byte (best) ", cycle_per_op, unitname); \
177177
if (verbose) \

benchmark/benchmarker.h

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -379,46 +379,53 @@ struct benchmarker {
379379
printf("%s%-13s: %8.4f ns per block (%6.2f%%) - %8.4f ns per byte - %8.4f ns per structural - %8.3f GB/s\n",
380380
prefix,
381381
"Speed",
382-
stage.elapsed_ns() / stats->blocks, // per block
383-
100.0 * stage.elapsed_sec() / all_stages.elapsed_sec(), // %
384-
stage.elapsed_ns() / stats->bytes, // per byte
385-
stage.elapsed_ns() / stats->structurals, // per structural
386-
(json.size() / 1000000000.0) / stage.elapsed_sec() // GB/s
382+
stage.elapsed_ns() / static_cast<double>(stats->blocks), // per block
383+
percent(stage.elapsed_sec(), all_stages.elapsed_sec()), // %
384+
stage.elapsed_ns() / static_cast<double>(stats->bytes), // per byte
385+
stage.elapsed_ns() / static_cast<double>(stats->structurals), // per structural
386+
(static_cast<double>(json.size()) / 1000000000.0) / stage.elapsed_sec() // GB/s
387387
);
388388

389389
if (collector.has_events()) {
390390
printf("%s%-13s: %8.4f per block (%6.2f%%) - %8.4f per byte - %8.4f per structural - %8.3f GHz est. frequency\n",
391391
prefix,
392392
"Cycles",
393-
stage.cycles() / stats->blocks,
394-
100.0 * stage.cycles() / all_stages.cycles(),
395-
stage.cycles() / stats->bytes,
396-
stage.cycles() / stats->structurals,
393+
stage.cycles() / static_cast<double>(stats->blocks),
394+
percent(stage.cycles(), all_stages.cycles()),
395+
stage.cycles() / static_cast<double>(stats->bytes),
396+
stage.cycles() / static_cast<double>(stats->structurals),
397397
(stage.cycles() / stage.elapsed_sec()) / 1000000000.0
398398
);
399399
printf("%s%-13s: %8.4f per block (%6.2f%%) - %8.4f per byte - %8.4f per structural - %8.3f per cycle\n",
400400
prefix,
401401
"Instructions",
402-
stage.instructions() / stats->blocks,
403-
100.0 * stage.instructions() / all_stages.instructions(),
404-
stage.instructions() / stats->bytes,
405-
stage.instructions() / stats->structurals,
406-
stage.instructions() / stage.cycles()
402+
stage.instructions() / static_cast<double>(stats->blocks),
403+
percent(stage.instructions(), all_stages.instructions()),
404+
stage.instructions() / static_cast<double>(stats->bytes),
405+
stage.instructions() / static_cast<double>(stats->structurals),
406+
stage.instructions() / static_cast<double>(stage.cycles())
407407
);
408408

409409
// NOTE: removed cycles/miss because it is a somewhat misleading stat
410410
printf("%s%-13s: %7.0f branch misses (%6.2f%%) - %.0f cache misses (%6.2f%%) - %.2f cache references\n",
411411
prefix,
412412
"Misses",
413413
stage.branch_misses(),
414-
100.0 * stage.branch_misses() / all_stages.branch_misses(),
414+
percent(stage.branch_misses(), all_stages.branch_misses()),
415415
stage.cache_misses(),
416-
100.0 * stage.cache_misses() / all_stages.cache_misses(),
416+
percent(stage.cache_misses(), all_stages.cache_misses()),
417417
stage.cache_references()
418418
);
419419
}
420420
}
421421

422+
static double percent(size_t a, size_t b) {
423+
return 100.0 * static_cast<double>(a) / static_cast<double>(b);
424+
}
425+
static double percent(double a, double b) {
426+
return 100.0 * a / b;
427+
}
428+
422429
void print(bool tabbed_output, size_t iterations) const {
423430
if (tabbed_output) {
424431
char* filename_copy = (char*)malloc(strlen(filename)+1);
@@ -432,14 +439,14 @@ struct benchmarker {
432439
base[strlen(base)-5] = '\0';
433440
}
434441

435-
double gb = json.size() / 1000000000.0;
442+
double gb = static_cast<double>(json.size()) / 1000000000.0;
436443
if (collector.has_events()) {
437444
printf("\"%s\"\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",
438445
base,
439-
allocate_stage.best.cycles() / json.size(),
440-
stage1.best.cycles() / json.size(),
441-
stage2.best.cycles() / json.size(),
442-
all_stages.best.cycles() / json.size(),
446+
allocate_stage.best.cycles() / static_cast<double>(json.size()),
447+
stage1.best.cycles() / static_cast<double>(json.size()),
448+
stage2.best.cycles() / static_cast<double>(json.size()),
449+
all_stages.best.cycles() / static_cast<double>(json.size()),
443450
gb / all_stages.best.elapsed_sec(),
444451
gb / stage1.best.elapsed_sec(),
445452
gb / stage2.best.elapsed_sec());
@@ -455,22 +462,22 @@ struct benchmarker {
455462
printf("\n");
456463
printf("%s\n", filename);
457464
printf("%s\n", string(strlen(filename), '=').c_str());
458-
printf("%9zu blocks - %10zu bytes - %5zu structurals (%5.1f %%)\n", stats->bytes / BYTES_PER_BLOCK, stats->bytes, stats->structurals, 100.0 * stats->structurals / stats->bytes);
465+
printf("%9zu blocks - %10zu bytes - %5zu structurals (%5.1f %%)\n", stats->bytes / BYTES_PER_BLOCK, stats->bytes, stats->structurals, percent(stats->structurals, stats->bytes));
459466
if (stats) {
460467
printf("special blocks with: utf8 %9zu (%5.1f %%) - escape %9zu (%5.1f %%) - 0 structurals %9zu (%5.1f %%) - 1+ structurals %9zu (%5.1f %%) - 8+ structurals %9zu (%5.1f %%) - 16+ structurals %9zu (%5.1f %%)\n",
461-
stats->blocks_with_utf8, 100.0 * stats->blocks_with_utf8 / stats->blocks,
462-
stats->blocks_with_escapes, 100.0 * stats->blocks_with_escapes / stats->blocks,
463-
stats->blocks_with_0_structurals, 100.0 * stats->blocks_with_0_structurals / stats->blocks,
464-
stats->blocks_with_1_structural, 100.0 * stats->blocks_with_1_structural / stats->blocks,
465-
stats->blocks_with_8_structurals, 100.0 * stats->blocks_with_8_structurals / stats->blocks,
466-
stats->blocks_with_16_structurals, 100.0 * stats->blocks_with_16_structurals / stats->blocks);
468+
stats->blocks_with_utf8, percent(stats->blocks_with_utf8, stats->blocks),
469+
stats->blocks_with_escapes, percent(stats->blocks_with_escapes, stats->blocks),
470+
stats->blocks_with_0_structurals, percent(stats->blocks_with_0_structurals, stats->blocks),
471+
stats->blocks_with_1_structural, percent(stats->blocks_with_1_structural, stats->blocks),
472+
stats->blocks_with_8_structurals, percent(stats->blocks_with_8_structurals, stats->blocks),
473+
stats->blocks_with_16_structurals, percent(stats->blocks_with_16_structurals, stats->blocks));
467474
printf("special block flips: utf8 %9zu (%5.1f %%) - escape %9zu (%5.1f %%) - 0 structurals %9zu (%5.1f %%) - 1+ structurals %9zu (%5.1f %%) - 8+ structurals %9zu (%5.1f %%) - 16+ structurals %9zu (%5.1f %%)\n",
468-
stats->blocks_with_utf8_flipped, 100.0 * stats->blocks_with_utf8_flipped / stats->blocks,
469-
stats->blocks_with_escapes_flipped, 100.0 * stats->blocks_with_escapes_flipped / stats->blocks,
470-
stats->blocks_with_0_structurals_flipped, 100.0 * stats->blocks_with_0_structurals_flipped / stats->blocks,
471-
stats->blocks_with_1_structural_flipped, 100.0 * stats->blocks_with_1_structural_flipped / stats->blocks,
472-
stats->blocks_with_8_structurals_flipped, 100.0 * stats->blocks_with_8_structurals_flipped / stats->blocks,
473-
stats->blocks_with_16_structurals_flipped, 100.0 * stats->blocks_with_16_structurals_flipped / stats->blocks);
475+
stats->blocks_with_utf8_flipped, percent(stats->blocks_with_utf8_flipped, stats->blocks),
476+
stats->blocks_with_escapes_flipped, percent(stats->blocks_with_escapes_flipped, stats->blocks),
477+
stats->blocks_with_0_structurals_flipped, percent(stats->blocks_with_0_structurals_flipped, stats->blocks),
478+
stats->blocks_with_1_structural_flipped, percent(stats->blocks_with_1_structural_flipped, stats->blocks),
479+
stats->blocks_with_8_structurals_flipped, percent(stats->blocks_with_8_structurals_flipped, stats->blocks),
480+
stats->blocks_with_16_structurals_flipped, percent(stats->blocks_with_16_structurals_flipped, stats->blocks));
474481
}
475482
printf("\n");
476483
printf("All Stages\n");
@@ -497,7 +504,7 @@ struct benchmarker {
497504
freqmin, freqmax, freqall);
498505
}
499506
}
500-
printf("\n%.1f documents parsed per second\n", iterations/loop.best.elapsed_sec());
507+
printf("\n%.1f documents parsed per second\n", static_cast<double>(iterations)/static_cast<double>(loop.best.elapsed_sec()));
501508
}
502509
}
503510
};

benchmark/distinctuseridcompetition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ int main(int argc, char *argv[]) {
366366
size_t size = s1.size();
367367

368368
int repeat = 500;
369-
int volume = p.size();
369+
size_t volume = p.size();
370370
if (just_data) {
371371
printf(
372372
"name cycles_per_byte cycles_per_byte_err gb_per_s gb_per_s_err \n");

benchmark/event_counter.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ struct event_count {
5656

5757
double elapsed_sec() const { return duration<double>(elapsed).count(); }
5858
double elapsed_ns() const { return duration<double, std::nano>(elapsed).count(); }
59-
double cycles() const { return event_counts[CPU_CYCLES]; }
60-
double instructions() const { return event_counts[INSTRUCTIONS]; }
61-
double branch_misses() const { return event_counts[BRANCH_MISSES]; }
62-
double cache_references() const { return event_counts[CACHE_REFERENCES]; }
63-
double cache_misses() const { return event_counts[CACHE_MISSES]; }
59+
double cycles() const { return static_cast<double>(event_counts[CPU_CYCLES]); }
60+
double instructions() const { return static_cast<double>(event_counts[INSTRUCTIONS]); }
61+
double branch_misses() const { return static_cast<double>(event_counts[BRANCH_MISSES]); }
62+
double cache_references() const { return static_cast<double>(event_counts[CACHE_REFERENCES]); }
63+
double cache_misses() const { return static_cast<double>(event_counts[CACHE_MISSES]); }
6464

6565
event_count& operator=(const event_count& other) {
6666
this->elapsed = other.elapsed;

benchmark/get_corpus_benchmark.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ double bench(std::string filename, simdjson::padded_string& p) {
1212
std::chrono::time_point<std::chrono::steady_clock> end_clock =
1313
std::chrono::steady_clock::now();
1414
std::chrono::duration<double> elapsed = end_clock - start_clock;
15-
return (p.size() / (1024. * 1024 * 1024.)) / elapsed.count();
15+
return (static_cast<double>(p.size()) / (1024. * 1024. * 1024.)) / elapsed.count();
1616
}
1717

1818
int main(int argc, char *argv[]) {
@@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
3232
double meanval = 0;
3333
double maxval = 0;
3434
double minval = 10000;
35-
std::cout << "file size: "<< (p.size() / (1024. * 1024 * 1024.)) << " GB" <<std::endl;
35+
std::cout << "file size: "<< (static_cast<double>(p.size()) / (1024. * 1024. * 1024.)) << " GB" <<std::endl;
3636
size_t times = p.size() > 1024*1024*1024 ? 5 : 50;
3737
#if __cpp_exceptions
3838
try {
@@ -49,7 +49,7 @@ std::cout << "file size: "<< (p.size() / (1024. * 1024 * 1024.)) << " GB" <<std
4949
return EXIT_FAILURE;
5050
}
5151
#endif
52-
std::cout << "average speed: " << meanval / times << " GB/s"<< std::endl;
52+
std::cout << "average speed: " << meanval / static_cast<double>(times) << " GB/s"<< std::endl;
5353
std::cout << "min speed : " << minval << " GB/s" << std::endl;
5454
std::cout << "max speed : " << maxval << " GB/s" << std::endl;
5555
return EXIT_SUCCESS;

benchmark/linux/linux-perf-events.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
1818
int fd;
1919
bool working;
2020
perf_event_attr attribs;
21-
int num_events;
21+
size_t num_events;
2222
std::vector<uint64_t> temp_result_vec;
2323
std::vector<uint64_t> ids;
2424

@@ -43,7 +43,7 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
4343
uint32_t i = 0;
4444
for (auto config : config_vec) {
4545
attribs.config = config;
46-
fd = syscall(__NR_perf_event_open, &attribs, pid, cpu, group, flags);
46+
fd = static_cast<int>(syscall(__NR_perf_event_open, &attribs, pid, cpu, group, flags));
4747
if (fd == -1) {
4848
report_error("perf_event_open");
4949
}

benchmark/minifiercompetition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ int main(int argc, char *argv[]) {
8787
buffer[p.size()] = '\0';
8888

8989
int repeat = 50;
90-
int volume = p.size();
90+
size_t volume = p.size();
9191
if (just_data) {
9292
printf(
9393
"name cycles_per_byte cycles_per_byte_err gb_per_s gb_per_s_err \n");
@@ -113,7 +113,7 @@ int main(int argc, char *argv[]) {
113113

114114
printf("minisize = %zu, original size = %zu (minified down to %.2f percent "
115115
"of original) \n",
116-
outlength, p.size(), outlength * 100.0 / p.size());
116+
outlength, p.size(), static_cast<double>(outlength) * 100.0 / static_cast<double>(p.size()));
117117

118118
/***
119119
* Is it worth it to minify before parsing?

benchmark/parse_stream.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int main (int argc, char *argv[]){
5656
auto end = std::chrono::steady_clock::now();
5757

5858
std::chrono::duration<double> secs = end - start;
59-
double speedinGBs = (p.size()) / (secs.count() * 1000000000.0);
59+
double speedinGBs = static_cast<double>(p.size()) / (static_cast<double>(secs.count()) * 1000000000.0);
6060
std::cout << speedinGBs << "\t\t\t\t" << count << std::endl;
6161

6262
if (parse_res != simdjson::SUCCESS) {
@@ -88,7 +88,7 @@ int main (int argc, char *argv[]){
8888
auto end = std::chrono::steady_clock::now();
8989

9090
std::chrono::duration<double> secs = end - start;
91-
double speedinGBs = (p.size()) / (secs.count() * 1000000000.0);
91+
double speedinGBs = static_cast<double>(p.size()) / (static_cast<double>(secs.count()) * 1000000000.0);
9292
if (speedinGBs > batch_size_res.at(i))
9393
batch_size_res[i] = speedinGBs;
9494

@@ -136,7 +136,7 @@ int main (int argc, char *argv[]){
136136
}
137137

138138
double min_result = *min_element(res.begin(), res.end());
139-
double speedinGBs = (p.size()) / (min_result * 1000000000.0);
139+
double speedinGBs = static_cast<double>(p.size()) / (min_result * 1000000000.0);
140140

141141

142142
std::cout << "Min: " << min_result << " bytes read: " << p.size()

benchmark/parseandstatcompetition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ int main(int argc, char *argv[]) {
336336
assert(stat_equal(s1, s2));
337337
assert(stat_equal(s1, s3));
338338
int repeat = 50;
339-
int volume = p.size();
339+
size_t volume = p.size();
340340
if (just_data) {
341341
printf("name cycles_per_byte cycles_per_byte_err gb_per_s gb_per_s_err \n");
342342
}

0 commit comments

Comments
 (0)