Skip to content

perf(bt_json): optimize _to_bt_safe and bt_safe_deep_copy hot paths#139

Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit intomainfrom
abhi-perf-optimize-bt-json-hot-paths
Mar 25, 2026
Merged

perf(bt_json): optimize _to_bt_safe and bt_safe_deep_copy hot paths#139
Abhijeet Prasad (AbhiPrasad) merged 1 commit intomainfrom
abhi-perf-optimize-bt-json-hot-paths

Conversation

@AbhiPrasad
Copy link
Member

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Mar 25, 2026

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):

_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

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
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) force-pushed the abhi-perf-optimize-bt-json-hot-paths branch from 858df3c to 7945ab6 Compare March 25, 2026 00:25
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) marked this pull request as ready for review March 25, 2026 00:25
Copy link
Contributor

@viadezo1er ViaDézo1er / cedric (viadezo1er) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on a small Linux machine, the speedup ratio is the same

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) merged commit 01d0cea into main Mar 25, 2026
34 checks passed
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) deleted the abhi-perf-optimize-bt-json-hot-paths branch March 25, 2026 17:28
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.

2 participants