Skip to content

Commit 5afe162

Browse files
tvaron3Copilot
andcommitted
perf(cosmos): strip unused fields from partition key range cache
Strip 9 unused fields from partition key range dicts after fetching from the service. CollectionRoutingMap only uses 4 fields (id, minInclusive, maxExclusive, parents) but the service returns 13. Reduces per-range dict memory by ~40%, measured with tracemalloc on 150 CosmosClient instances with PPCB enabled: Test setup: - Account: ~100 physical partitions, 2 regions, multi-write - Tool: tracemalloc (retained memory) - Operations: 1 read_item + 1 upsert_item per client Results (current memory, MB): | Clients | 4.15.0 PPCB=True | Patched PPCB=True | PPCB=false | Savings | |---------|-----------------|------------------|-----------|---------| | 1 | 14.3 | 14.3 | 14.0 | -0.0 | | 25 | 23.0 | 20.5 | 17.9 | -2.5 | | 50 | 31.9 | 27.4 | 21.7 | -4.5 | | 100 | 44.9 | 39.9 | 29.4 | -4.9 | | 150 | 63.8 | 52.9 | 36.4 | -10.9 | PPCB overhead reduction: | Clients | Before | After | Reduction | |---------|--------|--------|-----------| | 25 | 5.1 MB | 2.6 MB | -48% | | 50 |10.3 MB | 5.7 MB | -44% | | 100 |15.4 MB |10.5 MB | -31% | | 150 |27.4 MB |16.5 MB | -39% | Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent de23b45 commit 5afe162

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

sdk/cosmos/azure-cosmos/azure/cosmos/_routing/aio/routing_map_provider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ async def init_collection_routing_map_if_needed(
9797
# causing the partitionKeyRanges to have both the children ranges and their parents. Therefore, we need
9898
# to discard the parent ranges to have a valid routing map.
9999
collection_pk_ranges = list(PartitionKeyRangeCache._discard_parent_ranges(collection_pk_ranges))
100+
# Strip fields not used by CollectionRoutingMap to reduce memory footprint
101+
_KEEP = frozenset({"id", "minInclusive", "maxExclusive", "parents"})
102+
collection_pk_ranges = [{k: v for k, v in r.items() if k in _KEEP} for r in collection_pk_ranges]
103+
# Strip fields not used by CollectionRoutingMap to reduce memory footprint
104+
_KEEP = frozenset({"id", "minInclusive", "maxExclusive", "parents"})
105+
collection_pk_ranges = [{k: v for k, v in r.items() if k in _KEEP} for r in collection_pk_ranges]
100106
collection_routing_map = CollectionRoutingMap.CompleteRoutingMap(
101107
[(r, True) for r in collection_pk_ranges], collection_id
102108
)

sdk/cosmos/azure-cosmos/azure/cosmos/_routing/routing_map_provider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ def init_collection_routing_map_if_needed(
8282
# causing the partitionKeyRanges to have both the children ranges and their parents. Therefore, we need
8383
# to discard the parent ranges to have a valid routing map.
8484
collection_pk_ranges = list(PartitionKeyRangeCache._discard_parent_ranges(collection_pk_ranges))
85+
# Strip fields not used by CollectionRoutingMap to reduce memory footprint
86+
_KEEP = frozenset({"id", "minInclusive", "maxExclusive", "parents"})
87+
collection_pk_ranges = [{k: v for k, v in r.items() if k in _KEEP} for r in collection_pk_ranges]
88+
# Strip fields not used by CollectionRoutingMap to reduce memory footprint
89+
_KEEP = frozenset({"id", "minInclusive", "maxExclusive", "parents"})
90+
collection_pk_ranges = [{k: v for k, v in r.items() if k in _KEEP} for r in collection_pk_ranges]
8591
collection_routing_map = CollectionRoutingMap.CompleteRoutingMap(
8692
[(r, True) for r in collection_pk_ranges], collection_id
8793
)

0 commit comments

Comments
 (0)