Skip to content

Commit 783016f

Browse files
luisleo526claude
andcommitted
Exit scratch: publish the live-handle copies in batches of 64 rows when no I/O observer is installed
The adapter's two flat live-handle copies (the unconditional snapshots taken when an Applied callback finds a flat position) put each handle into the exit scratch as its own transaction, so N rows cost N publications. They now run in one insertion transaction that publishes every 64 rows (the existing exit-scope batch constant) and once more for a nonempty tail: 0, 1, 64 and 65 rows publish 0, 1, 1 and 2 times instead of 0, 1, 64 and 65. The transaction is finished before the consumer loop starts, so no builder is open at Applied, consumer, hash, callback or teardown boundaries. The batched form is eligible only when the bound backing carries no I/O observer and the context has no row or lookup observer. Production installs no observer (the one source-side registration clears it for derived identities and restores it); observers exist in measurement and test harnesses, which therefore see exactly the charge sequence they saw before. ScratchBacking and CommandScratchStore gain a const has_io_observer query that reads the already-present observer member, and the observer's comment now states its contract: a passive accounting sink that must not call back into the engine, a store or a consumer. All other producers keep the per-row path. No state is added: the mode is a template parameter and the store's existing fixed transaction workspace does the buffering. Tests: a construction-publication row counts work (puts, fresh handles, publications) for 0, 1, 64 and 65 rows with no observer, and an observed row requires N publications and the same charge-by-charge sequence as the per-row reference. A test comment now states that 64 is a batch constant, not a page capacity (an ordered leaf at 512-byte pages holds 58 keys). Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 0190528 commit 783016f

8 files changed

Lines changed: 463 additions & 34 deletions

File tree

‎ci/cr150-source-sites.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8198,7 +8198,7 @@
81988198
"scope": [
81998199
"namespace pineforge :: source #1",
82008200
"namespace detail #1",
8201-
"template < class T > class ExitScratchRows #1"
8201+
"template < class T , ExitScratchRowMode Mode = ExitScratchRowMode :: per_row > class ExitScratchRows #1"
82028202
],
82038203
"cls": "temp",
82048204
"spelling": "const_iterator",
@@ -8210,7 +8210,7 @@
82108210
"scope": [
82118211
"namespace pineforge :: source #1",
82128212
"namespace detail #1",
8213-
"template < class T > class ExitScratchRows #1"
8213+
"template < class T , ExitScratchRowMode Mode = ExitScratchRowMode :: per_row > class ExitScratchRows #1"
82148214
],
82158215
"cls": "decl",
82168216
"spelling": "const_iterator",
@@ -8222,7 +8222,7 @@
82228222
"scope": [
82238223
"namespace pineforge :: source #1",
82248224
"namespace detail #1",
8225-
"template < class T > class ExitScratchRows #1"
8225+
"template < class T , ExitScratchRowMode Mode = ExitScratchRowMode :: per_row > class ExitScratchRows #1"
82268226
],
82278227
"cls": "decl",
82288228
"spelling": "const_iterator",

‎docs/design/native-feature-parity.md‎

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

‎include/pineforge/detail/scratch_backing.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ struct ScratchIoCharge {
6969
ScratchStatus status;
7070
};
7171

