From 0373164f459059b15d2ed2a04163d91cff42cbd4 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Tue, 5 May 2026 18:37:19 +0000 Subject: [PATCH 1/2] avoid format on the hot send path --- tractor/ipc/_chan.py | 4 ++-- tractor/msg/_ops.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tractor/ipc/_chan.py b/tractor/ipc/_chan.py index 10a800e47..0bf254d72 100644 --- a/tractor/ipc/_chan.py +++ b/tractor/ipc/_chan.py @@ -326,8 +326,8 @@ async def send( __tracebackhide__: bool = hide_tb try: log.transport( - '=> send IPC msg:\n\n' - f'{pformat(payload)}\n' + '=> send IPC msg\n\n' + # f'{pformat(payload)}\n' ) # assert self._transport # but why typing? await self._transport.send( diff --git a/tractor/msg/_ops.py b/tractor/msg/_ops.py index 3b4eaa842..851da8a71 100644 --- a/tractor/msg/_ops.py +++ b/tractor/msg/_ops.py @@ -308,12 +308,12 @@ def decode_pld( pld: PayloadT = self._pld_dec.decode(pld) log.runtime( f'Decoded payload for\n' - # f'\n' - f'{msg}\n' - # ^TODO?, ideally just render with `, - # pld={decode}` in the `msg.pformat()`?? - f'where, ' - f'{type(msg).__name__}.pld={pld!r}\n' + # # f'\n' + # f'{msg}\n' + # # ^TODO?, ideally just render with `, + # # pld={decode}` in the `msg.pformat()`?? + # f'where, ' + # f'{type(msg).__name__}.pld={pld!r}\n' ) return pld except TypeError as typerr: From 9961b1890dc6382c7770d01bac57dec59be9cfe4 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 7 May 2026 22:01:55 -0700 Subject: [PATCH 2/2] Guard hot-path log calls to avoid payload rendering when disabled Wrap `log.transport()` in `Channel.send()` and `log.runtime()` in `PldRx.decode_pld()` with `log.at_least_level()` checks so that expensive `pformat(payload)` / `repr(msg)` / `repr(pld)` calls are skipped entirely when the respective log level is not active. Previously the f-string arguments were eagerly evaluated before being passed to the log method, even though `StackLevelAdapter.log()` would then discard the message internally via its own `isEnabledFor()` check. On high-frequency IPC paths this caused `pformat` to dominate CPU usage (~60-70 %) as reported in #455. This also restores the full diagnostic output (msg type, decoded payload) that was temporarily commented out in 0373164 as a stopgap. Resolves #455 --- tractor/ipc/_chan.py | 10 ++++++---- tractor/msg/_ops.py | 18 +++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/tractor/ipc/_chan.py b/tractor/ipc/_chan.py index 0bf254d72..0bae7e262 100644 --- a/tractor/ipc/_chan.py +++ b/tractor/ipc/_chan.py @@ -325,10 +325,12 @@ async def send( ''' __tracebackhide__: bool = hide_tb try: - log.transport( - '=> send IPC msg\n\n' - # f'{pformat(payload)}\n' - ) + if log.at_least_level('transport'): + # don't materialize the payload repr if not necessary + log.transport( + '=> send IPC msg:\n\n' + f'{pformat(payload)}\n' + ) # assert self._transport # but why typing? await self._transport.send( payload, diff --git a/tractor/msg/_ops.py b/tractor/msg/_ops.py index 851da8a71..929d97057 100644 --- a/tractor/msg/_ops.py +++ b/tractor/msg/_ops.py @@ -306,15 +306,15 @@ def decode_pld( ): try: pld: PayloadT = self._pld_dec.decode(pld) - log.runtime( - f'Decoded payload for\n' - # # f'\n' - # f'{msg}\n' - # # ^TODO?, ideally just render with `, - # # pld={decode}` in the `msg.pformat()`?? - # f'where, ' - # f'{type(msg).__name__}.pld={pld!r}\n' - ) + if log.at_least_level('runtime'): + # don't materialize the payload repr if not necessary + log.runtime( + f'Decoded payload for\n' + f'\n' + f'{msg}\n' + f'where, ' + f'{type(msg).__name__}.pld={pld!r}\n' + ) return pld except TypeError as typerr: __tracebackhide__: bool = False