Skip to content

Commit d0f7699

Browse files
committed
Add __slots__ to Tags.
This reduces memory usage a bit per each Tags instance. Because each TestCase and more importantly Keyword object has tags, that adds up. This is part of memory reduction explained in #4114.
1 parent afa428c commit d0f7699

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/robot/model/tags.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,34 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from robot.utils import (Matcher, normalize, NormalizedDict, is_string, py3to2,
17-
setter, unic, unicode)
16+
from robot.utils import (is_string, normalize, NormalizedDict, Matcher, py3to2,
17+
unic, unicode)
1818

1919

2020
@py3to2
2121
class Tags(object):
22+
__slots__ = ['_tags']
2223

2324
def __init__(self, tags=None):
24-
self._tags = tags
25-
26-
@setter
27-
def _tags(self, tags):
2825
if not tags:
29-
return ()
30-
if is_string(tags):
26+
tags = ()
27+
elif is_string(tags):
3128
tags = (tags,)
32-
return self._deduplicate_normalized(tags)
29+
self._tags = self._normalize(tags)
3330

34-
def _deduplicate_normalized(self, tags):
31+
def _normalize(self, tags):
3532
normalized = NormalizedDict(((unic(t), 1) for t in tags), ignore='_')
3633
for removed in '', 'NONE':
3734
if removed in normalized:
3835
normalized.pop(removed)
3936
return tuple(normalized)
4037

4138
def add(self, tags):
42-
self._tags = tuple(self) + tuple(Tags(tags))
39+
self._tags = self._normalize(tuple(self) + tuple(Tags(tags)))
4340

4441
def remove(self, tags):
4542
tags = TagPatterns(tags)
46-
self._tags = [t for t in self if not tags.match(t)]
43+
self._tags = tuple([t for t in self if not tags.match(t)])
4744

4845
def match(self, tags):
4946
return TagPatterns(tags).match(self)

utest/model/test_tags.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22

3-
from robot.utils.asserts import assert_equal, assert_false, assert_not_equal, assert_true
3+
from robot.utils.asserts import (assert_equal, assert_false, assert_not_equal,
4+
assert_raises, assert_true)
45
from robot.utils import seq2str, IRONPYTHON, PY2, unicode
56
from robot.model.tags import Tags, TagPattern, TagPatterns
67

@@ -156,6 +157,10 @@ def test__eq__normalized(self):
156157
assert_equal(Tags(['Hello world', 'Foo', 'Not_world']),
157158
Tags(['nOT WORLD', 'FOO', 'hello world']))
158159

160+
def test__slots__(self):
161+
assert_raises(AttributeError, setattr, Tags(), 'attribute', 'value')
162+
163+
159164
class TestNormalizing(unittest.TestCase):
160165

161166
def test_empty(self):

0 commit comments

Comments
 (0)