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 #455wkt/start_or_cancel_tests_474
parent
148a098ca6
commit
0e11ff7e9d
|
|
@ -325,10 +325,12 @@ class Channel:
|
||||||
'''
|
'''
|
||||||
__tracebackhide__: bool = hide_tb
|
__tracebackhide__: bool = hide_tb
|
||||||
try:
|
try:
|
||||||
log.transport(
|
if log.at_least_level('transport'):
|
||||||
'=> send IPC msg\n\n'
|
# don't materialize the payload repr if not necessary
|
||||||
# f'{pformat(payload)}\n'
|
log.transport(
|
||||||
)
|
'=> send IPC msg:\n\n'
|
||||||
|
f'{pformat(payload)}\n'
|
||||||
|
)
|
||||||
# assert self._transport # but why typing?
|
# assert self._transport # but why typing?
|
||||||
await self._transport.send(
|
await self._transport.send(
|
||||||
payload,
|
payload,
|
||||||
|
|
|
||||||
|
|
@ -306,15 +306,15 @@ class PldRx(Struct):
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
pld: PayloadT = self._pld_dec.decode(pld)
|
pld: PayloadT = self._pld_dec.decode(pld)
|
||||||
log.runtime(
|
if log.at_least_level('runtime'):
|
||||||
f'Decoded payload for\n'
|
# don't materialize the payload repr if not necessary
|
||||||
# # f'\n'
|
log.runtime(
|
||||||
# f'{msg}\n'
|
f'Decoded payload for\n'
|
||||||
# # ^TODO?, ideally just render with `,
|
f'\n'
|
||||||
# # pld={decode}` in the `msg.pformat()`??
|
f'{msg}\n'
|
||||||
# f'where, '
|
f'where, '
|
||||||
# f'{type(msg).__name__}.pld={pld!r}\n'
|
f'{type(msg).__name__}.pld={pld!r}\n'
|
||||||
)
|
)
|
||||||
return pld
|
return pld
|
||||||
except TypeError as typerr:
|
except TypeError as typerr:
|
||||||
__tracebackhide__: bool = False
|
__tracebackhide__: bool = False
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue