perf(bt_json): optimize _to_bt_safe and bt_safe_deep_copy hot paths#139
Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit intomainfrom Mar 25, 2026
Merged
Conversation
Add primitive fast-paths using type identity checks before expensive isinstance calls against abstract classes and Pydantic model_dump. Rewrite _deep_copy_object to use type(v) is dict/list instead of isinstance(v, Mapping), inline primitive checks, and cache visited set methods. Guard Pydantic model_dump/dict with hasattr to avoid warnings overhead on non-Pydantic values. Benchmark results (geometric mean: 8.49x faster): _to_bt_safe: primitive-int 2.02 us -> 42.2 ns 47.8x faster primitive-float-nan 2.01 us -> 67.8 ns 29.7x faster str-subclass-enum 2.02 us -> 97.5 ns 20.8x faster dataclass 10.2 us -> 3.32 us 3.1x faster pydantic-v2-like 1.68 us -> 1.78 us 1.1x slower (noise) pydantic-v1-like 1.94 us -> 841 ns 2.3x faster bt_safe_deep_copy: small 19.7 us -> 2.30 us 8.6x faster medium 179 us -> 17.3 us 10.4x faster large 1.66 ms -> 138 us 12.1x faster circular 178 us -> 17.9 us 9.9x faster non-string-keys 16.7 us -> 2.12 us 7.9x faster
858df3c to
7945ab6
Compare
ViaDézo1er / cedric (viadezo1er)
approved these changes
Mar 25, 2026
3 tasks
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.
Extracting just the json serialization changes from #101. Validated with benchmarking suite.
Add primitive fast-paths using type identity checks before expensive
isinstance calls against abstract classes and Pydantic model_dump.
Rewrite _deep_copy_object to use type(v) is dict/list instead of
isinstance(v, Mapping), inline primitive checks, and cache
visited set methods. Guard Pydantic model_dump/dict with hasattr
to avoid warnings overhead on non-Pydantic values.
Benchmark results (geometric mean: 8.49x faster):