Spaces:
Running
Running
Download bench/fanout.cpp from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 12.7 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/bench/fanout.cpp
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/bench/fanout.cpp
-
curl -L -o fanout.cpp https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/bench/fanout.cpp
12.7 kB
| // g++ -O3 -DNDEBUG -std=c++17 -Iinclude bench/fanout.cpp -o fanout && ./fanout | |
| // CSV reports medians of 9 interleaved trials, in microseconds per plan. | |
| // Each independent visit only increments a counter. Factored algorithms build | |
| // owned rows and per-reader lengths. Wire transfer and payload fetch are excluded. | |
| namespace { | |
| using sincekit::BatchPlan; | |
| using sincekit::BatchRow; | |
| using sincekit::Index; | |
| using Cursors = std::vector<std::uint64_t>; | |
| volatile std::uint64_t sink = 0; | |
| std::size_t fixed_step_rank(const std::vector<BatchRow>& rows, std::uint64_t cursor) { | |
| if (rows.empty() || rows.front().revision <= cursor) return 0; | |
| if (rows.size() == 1 || rows[1].revision <= cursor) return 1; | |
| if (rows.back().revision > cursor) return rows.size(); | |
| std::size_t first = 0; | |
| auto length = rows.size(); | |
| while (length > 1) { | |
| const auto half = length / 2; | |
| const auto middle = first + half; | |
| first = rows[middle].revision > cursor ? middle : first; | |
| length -= half; | |
| } | |
| return first + 1; | |
| } | |
| template<bool FixedStep> | |
| BatchPlan binary_plan(const Index& index, const Cursors& cursors) { | |
| BatchPlan plan; | |
| plan.revision = index.revision(); | |
| if (cursors.empty()) return plan; | |
| const auto bounds = std::minmax_element(cursors.begin(), cursors.end()); | |
| if (*bounds.second > plan.revision) throw std::out_of_range("future cursor"); | |
| plan.rows.reserve(sincekit::batch_detail::initial_rows(index, *bounds.first, cursors.size())); | |
| index.since(*bounds.first, [&](auto id, auto rev, auto deleted) { | |
| plan.rows.push_back({rev, id, deleted}); | |
| }); | |
| if (*bounds.first == *bounds.second || plan.rows.empty()) { | |
| plan.prefix_lengths.assign(cursors.size(), plan.rows.size()); | |
| return plan; | |
| } | |
| plan.prefix_lengths.resize(cursors.size()); | |
| for (std::size_t i = 0; i < cursors.size(); ++i) { | |
| if constexpr (FixedStep) { | |
| plan.prefix_lengths[i] = fixed_step_rank(plan.rows, cursors[i]); | |
| } else { | |
| auto it = std::lower_bound(plan.rows.begin(), plan.rows.end(), cursors[i], | |
| [](const BatchRow& row, auto cursor) { return row.revision > cursor; }); | |
| plan.prefix_lengths[i] = static_cast<std::size_t>(it - plan.rows.begin()); | |
| } | |
| } | |
| return plan; | |
| } | |
| // Sort cursors once, then advance a single pointer over newest-first rows. | |
| // Ascending and descending input orders avoid the permutation allocation. | |
| BatchPlan sweep_plan(const Index& index, const Cursors& cursors) { | |
| BatchPlan plan; | |
| plan.revision = index.revision(); | |
| if (cursors.empty()) return plan; | |
| const auto bounds = std::minmax_element(cursors.begin(), cursors.end()); | |
| if (*bounds.second > plan.revision) throw std::out_of_range("future cursor"); | |
| plan.rows.reserve(sincekit::batch_detail::initial_rows(index, *bounds.first, cursors.size())); | |
| index.since(*bounds.first, [&](auto id, auto rev, auto deleted) { | |
| plan.rows.push_back({rev, id, deleted}); | |
| }); | |
| if (*bounds.first == *bounds.second || plan.rows.empty()) { | |
| plan.prefix_lengths.assign(cursors.size(), plan.rows.size()); | |
| return plan; | |
| } | |
| plan.prefix_lengths.resize(cursors.size()); | |
| std::size_t count = 0; | |
| auto assign = [&](std::size_t reader, std::uint64_t cursor) { | |
| while (count < plan.rows.size() && plan.rows[count].revision > cursor) ++count; | |
| plan.prefix_lengths[reader] = count; | |
| }; | |
| if (std::is_sorted(cursors.begin(), cursors.end(), std::greater<std::uint64_t>{})) { | |
| for (std::size_t i = 0; i < cursors.size(); ++i) assign(i, cursors[i]); | |
| } else if (std::is_sorted(cursors.begin(), cursors.end())) { | |
| for (std::size_t i = cursors.size(); i != 0; --i) assign(i - 1, cursors[i - 1]); | |
| } else { | |
| std::vector<std::pair<std::uint64_t, std::size_t>> ordered; | |
| ordered.reserve(cursors.size()); | |
| for (std::size_t i = 0; i < cursors.size(); ++i) ordered.emplace_back(cursors[i], i); | |
| std::sort(ordered.begin(), ordered.end(), std::greater<>{}); | |
| for (const auto& reader : ordered) assign(reader.second, reader.first); | |
| } | |
| return plan; | |
| } | |
| // Escape both buffers so that the timed binary, sweep, and adaptive paths | |
| // must materialize the same plan contents. | |
| inline void observe(const void* p, const void* q) { | |
| asm volatile("" : : "g"(p), "g"(q) : "memory"); | |
| static const void* volatile observed[2]; | |
| observed[0] = p; | |
| observed[1] = q; | |
| std::atomic_signal_fence(std::memory_order_seq_cst); | |
| } | |
| std::uint64_t independent_count(const Index& index, const Cursors& cursors) { | |
| std::uint64_t count = 0; | |
| for (auto cursor : cursors) index.since(cursor, [&](auto, auto, auto) { ++count; }); | |
| return count; | |
| } | |
| std::uint64_t logical_count(const BatchPlan& plan) { | |
| observe(plan.rows.data(), plan.prefix_lengths.data()); | |
| return std::accumulate(plan.prefix_lengths.begin(), plan.prefix_lengths.end(), std::uint64_t{0}); | |
| } | |
| void verify(const Index& index, const Cursors& cursors, const BatchPlan& plan) { | |
| if (binary_plan<false>(index, cursors).prefix_lengths != plan.prefix_lengths || | |
| binary_plan<true>(index, cursors).prefix_lengths != plan.prefix_lengths || | |
| sweep_plan(index, cursors).prefix_lengths != plan.prefix_lengths) { | |
| throw std::runtime_error("binary reference disagrees"); | |
| } | |
| for (std::size_t reader = 0; reader < cursors.size(); ++reader) { | |
| std::size_t n = 0; | |
| index.since(cursors[reader], [&](auto id, auto rev, auto deleted) { | |
| const auto& row = plan.rows.at(n++); | |
| if (row.id != id || row.revision != rev || row.deleted != deleted) { | |
| throw std::runtime_error("row mismatch"); | |
| } | |
| }); | |
| if (n != plan.prefix_lengths.at(reader)) throw std::runtime_error("reader length mismatch"); | |
| } | |
| } | |
| void run(const std::string& name, const Index& index, const Cursors& cursors, int repetitions, bool raw) { | |
| auto plan = sincekit::since_many(index, cursors); | |
| verify(index, cursors, plan); | |
| const auto expected = logical_count(plan); | |
| std::array<std::vector<double>, 5> times; | |
| // Rotate the first algorithm, and verify each timed trial's logical count. | |
| for (int trial = 0; trial <= 9; ++trial) { | |
| for (std::size_t j = 0; j < times.size(); ++j) { | |
| const auto method = (j + static_cast<std::size_t>(trial)) % times.size(); | |
| std::uint64_t sum = 0; | |
| auto start = std::chrono::steady_clock::now(); | |
| for (int repetition = 0; repetition < repetitions; ++repetition) { | |
| observe(&index, cursors.data()); | |
| switch (method) { | |
| case 0: sum += independent_count(index, cursors); break; | |
| case 1: sum += logical_count(binary_plan<false>(index, cursors)); break; | |
| case 2: sum += logical_count(binary_plan<true>(index, cursors)); break; | |
| case 3: sum += logical_count(sweep_plan(index, cursors)); break; | |
| case 4: sum += logical_count(sincekit::since_many(index, cursors)); break; | |
| } | |
| } | |
| auto duration = std::chrono::steady_clock::now() - start; | |
| if (sum != expected * static_cast<std::uint64_t>(repetitions)) { | |
| throw std::runtime_error("timed result mismatch"); | |
| } | |
| sink = sink + sum; | |
| if (trial != 0) { | |
| times[method].push_back(std::chrono::duration<double, std::micro>(duration).count() / | |
| repetitions); | |
| } | |
| } | |
| } | |
| auto print = [&](const std::string& trial, std::size_t position) { | |
| std::cout << name << ',' << trial << ',' << repetitions << ',' << cursors.size() | |
| << ',' << plan.rows.size() << ',' << expected; | |
| for (const auto& sample : times) { | |
| std::cout << ',' << std::fixed << std::setprecision(4) << sample[position]; | |
| } | |
| std::cout << '\n'; | |
| }; | |
| if (raw) { | |
| for (std::size_t i = 0; i < times[0].size(); ++i) print(std::to_string(i + 1), i); | |
| } else { | |
| for (auto& sample : times) std::sort(sample.begin(), sample.end()); | |
| print("median", times[0].size() / 2); | |
| } | |
| } | |
| } // namespace | |
| int main(int argc, char** argv) { | |
| if (argc > 2 || (argc == 2 && std::string(argv[1]) != "--raw")) { | |
| std::cerr << "usage: fanout [--raw]\n"; | |
| return 2; | |
| } | |
| const bool raw = argc == 2; | |
| std::mt19937_64 rng(918219); | |
| std::cout << "scenario,trial,repetitions,readers,shared_rows,logical_deliveries,independent_count_us," | |
| "union_lower_bound_us,union_fixed_step_us,union_sorted_sweep_us,since_many_us\n"; | |
| Index contiguous(8192); | |
| for (std::uint32_t i = 0; i < 8192; ++i) contiguous.touch(i); | |
| Cursors cursors(2048); | |
| for (auto& c : cursors) c = rng() % (contiguous.revision() + 1); | |
| run("contiguous_uniform_age", contiguous, cursors, 1, raw); | |
| Index churn(16384); | |
| for (int i = 0; i < 65536; ++i) { | |
| auto id = static_cast<std::uint32_t>(rng() % churn.capacity()); | |
| if (rng() % 5 == 0) churn.erase(id); | |
| else churn.touch(id); | |
| } | |
| cursors.resize(4096); | |
| for (auto& c : cursors) c = rng() % (churn.revision() + 1); | |
| run("churn_uniform_age", churn, cursors, 1, raw); | |
| std::sort(cursors.begin(), cursors.end(), std::greater<>{}); | |
| run("churn_presorted_age", churn, cursors, 1, raw); | |
| for (auto& c : cursors) { | |
| const auto age = (rng() % 20 != 0) ? rng() % 64 : rng() % (churn.revision() + 1); | |
| c = churn.revision() - age; | |
| } | |
| run("churn_95pct_recent", churn, cursors, 1, raw); | |
| Index sparse(256); | |
| for (std::uint32_t id = 0; id < 256; ++id) sparse.touch(id); | |
| for (int i = 0; i < 100000; ++i) sparse.touch(0); | |
| cursors.resize(2048); | |
| for (auto& c : cursors) c = rng() % (sparse.revision() + 1); | |
| cursors[0] = 0; | |
| run("sparse_old_gap", sparse, cursors, 100, raw); | |
| // Post-tuning holdouts for the long-gap workload. A fixed, independent | |
| // seed drives nine hot keys. The second stream has periodic cold writes. | |
| for (const bool heartbeat : {false, true}) { | |
| std::mt19937_64 heldout(3093553); | |
| Index multi(521); | |
| for (std::uint32_t id = 0; id < multi.capacity(); ++id) multi.touch(id); | |
| for (std::uint32_t t = 1; t <= 170000; ++t) { | |
| const auto id = heartbeat && t % 8191 == 0 | |
| ? 9u + static_cast<std::uint32_t>(heldout() % 512) | |
| : static_cast<std::uint32_t>(heldout() % 9); | |
| multi.touch(id); | |
| } | |
| cursors.resize(3072); | |
| for (auto& c : cursors) { | |
| const auto age = heldout() % 4 == 0 ? heldout() % 512 : | |
| heldout() % (multi.revision() + 1); | |
| c = multi.revision() - age; | |
| } | |
| cursors[0] = 0; | |
| run(heartbeat ? "sparse_nine_hot_heartbeat" : "sparse_nine_hot", | |
| multi, cursors, 40, raw); | |
| } | |
| // Held back until after the sparse directory design was frozen. Hot keys | |
| // rotate, cold writes arrive in variable-length bursts, and many readers | |
| // share a cursor at a burst boundary. | |
| std::mt19937_64 shifted_rng(7314273); | |
| Index shifted(1024); | |
| for (std::uint32_t id = 0; id < shifted.capacity(); ++id) shifted.touch(id); | |
| Cursors burst_starts; | |
| std::uint32_t next_burst = 511; | |
| for (std::uint32_t t = 1; t <= 160000; ++t) { | |
| if (t == next_burst) { | |
| burst_starts.push_back(shifted.revision()); | |
| const auto burst = 2 + shifted_rng() % 29; | |
| for (unsigned j = 0; j < burst; ++j) { | |
| shifted.touch(85 + static_cast<std::uint32_t>(shifted_rng() % 939)); | |
| } | |
| next_burst += 503 + static_cast<std::uint32_t>(shifted_rng() % 8500); | |
| } | |
| const auto band = 17 * ((t / 29000) % 5); | |
| shifted.touch(band + static_cast<std::uint32_t>(shifted_rng() % 17)); | |
| } | |
| cursors.resize(2048); | |
| for (auto& c : cursors) { | |
| if (shifted_rng() % 3 == 0) c = burst_starts[shifted_rng() % burst_starts.size()]; | |
| else c = shifted_rng() % (shifted.revision() + 1); | |
| } | |
| cursors[0] = 0; | |
| run("sparse_rotating17_cold_bursts", shifted, cursors, 16, raw); | |
| run("one_reader_current", churn, {churn.revision()}, 1000, raw); | |
| } | |