Avoid kwargs unpacking in _process_event hot path#821
Open
Charisn wants to merge 1 commit into
Open
Conversation
`BoundLoggerBase._process_event` runs on every non-filtered log call and
is the hottest function in structlog. It assembled the event dict with:
event_dict = self._context.copy()
event_dict.update(**event_kw)
`event_kw` is always a plain `dict` -- it originates from `**event_kw` in
`_proxy_to_logger` -- so unpacking it back into keyword arguments only for
`dict.update` to repack them is pure overhead. The `update(**event_kw)`
call path materializes a fresh keyword dict and revalidates the keys on
every single log call, even when `event_kw` is empty.
Passing the mapping positionally -- `event_dict.update(event_kw)` -- is
semantically identical for a dict of string keys (both the resulting
value and its type are unchanged) but skips that machinery entirely.
Measured on CPython 3.14, averaged over several A/B rounds on the
`copy()` + `update()` core of `_process_event`:
kwargs before after speedup
0 117 ns 89 ns 1.31x
1 134 ns 91 ns 1.48x
3 199 ns 150 ns 1.33x
8 258 ns 180 ns 1.44x
End-to-end through `_proxy_to_logger` (3-key bound context, 3 kwargs):
~919 ns -> ~844 ns per call, roughly 8% faster. The improvement is
present even with zero kwargs, because `update(**{})` still pays for the
empty-kwargs path.
This is behavior-preserving; the existing test suite passes unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BoundLoggerBase._process_eventruns on every non-filtered log call and is the hottest function in structlog. It assembles the event dict like this:event_kwis always a plaindict— it originates from**event_kwin_proxy_to_logger— soupdate(**event_kw)unpacks it back into keyword arguments only fordict.updateto repack them into a dict again. That**call path materializes a fresh keyword dict and revalidates the keys on every single log call, even whenevent_kwis empty.Passing the mapping positionally —
event_dict.update(event_kw)— is semantically identical for a dict of string keys (both the resulting value and its type are unchanged, so customcontext_classes are unaffected) and skips that machinery entirely.Why it's safe
event_kwis guaranteed to be adict, so there's no behavioral difference between the positional and keyword forms._process_eventis exercised throughouttests/test_base.pyand others): 276 passed, 1 skipped locally.Numbers
Measured on CPython 3.14, averaged over several A/B rounds isolating the
copy()+update()core of_process_event:End-to-end through
_proxy_to_logger(3-key bound context, 3 kwargs): ~919 ns → ~844 ns per call, roughly 8% faster. The win is present even with zero kwargs, becauseupdate(**{})still pays for the empty-kwargs path.Pull Request Check List
mainbranch._process_eventalready cover the modified line, and they pass unchanged.api.py.versionchangeddirective applies..rstand.mdfiles is written using semantic newlines.