|
| 1 | +#include <pineforge/source/pine_adapter.hpp> |
| 2 | + |
| 3 | +#include <cstddef> |
| 4 | +#include <cstdint> |
| 5 | +#include <cstdio> |
| 6 | +#include <stdexcept> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace { |
| 11 | +using namespace pineforge; |
| 12 | +using source::PlacementSnapshot; |
| 13 | +using source::PlacementTable; |
| 14 | + |
| 15 | +struct PlacementWorkTag { |
| 16 | + using type = std::uint64_t PlacementTable::*; |
| 17 | + friend type access(PlacementWorkTag); |
| 18 | +}; |
| 19 | + |
| 20 | +struct LookupWorkTag { |
| 21 | + using type = std::uint64_t PlacementTable::*; |
| 22 | + friend type access(LookupWorkTag); |
| 23 | +}; |
| 24 | + |
| 25 | +struct RemovalWorkTag { |
| 26 | + using type = std::uint64_t PlacementTable::*; |
| 27 | + friend type access(RemovalWorkTag); |
| 28 | +}; |
| 29 | + |
| 30 | +struct OldOnlyTag { |
| 31 | + using type = bool PlacementTable::*; |
| 32 | + friend type access(OldOnlyTag); |
| 33 | +}; |
| 34 | + |
| 35 | +template<class Tag, typename Tag::type Member> |
| 36 | +struct PrivateAccess { |
| 37 | + friend typename Tag::type access(Tag) { return Member; } |
| 38 | +}; |
| 39 | + |
| 40 | +template struct PrivateAccess<PlacementWorkTag, &PlacementTable::placement_work_>; |
| 41 | +template struct PrivateAccess<LookupWorkTag, &PlacementTable::lookup_work_>; |
| 42 | +template struct PrivateAccess<RemovalWorkTag, &PlacementTable::removal_work_>; |
| 43 | +template struct PrivateAccess<OldOnlyTag, &PlacementTable::old_only_>; |
| 44 | + |
| 45 | +struct Result { |
| 46 | + std::size_t live = 0; |
| 47 | + std::uint64_t work = 0; |
| 48 | +}; |
| 49 | + |
| 50 | +void require(bool condition, const std::string& message) { |
| 51 | + if (!condition) throw std::runtime_error(message); |
| 52 | +} |
| 53 | + |
| 54 | +PlacementSnapshot resting_limit(std::size_t serial) { |
| 55 | + PlacementSnapshot row; |
| 56 | + row.family = source::PineOrderFamily::Order; |
| 57 | + row.source_id = "resting-book"; |
| 58 | + row.comment = "never-fill-" + std::to_string(serial); |
| 59 | + row.requested_qty = 1.0; |
| 60 | + row.is_long = true; |
| 61 | + row.opening = true; |
| 62 | + row.exit_levels.limit = 1.0; |
| 63 | + row.command_sequence = static_cast<std::uint64_t>(serial + 1); |
| 64 | + return row; |
| 65 | +} |
| 66 | + |
| 67 | +std::uint64_t incarnation_for(std::size_t serial) { |
| 68 | + // The native handle space can contain gaps when other accepted commands |
| 69 | + // share a run. The gap grows with the input serial without using a |
| 70 | + // population-specific literal. |
| 71 | + const auto stride = serial / 100U + 1U; |
| 72 | + return 1U + static_cast<std::uint64_t>(serial) * stride; |
| 73 | +} |
| 74 | + |
| 75 | +Result place_book(std::size_t count) { |
| 76 | + PlacementTable table; |
| 77 | + for (std::size_t serial = 0; serial < count; ++serial) { |
| 78 | + const auto incarnation = incarnation_for(serial); |
| 79 | + const auto inserted = table.try_emplace(incarnation, resting_limit(serial)); |
| 80 | + require(inserted.second, "every never-fill limit placement has a fresh incarnation"); |
| 81 | + } |
| 82 | + |
| 83 | + require(table.size() == count, |
| 84 | + "live placement count follows the input: " + std::to_string(count)); |
| 85 | + std::uint64_t previous = 0; |
| 86 | + std::size_t visited = 0; |
| 87 | + table.visit_rows([&](std::uint64_t incarnation, const PlacementSnapshot& row) { |
| 88 | + require(incarnation > previous, "placement rows remain incarnation ordered"); |
| 89 | + require(row.family == source::PineOrderFamily::Order |
| 90 | + && row.exit_levels.limit == 1.0 |
| 91 | + && row.comment == "never-fill-" + std::to_string(visited), |
| 92 | + "each input limit remains a distinct never-fill order"); |
| 93 | + previous = incarnation; |
| 94 | + ++visited; |
| 95 | + return false; |
| 96 | + }); |
| 97 | + require(visited == count, "every live order is present after placement"); |
| 98 | + |
| 99 | + const auto work = table.*access(PlacementWorkTag{}); |
| 100 | + std::printf("placement-order N=%zu live=%zu work=%llu per_order=%.3f\n", |
| 101 | + count, table.size(), static_cast<unsigned long long>(work), |
| 102 | + static_cast<double>(work) / static_cast<double>(count)); |
| 103 | + return {table.size(), work}; |
| 104 | +} |
| 105 | + |
| 106 | +void density_and_access_work(std::size_t count, bool sparse) { |
| 107 | + PlacementTable table; |
| 108 | + table.fixture_use_memory_auxiliary_tables(); |
| 109 | + const std::uint64_t stride = sparse ? count + 1U : 1U; |
| 110 | + const auto key = [&](std::size_t serial) { return 1U + serial * stride; }; |
| 111 | + for (std::size_t serial = 0; serial != count; ++serial) { |
| 112 | + require(table.try_emplace(key(serial), resting_limit(serial)).second, |
| 113 | + "density input must contain distinct rows"); |
| 114 | + require((table.*access(OldOnlyTag{})) == (sparse && serial != 0), |
| 115 | + "only a sparse book should enter ordered storage"); |
| 116 | + } |
| 117 | + |
| 118 | + // One comparison is counted at each binary-search predicate evaluation. |
| 119 | + // The bound comes from the input population, rather than a pinned result. |
| 120 | + std::uint64_t comparison_bound = 1; |
| 121 | + for (auto remaining = count; remaining > 1; remaining /= 2U) ++comparison_bound; |
| 122 | + const auto before_lookup = table.*access(LookupWorkTag{}); |
| 123 | + const PlacementTable& rows = table; |
| 124 | + for (std::size_t serial = 0; serial != count; ++serial) { |
| 125 | + const auto found = rows.find(key(serial)); |
| 126 | + require(found != rows.end() && found->second.comment == resting_limit(serial).comment, |
| 127 | + "lookup must return the input row"); |
| 128 | + require(rows.contains(key(serial)), "contains must find every input row"); |
| 129 | + require(rows.at(key(serial)).command_sequence == serial + 1U, |
| 130 | + "at must return the input sequence"); |
| 131 | + } |
| 132 | + const auto lookup_work = (table.*access(LookupWorkTag{})) - before_lookup; |
| 133 | + require(sparse ? lookup_work >= 3U * count : lookup_work == 0, |
| 134 | + "ordered lookup comparisons must be measured; dense lookup stays direct"); |
| 135 | + require(lookup_work <= 3U * count * comparison_bound, "lookup exceeds binary-search bound"); |
| 136 | + |
| 137 | + const auto before_removal = table.*access(RemovalWorkTag{}); |
| 138 | + const auto before_search = table.*access(LookupWorkTag{}); |
| 139 | + const auto retired = count / 2U; |
| 140 | + std::uint64_t expected_removal_work = 0; |
| 141 | + for (std::size_t serial = 0; serial != retired; ++serial) { |
| 142 | + // Erasing the oldest row shifts every remaining vector element. |
| 143 | + // Count the removed element too; a dense slot removal costs one. |
| 144 | + expected_removal_work += sparse ? count - serial : 1U; |
| 145 | + require(table.erase(key(serial)), "oldest-half removal must succeed"); |
| 146 | + } |
| 147 | + const auto removal_work = (table.*access(RemovalWorkTag{})) - before_removal; |
| 148 | + const auto removal_search = (table.*access(LookupWorkTag{})) - before_search; |
| 149 | + require(removal_work == expected_removal_work, "removal did not count actual shifted rows"); |
| 150 | + require(sparse ? removal_search >= 2U * retired : removal_search == 0, |
| 151 | + "removal must measure its locate and take searches"); |
| 152 | + require(removal_search <= 2U * retired * comparison_bound, |
| 153 | + "removal search exceeds binary-search bound"); |
| 154 | + require(table.size() == count - retired && table.erased_rows() == retired, |
| 155 | + "removal must preserve the requested live and retired populations"); |
| 156 | + for (std::size_t serial = 0; serial != count; ++serial) |
| 157 | + require(rows.contains(key(serial)) == (serial >= retired), |
| 158 | + "converted removal changed the wrong row"); |
| 159 | + std::printf("placement-access N=%zu sparse=%u lookup=%llu removal_search=%llu removal=%llu\n", |
| 160 | + count, static_cast<unsigned>(sparse), |
| 161 | + static_cast<unsigned long long>(lookup_work), |
| 162 | + static_cast<unsigned long long>(removal_search), |
| 163 | + static_cast<unsigned long long>(removal_work)); |
| 164 | +} |
| 165 | +} // namespace |
| 166 | + |
| 167 | +int main() { |
| 168 | + try { |
| 169 | + for (const auto count : {258U, 516U, 1032U}) { |
| 170 | + density_and_access_work(count, false); |
| 171 | + density_and_access_work(count, true); |
| 172 | + } |
| 173 | + const std::vector<std::size_t> sizes{1000, 5000, 20000}; |
| 174 | + std::vector<Result> results; |
| 175 | + results.reserve(sizes.size()); |
| 176 | + for (const auto count : sizes) results.push_back(place_book(count)); |
| 177 | + |
| 178 | + for (std::size_t index = 0; index < results.size(); ++index) |
| 179 | + require(results[index].live == sizes[index], |
| 180 | + "the measured population is not the requested population"); |
| 181 | + |
| 182 | + for (std::size_t index = 1; index < results.size(); ++index) { |
| 183 | + // The work per placement may vary by a constant representation |
| 184 | + // transition, but it cannot rise with the live book. |
| 185 | + require(results[index].work * sizes[0] |
| 186 | + <= results[0].work * sizes[index] * 2U, |
| 187 | + "placement work per order grows with the live order book"); |
| 188 | + require(results[index].work <= sizes[index] * 4U, |
| 189 | + "placement work exceeds a constant amortised bound"); |
| 190 | + } |
| 191 | + return 0; |
| 192 | + } catch (const std::exception& error) { |
| 193 | + std::fprintf(stderr, "test_placement_order_scaling: %s\n", error.what()); |
| 194 | + return 1; |
| 195 | + } |
| 196 | +} |
0 commit comments