fix(serializer): avoid copying the whole Mapping before trimming to MAX_DATABAG_BREADTH#6783
Draft
Malkiz223 wants to merge 1 commit into
Draft
fix(serializer): avoid copying the whole Mapping before trimming to MAX_DATABAG_BREADTH#6783Malkiz223 wants to merge 1 commit into
Malkiz223 wants to merge 1 commit into
Conversation
…AX_DATABAG_BREADTH
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.
Description
EventSerializer._serialize_node_impl'sMappingbranch copied the object in full (dict(obj.items())) before trimming it down toMAX_DATABAG_BREADTH(10 by default) items. That copy is O(len(obj)), even though at mostMAX_DATABAG_BREADTHpairs ever get serialized.This mostly doesn't matter for normal
extra/tagspayloads, but it's a real problem for local-variable capture (include_local_variables, on by default): frame locals are whatever a function happens to hold, and it's common forselfin an instance method to reference a large in-memory structure (cache, lookup table, etc). We have big caches like this in production, and capturing one as a frame local cost hundreds of milliseconds to close to a second - with a burst of such exceptions, our whole process stalled for around 30 seconds.The fix bounds the copy to
remaining_breadth + 1pairs viaitertools.islice, falling back to a full copy only when breadth is unbounded (None/float("inf"), sinceislicedoesn't accept a float stop value). Behavior and output are unchanged - only the cost of the copy changes.Issues
serialize()copies large Mapping locals in full before trimming to MAX_DATABAG_BREADTH #6773