mirror of
https://github.com/element-hq/synapse.git
synced 2026-08-17 00:11:08 +00:00
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