Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/memory/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cc_binary(
srcs = ["memcpy_benchmark.cpp"],
deps = [
"//lib/memory:memcpy",
"//lib/memory/copy:copy",
"@google_benchmark//:benchmark_main",
],
)
45 changes: 40 additions & 5 deletions benchmarks/memory/memcpy_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "lib/memory/copy/copy.h"
#include "lib/memory/memcpy.h"

#include <cstddef>
Expand All @@ -18,14 +19,14 @@ std::vector<std::byte> MakeInput(std::size_t size) {
return input;
}

void BM_Memcpy(benchmark::State& state) {
template <auto CopyFn>
void BM_Copy(benchmark::State& state) {
const auto size = static_cast<std::size_t>(state.range(0));
const std::vector<std::byte> source = MakeInput(size);
std::vector<std::byte> destination(size);

for (auto _ : state) {
memcopy_bench::memory::CopyWithMemcpy(
destination.data(), source.data(), size);
CopyFn(destination.data(), source.data(), size);
benchmark::DoNotOptimize(destination.data());
benchmark::ClobberMemory();
}
Expand All @@ -34,8 +35,42 @@ void BM_Memcpy(benchmark::State& state) {
static_cast<std::int64_t>(size));
}

BENCHMARK(BM_Memcpy)->RangeMultiplier(4)->Range(64, 64 << 20);
inline void* CopyNaiveWrapper(void* d, const void* s, std::size_t n) {
return memory_bench::copy_naive(d, s, n);
}

} // namespace

template <auto Fn>
void RegisterBMCopy(const char *name)
{
benchmark::RegisterBenchmark(name, BM_Copy<Fn>)
->RangeMultiplier(4)
->Range(64, 64 << 20);
}

static const bool Registered = []
{
RegisterBMCopy<memcopy_bench::memory::CopyWithMemcpy>("BM_Memcpy");
RegisterBMCopy<CopyNaiveWrapper>("BM_CopyNaive");
RegisterBMCopy<memory_bench::copy_duff>("BM_CopyDuff");
RegisterBMCopy<memory_bench::copy_words64>("BM_CopyWords64");
RegisterBMCopy<memory_bench::copy_words64_unrolled4>("BM_CopyWords64Unrolled4");
RegisterBMCopy<memory_bench::copy_small_overlapping>("BM_CopySmallOverlapping");
return true;
}();

} // namespace
#if defined(__x86_64__) || defined(_M_X64)
static const bool x86Registered = []
{
RegisterBMCopy<memory_bench::copy_rep_movsb>("BM_CopyRepMovsb");
RegisterBMCopy<memory_bench::copy_rep_movsq>("BM_CopyRepMovsq");
RegisterBMCopy<memory_bench::copy_sse2>("BM_CopySSE2");
RegisterBMCopy<memory_bench::copy_avx2>("BM_CopyAVX2");
RegisterBMCopy<memory_bench::copy_avx2_stream>("BM_CopyAVX2Stream");
RegisterBMCopy<memory_bench::copy_avx2_prefetch>("BM_CopyAVX2Prefetch");
return true;
}();
#endif

BENCHMARK_MAIN();
39 changes: 39 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Set default recipe to show available commands
default:
@just --list

# Build all targets in the workspace
build:
bazel build //...

# Run all unit tests with error logs on failure
test:
bazel test //... --test_output=errors

# Run AddressSanitizer and UndefinedBehaviorSanitizer checks
sanitizers:
bazel test --config=asan //... --test_output=errors
bazel test --config=ubsan //... --test_output=errors

# Run C++ linter (clang-tidy)
lint:
sh tools/run-clang-tidy.sh

# Run complete local verification (format, lint, tests, and sanitizers)
check: lint test sanitizers

# Run benchmarks (optional filter argument: `just bench BM_Memcpy`)
bench filter="":
bazel run //benchmarks/memory:memcpy_benchmark -- {{ if filter != "" { "--benchmark_filter=" + filter } else { "" } }}

# Refresh compile_commands.json for clangd and IDE completion
compile-commands:
bazel run @hedron_compile_commands//:refresh_all

# Remove local build outputs and symlinks
clean:
bazel clean

# Wipe the entire local Bazel cache and reset external downloads
clean-all:
bazel clean --expunge
17 changes: 17 additions & 0 deletions lib/memory/copy/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

cc_library(
name = "copy",
hdrs = ["copy.h"],
visibility = ["//visibility:public"],
)

cc_test(
name = "copy_test",
srcs = ["copy_test.cpp"],
deps = [
":copy",
"@googletest//:gtest_main",
],
)
Loading