Commit fc47cf0
perf(cosmos): optimize partition key range cache memory usage
Three optimizations to reduce CollectionRoutingMap memory footprint:
1. Strip unused fields: Convert 13-field service response dicts to compact
PKRange namedtuples retaining only id, minInclusive, maxExclusive, parents.
PKRange supports dict-style access for backward compatibility.
2. Add __slots__ to Range class: Eliminates per-instance __dict__ overhead
(~100 bytes per Range object).
3. Skip redundant .upper() calls: Service returns uppercase hex strings.
Avoid creating unnecessary string copies when already uppercase.
Measured with tracemalloc, 150 CosmosClient instances, PPCB enabled:
| Clients | Original | Strip Only | All 3 Patches | PPCB=false |
|---------|----------|------------|---------------|------------|
| 25 | 23.0 MB | 20.5 MB | 20.0 MB | 17.9 MB |
| 50 | 31.9 MB | 27.4 MB | 25.8 MB | 21.7 MB |
| 100 | 44.9 MB | 39.9 MB | 36.6 MB | 29.4 MB |
| 150 | 63.8 MB | 52.9 MB | 43.3 MB | 36.4 MB |
PPCB overhead reduction:
| Clients | Original | All 3 | Reduction |
|---------|----------|--------|-----------|
| 25 | 5.1 MB | 2.1 MB | -58% |
| 50 | 10.3 MB | 4.1 MB | -60% |
| 100 | 15.4 MB | 7.2 MB | -53% |
| 150 | 27.4 MB | 6.9 MB | -74% |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent de23b45 commit fc47cf0
3 files changed
Lines changed: 56 additions & 3 deletions
File tree
- sdk/cosmos/azure-cosmos/azure/cosmos/_routing
- aio
Lines changed: 14 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
100 | 113 | | |
101 | 114 | | |
102 | 115 | | |
| |||
124 | 137 | | |
125 | 138 | | |
126 | 139 | | |
127 | | - | |
| 140 | + | |
128 | 141 | | |
129 | 142 | | |
130 | 143 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
85 | 98 | | |
86 | 99 | | |
87 | 100 | | |
| |||
Lines changed: 29 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
30 | 55 | | |
31 | 56 | | |
32 | 57 | | |
| |||
39 | 64 | | |
40 | 65 | | |
41 | 66 | | |
| 67 | + | |
| 68 | + | |
42 | 69 | | |
43 | 70 | | |
44 | 71 | | |
| |||
50 | 77 | | |
51 | 78 | | |
52 | 79 | | |
53 | | - | |
54 | | - | |
| 80 | + | |
| 81 | + | |
55 | 82 | | |
56 | 83 | | |
57 | 84 | | |
| |||
0 commit comments