Skip to content

Publish each admitted slot's clock instead of sharing its filter - #231

Merged
kahrendt merged 2 commits into
alloc-6-per-connectionfrom
alloc-7-clock-slots
Nov 3, 2026
Merged

kahrendt merged 2 commits into
alloc-6-per-connectionfrom
alloc-7-clock-slots

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Layer 7 of 7 (base: alloc-6-per-connection). Accept to admission: 4 -> 3 allocations under libc++, 6 -> 5 under libstdc++.

Summary

Each connection's time filter was a shared_ptr with its own mutex, published so role threads could convert timestamps without holding the connection. The filter is now a plain member used on the protocol task only, and other threads read a copy of its conversion. This is the one layer that changes a threading arrangement, so it sits on top.

Changes

  • TimeTransform (time_filter.h): the filter's offset, the drift it applies and its last update, with both conversions. The filter's own conversions run on it, unchanged bit for bit, and SendspinTimeFilter loses state_mutex_.
  • refresh_published_state() (tick step 11) publishes one TimeTransform per admitted slot, plus the primary's slot, and takes published_mutex_ only when a measurement, a slot's connection or the primary changed one. ConnectionManager::clock() replaces time_filter() and returns the primary's.
  • The public is_time_synced() and get_client_time() read clock(), so they report the filter as of the end of the protocol tick that last changed it, not the live filter. The client/state clock gate and the source role read the live filter on the protocol task.
  • No other thread shares or frees a filter any more. MAX_ADMITTED is 1, so there is one slot today; publishing per slot leaves room for a second admitted connection, such as an announcement-only server, to have its own clock.
  • docs/internals.md, conventions.md, playback-sync.md and CLAUDE.md describe the published clocks; the lock list loses the filter's mutex.

Testing

  • The time filter slot test also checks that a measurement taken after admission reaches the slot at the next publish; a refresh that republished only on a primary change fails it.

The whole stack ran on an ESP32 with no issues.

A connection's SendspinTimeFilter becomes a by-value member used on the
protocol task only, so it loses its mutex and its shared_ptr. Role threads
read a TimeTransform instead: the filter's offset and drift as of its
latest measurement, which refresh_published_state() copies into one slot
per admitted connection whenever a measurement, a slot's connection or the
primary moved it. ConnectionManager::clock() returns the primary's, which
is_time_synced() and get_client_time() convert through as before. A
reader shares nothing with the connection, and a second admitted
connection gets its own clock slot.

Accept to admission: 4 -> 3 allocations under libc++, 6 -> 5 under
libstdc++.
@kahrendt
kahrendt force-pushed the alloc-7-clock-slots branch from 0b4f933 to 84b228a Compare November 3, 2026 15:15
@kahrendt
kahrendt marked this pull request as ready for review November 3, 2026 15:36
@kahrendt
kahrendt requested a balanced review from Copilot November 3, 2026 15:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The lifecycle test covers only the newcomer’s first measurement, leaving subsequent clock corrections and snapshot visibility before and after publication untested.

1 open issue
What changed in this PR

This PR replaces shared, mutex-protected time filters with connection-owned filters and published clock snapshots, removing one allocation per admitted connection.

Changes:

  • Keeps live filters on the protocol task and publishes per-slot clock transforms for other threads.
  • Updates clock consumers, lifecycle coverage, and allocation expectations.
  • Revises architecture and synchronization documentation.
File Description
tests/​test_source_role.cpp Uses connection-owned filters in source tests.
tests/​test_noise_transport.cpp Removes explicit filter initialization.
tests/​test_client_lifecycle.cpp Checks publication of a post-admission measurement.
tests/​test_allocations.cpp Lowers expected admission allocations by one.
src/​time_filter.h Defines TimeTransform and removes the filter mutex.
src/​time_filter.cpp Moves conversions into the snapshot type.
src/​time_burst.h Updates the filter-access documentation example.
src/​time_burst.cpp Updates connection-owned filters directly.
src/​source_role.cpp Timestamps outgoing chunks through the live filter.
src/​connection.h Stores the filter directly in each connection.
src/​connection.cpp Removes heap-based filter initialization.
src/​connection_manager.h Declares published clock slots and the snapshot getter.
src/​connection_manager.cpp Publishes changed transforms and primary-slot state.
src/​client.cpp Reads published snapshots for public clock queries.
docs/​playback-sync.md Explains snapshot-based timestamp conversion.
docs/​internals.md Updates publication, threading, and lock documentation.
docs/​conventions.md Revises shared-state and lock conventions.
CLAUDE.md Updates the connection manager’s architecture description.

Review effort: Balanced

Comment thread src/connection_manager.cpp Resolved
The lifecycle test covered only the newcomer's first measurement, which
flips synced, so a publish that fired only on that flip or on a primary
change would have frozen readers at the first burst's clock unnoticed.
A second measurement now has to move get_client_time() to its offset.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the connection-owned filter and published clock snapshot changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@kahrendt
kahrendt merged commit f23d2f5 into main Nov 3, 2026
17 checks passed
@kahrendt
kahrendt deleted the alloc-7-clock-slots branch November 3, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants