From 38866a396310a3cebcdc92b8e82b77aed5098be1 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Fri, 29 Jul 2022 11:06:17 -0700 Subject: [PATCH] Optimize ShardRanges a little We make a lot of ShardRange objects, pretty much all the time. Try to make it a little better: * Add __slots__ to improve memory consumption and attribute lookups. * Avoid the overhead of catch_warnings() by not tripping the warning. Change-Id: Ib1c698be9e9f579649bc81acf3562c92feb6c8d3 --- swift/common/utils.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/swift/common/utils.py b/swift/common/utils.py index ab6615a4c6..1b70a6c302 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -5216,6 +5216,11 @@ class ShardRange(object): MIN = MinBound() MAX = MaxBound() + __slots__ = ( + 'account', 'container', + '_timestamp', '_meta_timestamp', '_state_timestamp', '_epoch', + '_lower', '_upper', '_deleted', '_state', '_count', '_bytes', + '_tombstones', '_reported') def __init__(self, name, timestamp, lower=MIN, upper=MAX, object_count=0, bytes_used=0, meta_timestamp=None, @@ -5352,10 +5357,9 @@ class ShardRange(object): @lower.setter def lower(self, value): - with warnings.catch_warnings(): - warnings.simplefilter('ignore', UnicodeWarning) - if value in (None, b'', u''): - value = ShardRange.MIN + if value is None or (value == b"" if isinstance(value, bytes) else + value == u""): + value = ShardRange.MIN try: value = self._encode_bound(value) except TypeError as err: @@ -5380,10 +5384,9 @@ class ShardRange(object): @upper.setter def upper(self, value): - with warnings.catch_warnings(): - warnings.simplefilter('ignore', UnicodeWarning) - if value in (None, b'', u''): - value = ShardRange.MAX + if value is None or (value == b"" if isinstance(value, bytes) else + value == u""): + value = ShardRange.MAX try: value = self._encode_bound(value) except TypeError as err: