Skip to content

Commit

Permalink
[hive] Benchmark for D2596R0's "un-criticism"
Browse files Browse the repository at this point in the history
  • Loading branch information
Quuxplusone committed Jun 10, 2022
1 parent 7eabd14 commit e0012e6
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${SG14_INCLUDE_DIRECTORY}>
)

add_subdirectory(benchmarks)
add_subdirectory(test)

install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}_targets)
Expand Down
23 changes: 23 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

find_package(benchmark REQUIRED)

set(BENCH_SOURCE_FILES
hive_bench.cpp
)

set(BENCH_NAME ubench)
add_executable(${BENCH_NAME} ${BENCH_SOURCE_FILES})
include_directories(${BENCHMARK_INCLUDE_DIRS} ${SG14_INCLUDE_DIRECTORY})
target_link_libraries(${BENCH_NAME} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(${BENCH_NAME} benchmark::benchmark_main)

# Compile options
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(${BENCH_NAME} PRIVATE -Wall -Wextra -Werror)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(${BENCH_NAME} PRIVATE -Wall -Wextra -Werror)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${BENCH_NAME})
target_compile_options(${BENCH_NAME} PRIVATE /Zc:__cplusplus /permissive- /W4 /WX)
add_definitions(-DNOMINMAX -D_SCL_SECURE_NO_WARNINGS)
endif()
182 changes: 182 additions & 0 deletions benchmarks/hive_bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@

// You can omit this line, but then we'll spend most of our time in std::next.
#define PLF_HIVE_RANDOM_ACCESS_ITERATORS 1

#define PLF_HIVE_P2596 0
#include <sg14/plf_hive.h>
#undef PLF_HIVE_P2596

#undef PLF_HIVE_H
#define plf p2596
#define PLF_HIVE_P2596 1
#include <sg14/plf_hive.h>
#undef PLF_HIVE_P2596
#undef plf

#undef PLF_HIVE_H
#define plf matt
#include "../../plf_hive/plf_hive.h"
#undef plf

#include <benchmark/benchmark.h>

struct xoshiro256ss {
using u64 = unsigned long long;
u64 s[4] {};

static constexpr u64 splitmix64(u64& x) {
u64 z = (x += 0x9e3779b97f4a7c15uLL);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9uLL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebuLL;
return z ^ (z >> 31);
}

constexpr explicit xoshiro256ss() : xoshiro256ss(0) {}

constexpr explicit xoshiro256ss(u64 seed) {
s[0] = splitmix64(seed);
s[1] = splitmix64(seed);
s[2] = splitmix64(seed);
s[3] = splitmix64(seed);
}

using result_type = u64;
static constexpr u64 min() { return 0; }
static constexpr u64 max() { return u64(-1); }

static constexpr u64 rotl(u64 x, int k) {
return (x << k) | (x >> (64 - k));
}

constexpr u64 operator()() {
u64 result = rotl(s[1] * 5, 7) * 9;
u64 t = s[1] << 17;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = rotl(s[3], 45);
return result;
}
};

#define N 1000

#if N < 256
static void BM_PlfStackIssue1_MattSmart(benchmark::State& state) {
int fake_input[N] = {};
xoshiro256ss g;
matt::hive<int> h;
h.reshape({N, N});

for (auto _ : state) {
for (int t = 0; t < 100; ++t) {
if (g() % 2 || h.empty()) {
h.insert(fake_input, fake_input + N);
} else {
h.erase(h.begin(), std::next(h.begin(), N));
}
}
}
benchmark::DoNotOptimize(h);
}
BENCHMARK(BM_PlfStackIssue1_MattSmart);

static void BM_PlfStackIssue1_MattNaive(benchmark::State& state) {
int fake_input[N] = {};
xoshiro256ss g;
matt::hive<int> h;

for (auto _ : state) {
for (int t = 0; t < 100; ++t) {
if (g() % 2 || h.empty()) {
h.insert(fake_input, fake_input + N);
} else {
h.erase(h.begin(), std::next(h.begin(), N));
}
}
}
benchmark::DoNotOptimize(h);
}
BENCHMARK(BM_PlfStackIssue1_MattNaive);
#endif

static void BM_PlfStackIssue1_OldSmart(benchmark::State& state) {
int fake_input[N] = {};
xoshiro256ss g;
plf::hive<int> h;
h.reshape({N, N});

for (auto _ : state) {
for (int t = 0; t < 100; ++t) {
if (g() % 2 || h.empty()) {
h.insert(fake_input, fake_input + N);
} else {
h.erase(h.begin(), std::next(h.begin(), N));
}
}
}
benchmark::DoNotOptimize(h);
}
BENCHMARK(BM_PlfStackIssue1_OldSmart);

static void BM_PlfStackIssue1_OldNaive(benchmark::State& state) {
int fake_input[N] = {};
xoshiro256ss g;
plf::hive<int> h;

for (auto _ : state) {
for (int t = 0; t < 100; ++t) {
if (g() % 2 || h.empty()) {
h.insert(fake_input, fake_input + N);
} else {
h.erase(h.begin(), std::next(h.begin(), N));
}
}
}
benchmark::DoNotOptimize(h);
}
BENCHMARK(BM_PlfStackIssue1_OldNaive);

static void BM_PlfStackIssue1_NewSmart(benchmark::State& state) {
int fake_input[N] = {};
xoshiro256ss g;
p2596::hive<int> h;
for (auto _ : state) {
for (int t = 0; t < 100; ++t) {
if (g() % 2 || h.empty()) {
if (h.capacity() == h.size()) {
p2596::hive<int> temp;
temp.reserve(N);
h.splice(temp);
}
h.insert(fake_input, fake_input + N);
} else {
h.erase(h.begin(), std::next(h.begin(), N));
}
}
}
benchmark::DoNotOptimize(h);
}
BENCHMARK(BM_PlfStackIssue1_NewSmart);

static void BM_PlfStackIssue1_NewNaive(benchmark::State& state) {
int fake_input[N] = {};
xoshiro256ss g;
p2596::hive<int> h;

for (auto _ : state) {
for (int t = 0; t < 100; ++t) {
if (g() % 2 || h.empty()) {
h.insert(fake_input, fake_input + N);
} else {
h.erase(h.begin(), std::next(h.begin(), N));
}
}
}
benchmark::DoNotOptimize(h);
}
BENCHMARK(BM_PlfStackIssue1_NewNaive);

BENCHMARK_MAIN();

0 comments on commit e0012e6

Please sign in to comment.