Skip to content

Commit 128d459

Browse files
tvaron3Copilot
andcommitted
perf(cosmos): add __slots__ to Range and skip redundant .upper()
- Add __slots__ to Range class: eliminates per-instance __dict__, reducing from ~250 bytes to 64 bytes per Range object. - Skip .upper() when string is already uppercase: avoids creating unnecessary string copies since service returns uppercase hex. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 342d80a commit 128d459

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class PartitionKeyRange(object):
3939
class Range(object):
4040
"""description of class"""
4141

42+
__slots__ = ('min', 'max', 'isMinInclusive', 'isMaxInclusive')
43+
4244
MinPath = "min"
4345
MaxPath = "max"
4446
IsMinInclusivePath = "isMinInclusive"
@@ -50,8 +52,8 @@ def __init__(self, range_min, range_max, isMinInclusive, isMaxInclusive):
5052
if range_max is None:
5153
raise ValueError("max is missing")
5254

53-
self.min = range_min.upper()
54-
self.max = range_max.upper()
55+
self.min = range_min if range_min == range_min.upper() else range_min.upper()
56+
self.max = range_max if range_max == range_max.upper() else range_max.upper()
5557
self.isMinInclusive = isMinInclusive
5658
self.isMaxInclusive = isMaxInclusive
5759

sdk/cosmos/azure-cosmos/tests/routing/test_shared_pk_range_cache.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
import pytest
66

7+
import sys
78
from azure.cosmos._routing.routing_range import Range
89
from azure.cosmos._routing.collection_routing_map import CollectionRoutingMap
910
from azure.cosmos._routing.routing_map_provider import (
@@ -98,5 +99,30 @@ def test_clear_cache_does_not_affect_other_endpoints(self):
9899
self.assertIn("coll2", cache2._collection_routing_map_by_item)
99100

100101

102+
103+
def test_range_has_slots(self):
104+
"""Range class uses __slots__ to reduce per-instance memory."""
105+
r = Range("00", "FF", True, False)
106+
self.assertFalse(hasattr(r, "__dict__"))
107+
self.assertTrue(hasattr(Range, "__slots__"))
108+
109+
def test_range_slots_memory_savings(self):
110+
"""Range with __slots__ should be smaller than 100 bytes."""
111+
r = Range("00", "FF", True, False)
112+
self.assertLess(sys.getsizeof(r), 100)
113+
114+
def test_range_skips_upper_when_already_uppercase(self):
115+
"""Range reuses the same string object when input is already uppercase."""
116+
original = "05C1C9CD673398"
117+
r = Range(original, original, True, False)
118+
self.assertIs(r.min, original)
119+
self.assertIs(r.max, original)
120+
121+
def test_range_applies_upper_when_lowercase(self):
122+
"""Range uppercases lowercase input."""
123+
r = Range("05c1c9cd", "05c1d9cd", True, False)
124+
self.assertEqual(r.min, "05C1C9CD")
125+
self.assertEqual(r.max, "05C1D9CD")
126+
101127
if __name__ == "__main__":
102128
unittest.main()

0 commit comments

Comments
 (0)