Skip to content

Guard hot-path log calls to avoid payload rendering when disabled#458

Open
mahmoudhas wants to merge 2 commits into
goodboy:mainfrom
mahmoudhas:fix/guard-hot-path-log-rendering
Open

Guard hot-path log calls to avoid payload rendering when disabled#458
mahmoudhas wants to merge 2 commits into
goodboy:mainfrom
mahmoudhas:fix/guard-hot-path-log-rendering

Conversation

@mahmoudhas
Copy link
Copy Markdown
Contributor

@mahmoudhas mahmoudhas commented May 8, 2026

Summary

  • Wraps log.transport() in Channel.send() and log.runtime() in PldRx.decode_pld() with log.at_least_level() guards so that expensive pformat(payload) / repr(msg) / repr(pld) rendering is skipped entirely when the respective log level is not active.
  • Re-enables the full log content (payload details) that was previously commented out as a workaround, now that the guard prevents it from being evaluated on the hot path.

Fixes #455 — on heavy-payload IPC workload, py-spy showed pformat / decode_pld consuming a significant amount of CPU time.

This follows the existing pattern already used elsewhere in the codebase (e.g. _chan.py:204 Channel.connect_from, _context.py:949 cancel-scope formatting).

Test plan

  • Verify py-spy top shows no time spent in pformat / _safe_repr when running with default log level (ERROR).

mahmoudhas and others added 2 commits May 6, 2026 05:49
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 goodboy#455.

This also restores the full diagnostic output (msg type, decoded
payload) that was temporarily commented out in 0373164 as a stopgap.

Resolves goodboy#455
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

performance bottleneck due to logging

1 participant