@@ -897,7 +897,7 @@ PoocCloseFillViewSwitches& pooc_close_fill_view_switches() noexcept {
897897} // namespace detail
898898
899899// The retired-row compaction: the placement table's storage.
900- void PlacementTable::place(std::uint64_t incarnation, std::unique_ptr<PlacementSnapshot> node) {
900+ void PlacementTable::place(std::uint64_t incarnation, std::unique_ptr<PlacementSnapshot> node) { if (old_only_) return place_old(incarnation, std::move(node));
901901 if (window_rows_ == 0 && (old_.empty() || incarnation > old_.back().first)) {
902902 // An empty window restarts at the new row, above every older row.
903903 window_.clear();
@@ -909,28 +909,28 @@ void PlacementTable::place(std::uint64_t incarnation, std::unique_ptr<PlacementS
909909 // window starts at, remembered only now): among the older rows.
910910 const auto at = std::lower_bound(old_.begin(), old_.end(), incarnation,
911911 [](const auto& entry, std::uint64_t key) { return entry.first < key; });
912- old_.emplace(at, incarnation, std::move(node));
912+ placement_work_ += static_cast<std::uint64_t>(old_.end() - at) + 1U; old_.emplace(at, incarnation, std::move(node));
913913 return;
914914 }
915915 const auto index = head_ + static_cast<std::size_t>(incarnation - base_);
916- if (index >= window_.size()) window_.resize (index + 1U );
917- window_[index] = std::move(node);
916+ if (index >= window_.size()) grow_window (index);
917+ ++placement_work_; window_[index] = std::move(node);
918918 ++window_rows_;
919919}
920920
921921std::unique_ptr<PlacementSnapshot> PlacementTable::take(std::uint64_t incarnation) noexcept {
922- if (incarnation >= base_) {
922+ if (!old_only_ && incarnation >= base_) {
923923 const std::uint64_t offset = incarnation - base_;
924924 if (offset >= window_.size() - head_) return nullptr;
925925 auto& slot = window_[head_ + static_cast<std::size_t>(offset)];
926- if (slot) --window_rows_;
926+ if (slot) { --window_rows_; ++removal_work_; }
927927 return std::move(slot);
928928 }
929929 const auto found = std::lower_bound(old_.begin(), old_.end(), incarnation,
930- [](const auto& entry, std::uint64_t key) { return entry.first < key; });
930+ [this ](const auto& entry, std::uint64_t key) { ++lookup_work_; return entry.first < key; });
931931 if (found == old_.end() || found->first != incarnation) return nullptr;
932932 auto node = std::move(found->second);
933- old_.erase(found);
933+ removal_work_ += static_cast<std::uint64_t>(old_.end() - found); old_.erase(found);
934934 return node;
935935}
936936
@@ -952,7 +952,7 @@ void PlacementTable::remember_erased(const PlacementSnapshot& row) {
952952 sizeof row.command_sequence);
953953}
954954
955- void PlacementTable::compact() {
955+ void PlacementTable::compact() { if (compact_old()) return;
956956 constexpr std::size_t kDenseFloor = 64;
957957 for (;;) {
958958 while (head_ < window_.size() && !window_[head_]) {
@@ -29960,12 +29960,36 @@ void PlacementTable::prepare_place(std::uint64_t incarnation) {
2996029960 + std::min(rows.capacity(), rows.max_size() - rows.capacity());
2996129961 rows.reserve(std::max(required, grown));
2996229962 };
29963+ if (old_only_) {
29964+ reserve(old_, old_.size() + 1U);
29965+ return;
29966+ }
2996329967 if (window_rows_ == 0 && (old_.empty() || incarnation > old_.back().first)) {
2996429968 reserve(window_, 1);
2996529969 } else if (incarnation < base_) {
2996629970 reserve(old_, old_.size() + 1);
2996729971 } else {
2996829972 const auto index = head_ + static_cast<std::size_t>(incarnation - base_);
29973+ constexpr std::size_t kDenseFloor = 64;
29974+ const auto span = std::max(window_.size() - head_,
29975+ static_cast<std::size_t>(incarnation - base_) + 1U);
29976+ const bool dense = span <= kDenseFloor || span <= 4U * window_rows_;
29977+ if (window_rows_ != 0 && !dense) {
29978+ reserve(old_, old_.size() + window_rows_ + 1U);
29979+ for (std::size_t slot = head_; slot < window_.size(); ++slot) {
29980+ if (!window_[slot]) continue;
29981+ old_.emplace_back(
29982+ base_ + static_cast<std::uint64_t>(slot - head_),
29983+ std::move(window_[slot]));
29984+ ++placement_work_;
29985+ }
29986+ window_.clear();
29987+ head_ = 0;
29988+ window_rows_ = 0;
29989+ base_ = std::numeric_limits<std::uint64_t>::max();
29990+ old_only_ = true;
29991+ return;
29992+ }
2996929993 if (index >= window_.size()) reserve(window_, index + 1);
2997029994 }
2997129995}
@@ -30568,3 +30592,45 @@ LookupIndexCounts probe_lookup_index(const NativeHostCache* cache) noexcept {
3056830592}
3056930593
3057030594} // namespace pineforge::source::detail
30595+
30596+ namespace pineforge::source {
30597+
30598+ // Storage policy and work counters are derived observers, outside broker
30599+ // state. Keep these bodies after the adapter's cited implementation regions.
30600+ void PlacementTable::reset_storage_work() noexcept {
30601+ old_only_ = false;
30602+ placement_work_ = 0;
30603+ lookup_work_ = 0;
30604+ removal_work_ = 0;
30605+ }
30606+
30607+ void PlacementTable::grow_window(std::size_t index) {
30608+ placement_work_ += static_cast<std::uint64_t>(index + 1U - window_.size());
30609+ window_.resize(index + 1U);
30610+ }
30611+
30612+ void PlacementTable::place_old(std::uint64_t incarnation,
30613+ std::unique_ptr<PlacementSnapshot> node) {
30614+ // Monotone incarnations append; an earlier incarnation still uses the
30615+ // same sorted representation and shifts only the rows above it.
30616+ const auto at = old_.empty() || incarnation > old_.back().first
30617+ ? old_.end()
30618+ : std::lower_bound(old_.begin(), old_.end(), incarnation,
30619+ [this](const auto& entry, std::uint64_t key) {
30620+ ++lookup_work_;
30621+ return entry.first < key;
30622+ });
30623+ placement_work_ += static_cast<std::uint64_t>(old_.end() - at) + 1U;
30624+ old_.emplace(at, incarnation, std::move(node));
30625+ }
30626+
30627+ bool PlacementTable::compact_old() noexcept {
30628+ if (!old_only_) return false;
30629+ if (old_.empty()) {
30630+ old_only_ = false;
30631+ base_ = high_water_ + 1U;
30632+ }
30633+ return true;
30634+ }
30635+
30636+ } // namespace pineforge::source
0 commit comments