Files
synapse/rust
Erik JohnstonandClaude Fable 5 913c00a07f Trim Python round-trips and refcount churn from the switch path
Four costs the switch/accounting hot path paid per operation:

- get_thread_id round-tripped into Python (threading.get_ident) up to
  twice per context switch. Cache the value in a Rust thread-local — it is
  constant for the thread's lifetime, and a fresh thread starts with an
  empty slot so recycled thread ids cannot go stale.

- sentinel() returned a clone_ref per call, so the identity checks on the
  switch path (every switch to or from the sentinel) paid an INCREF/DECREF
  pair per pointer comparison. Return the &'static reference and clone_ref
  only where a reference is handed back to Python.

- forward_to_parent always dispatched through a Python method call even
  when the parent is a plain base LoggingContext (the common case — every
  Measure-created nested context). Take the same native-vs-subclass split
  switch_context already uses.

- __init__ allocated a second ContextResourceUsage and discarded the one
  __new__ had just built; reset the existing object in place instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JFbRtswu7rsHrttJFauUUb
2026-07-16 15:27:21 +00:00
..