Skip to content

Commit b5395b5

Browse files
committed
Simplify and improve performance of __reduce__
1 parent 72d30a0 commit b5395b5

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

sortedcontainers/sorteddict.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ def __reduce__(self):
572572
:func:`SortedDict.__init__` confuse pickle so customize the reducer.
573573
574574
"""
575-
return (self.__class__, (self._key, list(self.items())))
575+
items = dict.copy(self)
576+
return (type(self), (self._key, items))
576577

577578

578579
@recursive_repr()

sortedcontainers/sortedlist.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,11 @@ def comparer(self, other):
15801580
__make_cmp = staticmethod(__make_cmp)
15811581

15821582

1583+
def __reduce__(self):
1584+
values = reduce(iadd, self._lists, [])
1585+
return (type(self), (values,))
1586+
1587+
15831588
@recursive_repr()
15841589
def __repr__(self):
15851590
"""Return string representation of sorted list.
@@ -2526,6 +2531,11 @@ def __mul__(self, num):
25262531
return self.__class__(values, key=self._key)
25272532

25282533

2534+
def __reduce__(self):
2535+
values = reduce(iadd, self._lists, [])
2536+
return (type(self), (values, self.key))
2537+
2538+
25292539
@recursive_repr()
25302540
def __repr__(self):
25312541
"""Return string representation of sorted-key list.

sortedcontainers/sortedset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def __reduce__(self):
702702
confuse pickle so customize the reducer.
703703
704704
"""
705-
return (type(self), (self._set, self._key))
705+
return (type(self)._fromset, (self._set, self._key))
706706

707707

708708
@recursive_repr()

tests/test_coverage_sortedkeylist_negate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ def test_pickle():
597597
beta = pickle.loads(pickle.dumps(alpha))
598598
assert alpha == beta
599599
assert alpha._key == beta._key
600-
assert alpha._load == beta._load
600+
assert alpha._load == 500
601+
assert beta._load == 1000
601602

602603
def test_check():
603604
slt = SortedKeyList(range(10), key=negate)

tests/test_coverage_sortedlist.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ def test_pickle():
589589
alpha._reset(500)
590590
beta = pickle.loads(pickle.dumps(alpha))
591591
assert alpha == beta
592-
assert alpha._load == beta._load
592+
assert alpha._load == 500
593+
assert beta._load == 1000
593594

594595
def test_build_index():
595596
slt = SortedList([0])

0 commit comments

Comments
 (0)