72+
// A passive accounting sink. Its callback must not call back into the engine,
73+
// a store or a consumer.
7274
struct ScratchIoObserver {
7375
void* context = nullptr;
7476
void (*charge)(void*, const ScratchIoCharge&) noexcept = nullptr;
@@ -95,6 +97,8 @@ class ScratchBacking {
9597

9698
ScratchBackingKind kind() const noexcept { return kind_; }
9799
std::size_t page_size() const noexcept { return page_size_; }
100+
// True when this backing carries a registered passive accounting sink.
101+
bool has_io_observer() const noexcept { return observer_.charge != nullptr; }
98102
virtual std::uint64_t page_count() const noexcept = 0;
99103
// Construction-time arena region bound, including a zero-byte share.
100104
// No value for disk/test backings; this policy is not logical store state.

‎include/pineforge/detail/scratch_store.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class CommandScratchStore final : public ScratchReadback {
103103
// True from a successful begin until a successful commit or rollback,
104104
// including while the transaction is rollback-only.
105105
bool transaction_active() const noexcept;
106+
// True when the borrowed backing has a passive logical-I/O observer.
107+
bool has_io_observer() const noexcept;
106108

107109
// begin snapshots logical state including handle allocation. Nested begin
108110
// is programming_error. Reads thereafter include successful pending edits.

‎include/pineforge/detail/scratch_store_impl.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,10 @@ inline std::uint64_t CommandScratchStore::record_count() const noexcept {
18091809
inline bool CommandScratchStore::transaction_active() const noexcept {
18101810
return state_ != nullptr && state_->open_status_ == ScratchStatus::ok && state_->active_;
18111811
}
1812+
inline bool CommandScratchStore::has_io_observer() const noexcept {
1813+
return state_ != nullptr && state_->backing_ != nullptr
1814+
&& state_->backing_->has_io_observer();
1815+
}
18121816
inline ScratchStatus CommandScratchStore::begin() noexcept {
18131817
return state_ == nullptr ? ScratchStatus::programming_error : state_->begin();
18141818
}

‎include/pineforge/source/exit_scratch.hpp‎

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,25 @@ inline ScratchHandle exit_scratch_seek(ExitScratchContext& context,
353353
exit_scratch_check(status, context.store);
354354
return handle;
355355
}
356+
inline void exit_scratch_edit_failure(ExitScratchContext& context,
357+
ScratchStatus status) {
358+
// Rollback makes further backing calls. Preserve the first failure before
359+
// cleanup can replace the backing's borrowed diagnostic.
360+
std::optional<pineforge::detail::ReportScratchIoError> failure;
361+
if (status == ScratchStatus::io_error)
362+
if (const auto* carried = context.store.carried_failure()) failure = *carried;
363+
const auto rollback_status = context.store.rollback();
364+
pineforge::detail::rethrow_carried_scratch_failure(failure ? &*failure : nullptr);
365+
exit_scratch_check(rollback_status, context.store);
366+
exit_scratch_failure(status);
367+
}
356368
template<class Edit> void exit_scratch_edit(ExitScratchContext& context, Edit operation) {
357369
exit_scratch_check_teardown(context);
358370
exit_scratch_check(context.store.begin(), context.store);
359371
auto status = operation();
360372
if (status == ScratchStatus::ok)
361373
status = context.store.commit(ScratchDurability::buffered);
362-
if (status != ScratchStatus::ok) {
363-
// Rollback makes further backing calls. Preserve the first failure
364-
// before cleanup can replace the backing's borrowed diagnostic.
365-
std::optional<pineforge::detail::ReportScratchIoError> failure;
366-
if (status == ScratchStatus::io_error)
367-
if (const auto* carried = context.store.carried_failure()) failure = *carried;
368-
const auto rollback_status = context.store.rollback();
369-
pineforge::detail::rethrow_carried_scratch_failure(failure ? &*failure : nullptr);
370-
exit_scratch_check(rollback_status, context.store);
371-
exit_scratch_failure(status);
372-
}
374+
if (status != ScratchStatus::ok) exit_scratch_edit_failure(context, status);
373375
}
374376

375377
// Teardown uses only status-returning store operations. Snapshot the original
@@ -581,7 +583,13 @@ template<> struct ExitScratchValue<native_order::RequestHandle> {
581583
// No store visitor is active while a caller cancels, replaces, executes or
582584
// creates another scope. Each edit commits separately, so nested Applied
583585
// callbacks require neither nested transactions nor owning snapshots.
584-
template<class T> class ExitScratchRows {
586+
enum class ExitScratchRowMode : std::uint8_t {
587+
per_row,
588+
bounded_batch,
589+
};
590+
591+
template<class T, ExitScratchRowMode Mode = ExitScratchRowMode::per_row>
592+
class ExitScratchRows {
585593
public:
586594
explicit ExitScratchRows(const PineExecutionAdapter& owner)
587595
: ExitScratchRows(exit_scratch(owner)) {}
@@ -600,19 +608,47 @@ template<class T> class ExitScratchRows {
600608
if (value.run != context().run)
601609
exit_scratch_failure(ScratchStatus::programming_error);
602610
}
603-
put(count_, value);
604-
++count_;
611+
if constexpr (Mode == ExitScratchRowMode::bounded_batch) {
612+
if (batch_enabled()) {
613+
if (count_ % kExitScopeEraseBatch == 0
614+
|| !context().store.transaction_active())
615+
begin_batch();
616+
put_batch(count_, value);
617+
++count_;
618+
if (count_ % kExitScopeEraseBatch == 0) finish();
619+
} else {
620+
finish();
621+
put(count_, value);
622+
++count_;
623+
}
624+
} else {
625+
put(count_, value);
626+
++count_;
627+
}
605628
observe();
606629
}
630+
void finish() {
631+
if constexpr (Mode == ExitScratchRowMode::bounded_batch) {
632+
if (count_ == 0
633+
|| !context().store.transaction_active())
634+
return;
635+
exit_scratch_check_teardown(context());
636+
const auto status = context().store.commit(ScratchDurability::buffered);
637+
if (status != ScratchStatus::ok)
638+
exit_scratch_edit_failure(context(), status);
639+
}
640+
}
607641
void set(std::size_t index, const T& value) {
608642
static_assert(!std::is_same_v<T, std::string>, "text selections are immutable");
643+
finish();
609644
put(index, value);
610645
}
611646
auto operator[](std::size_t index) const {
612647
return ExitScratchValue<T>::read(context(), find(index));
613648
}
614649
auto front() const { return (*this)[0]; }
615650
void clear() {
651+
finish();
616652
scope_.clear();
617653
count_ = 0;
618654
observe();
@@ -679,6 +715,37 @@ template<class T> class ExitScratchRows {
679715
const_iterator end() const { return {this, size()}; }
680716

681717
private:
718+
bool batch_enabled() const noexcept {
719+
if constexpr (Mode != ExitScratchRowMode::bounded_batch) {
720+
return false;
721+
} else {
722+
return !context().store.has_io_observer()
723+
&& context().observe_rows == nullptr
724+
&& context().observe_lookup == nullptr;
725+
}
726+
}
727+
void begin_batch() {
728+
exit_scratch_check_teardown(context());
729+
exit_scratch_check(context().store.begin(), context().store);
730+
}
731+
ScratchStatus put_value(std::uint64_t index, const T& value) {
732+
const ExitScratchKey key(scope(), 0, 0, index);
733+
const auto payload = ExitScratchValue<T>::source(value);
734+
ScratchHandle handle = 0;
735+
auto status = context().store.put(key, payload, handle);
736+
if constexpr (std::is_same_v<T, std::string>) {
737+
if (status != ScratchStatus::ok) return status;
738+
const ExitScratchKey ordered(scope(), 1, 0, index, &payload);
739+
const ScratchSpanSource empty({nullptr, 0});
740+
status = context().store.put(ordered, empty, handle);
741+
}
742+
return status;
743+
}
744+
void put_batch(std::uint64_t index, const T& value) {
745+
const auto status = put_value(index, value);
746+
if (status != ScratchStatus::ok)
747+
exit_scratch_edit_failure(context(), status);
748+
}
682749
void observe() const noexcept {
683750
if (context().observe_rows)
684751
context().observe_rows(context().observation, scope(), size());
@@ -692,23 +759,16 @@ template<class T> class ExitScratchRows {
692759
return handle;
693760
}
694761
void put(std::uint64_t index, const T& value) {
695-
const ExitScratchKey key(scope(), 0, 0, index);
696-
const auto payload = ExitScratchValue<T>::source(value);
697762
exit_scratch_edit(context(), [&] {
698-
ScratchHandle handle = 0;
699-
auto status = context().store.put(key, payload, handle);
700-
if constexpr (std::is_same_v<T, std::string>) {
701-
if (status != ScratchStatus::ok) return status;
702-
const ExitScratchKey ordered(scope(), 1, 0, index, &payload);
703-
const ScratchSpanSource empty({nullptr, 0});
704-
status = context().store.put(ordered, empty, handle);
705-
}
706-
return status;
763+
return put_value(index, value);
707764
});
708765
}
709766
ExitScratchScope scope_;
710767
std::uint64_t count_ = 0;
711768
};
712769

770+
template<class T>
771+
using ExitScratchBatchRows = ExitScratchRows<T, ExitScratchRowMode::bounded_batch>;
772+
713773
} // namespace detail
714774
} // namespace pineforge::source

‎src/source/pine_adapter.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25851,8 +25851,9 @@ void PineExecutionAdapter::on_applied(const native_order::ExecutionAppliedEvent&
2585125851
// The generic cohort is already the quantity authority. Rebind only
2585225852
// adapter lifecycle/reservation receipts after the opening becomes a
2585325853
// live physical fact; no request is resized or resubmitted here.
25854-
detail::ExitScratchRows<native_order::RequestHandle> handles(*this);
25854+
detail::ExitScratchBatchRows<native_order::RequestHandle> handles(*this);
2585525855
for (const auto& handle : live_handles_) handles.push_back(handle);
25856+
handles.finish();
2585625857
for (const auto& handle : handles) {
2585725858
const auto pending = placement_.find(handle.incarnation);
2585825859
if (pending == placement_.end()) continue;
@@ -26366,8 +26367,9 @@ void PineExecutionAdapter::on_applied(const native_order::ExecutionAppliedEvent&
2636626367
::pineforge::detail::clear_exposure_storage(cohort.second.live_units_by_origin);
2636726368
cohorts_by_id_.refresh_held(current);
2636826369
}
26369-
detail::ExitScratchRows<native_order::RequestHandle> handles(*this);
26370+
detail::ExitScratchBatchRows<native_order::RequestHandle> handles(*this);
2637026371
for (const auto& handle : live_handles_) handles.push_back(handle);
26372+
handles.finish();
2637126373
for (const auto& handle : handles) {
2637226374
const auto found = placement_.find(handle.incarnation);
2637326375
if (found == placement_.end()) continue;

0 commit comments

Comments
 (0)