Skip to content

Commit 50ce5ff

Browse files
committed
Merge pull request #5664 from mdboom/performance
PRF: Low-hanging performance improvements
2 parents 7868f0b + 8a69511 commit 50ce5ff

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

lib/matplotlib/axis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ def reset_ticks(self):
761761
# build a few default ticks; grow as necessary later; only
762762
# define 1 so properties set on ticks will be copied as they
763763
# grow
764-
cbook.popall(self.majorTicks)
765-
cbook.popall(self.minorTicks)
764+
del self.majorTicks[:]
765+
del self.minorTicks[:]
766766

767767
self.majorTicks.extend([self._get_tick(major=True)])
768768
self.minorTicks.extend([self._get_tick(major=False)])

lib/matplotlib/cbook.py

-6
Original file line numberDiff line numberDiff line change
@@ -1392,12 +1392,6 @@ def remove(self, o):
13921392
self.push(thiso)
13931393

13941394

1395-
def popall(seq):
1396-
'empty a list'
1397-
for i in xrange(len(seq)):
1398-
seq.pop()
1399-
1400-
14011395
def finddir(o, match, case=False):
14021396
"""
14031397
return all attributes of *o* which match string in match. if case

lib/matplotlib/markers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
CARETLEFT, CARETRIGHT, CARETUP, CARETDOWN,
9898
CARETLEFTBASE, CARETRIGHTBASE, CARETUPBASE, CARETDOWNBASE) = list(xrange(12))
9999

100+
_empty_path = Path(np.empty((0, 2)))
101+
100102

101103
class MarkerStyle(object):
102104

@@ -190,7 +192,7 @@ def __setstate__(self, statedict):
190192
self._recache()
191193

192194
def _recache(self):
193-
self._path = Path(np.empty((0, 2)))
195+
self._path = _empty_path
194196
self._transform = IdentityTransform()
195197
self._alt_path = None
196198
self._alt_transform = None

lib/matplotlib/tests/test_pickle.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ def test_transform():
278278
# Check parent -> child links of TransformWrapper.
279279
assert_equal(obj.wrapper._child, obj.composite)
280280
# Check child -> parent links of TransformWrapper.
281-
assert_equal(list(obj.wrapper._parents.values()), [obj.composite2])
281+
assert_equal(
282+
[v() for v in obj.wrapper._parents.values()], [obj.composite2])
282283
# Check input and output dimensions are set as expected.
283284
assert_equal(obj.wrapper.input_dims, obj.composite.input_dims)
284285
assert_equal(obj.wrapper.output_dims, obj.composite.output_dims)

lib/matplotlib/transforms.py

+25-16
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
update_path_extents)
4141
from numpy.linalg import inv
4242

43-
from weakref import WeakValueDictionary
43+
import weakref
4444
import warnings
4545
try:
4646
set
@@ -92,10 +92,7 @@ def __init__(self, shorthand_name=None):
9292
other than to improve the readability of
9393
``str(transform)`` when DEBUG=True.
9494
"""
95-
# Parents are stored in a WeakValueDictionary, so that if the
96-
# parents are deleted, references from the children won't keep
97-
# them alive.
98-
self._parents = WeakValueDictionary()
95+
self._parents = {}
9996

10097
# TransformNodes start out as invalid until their values are
10198
# computed for the first time.
@@ -109,14 +106,17 @@ def __str__(self):
109106

110107
def __getstate__(self):
111108
d = self.__dict__.copy()
112-
# turn the weakkey dictionary into a normal dictionary
113-
d['_parents'] = dict(six.iteritems(self._parents))
109+
# turn the dictionary with weak values into a normal dictionary
110+
d['_parents'] = dict((k, v()) for (k, v) in
111+
six.iteritems(self._parents))
114112
return d
115113

116114
def __setstate__(self, data_dict):
117115
self.__dict__ = data_dict
118-
# turn the normal dictionary back into a WeakValueDictionary
119-
self._parents = WeakValueDictionary(self._parents)
116+
# turn the normal dictionary back into a dictionary with weak
117+
# values
118+
self._parents = dict((k, weakref.ref(v)) for (k, v) in
119+
six.iteritems(self._parents) if v is not None)
120120

121121
def __copy__(self, *args):
122122
raise NotImplementedError(
@@ -156,8 +156,11 @@ def _invalidate_internal(self, value, invalidating_node):
156156
self._invalid = value
157157

158158
for parent in list(six.itervalues(self._parents)):
159-
parent._invalidate_internal(value=value,
160-
invalidating_node=self)
159+
# Dereference the weak reference
160+
parent = parent()
161+
if parent is not None:
162+
parent._invalidate_internal(
163+
value=value, invalidating_node=self)
161164

162165
def set_children(self, *children):
163166
"""
@@ -166,8 +169,11 @@ def set_children(self, *children):
166169
Should be called from the constructor of any transforms that
167170
depend on other transforms.
168171
"""
172+
# Parents are stored as weak references, so that if the
173+
# parents are destroyed, references from the children won't
174+
# keep them alive.
169175
for child in children:
170-
child._parents[id(self)] = self
176+
child._parents[id(self)] = weakref.ref(self)
171177

172178
if DEBUG:
173179
_set_children = set_children
@@ -1560,8 +1566,9 @@ def __getstate__(self):
15601566
'child': self._child,
15611567
'input_dims': self.input_dims,
15621568
'output_dims': self.output_dims,
1563-
# turn the weakkey dictionary into a normal dictionary
1564-
'parents': dict(six.iteritems(self._parents))
1569+
# turn the weak-values dictionary into a normal dictionary
1570+
'parents': dict((k, v()) for (k, v) in
1571+
six.iteritems(self._parents))
15651572
}
15661573

15671574
def __setstate__(self, state):
@@ -1570,8 +1577,10 @@ def __setstate__(self, state):
15701577
# The child may not be unpickled yet, so restore its information.
15711578
self.input_dims = state['input_dims']
15721579
self.output_dims = state['output_dims']
1573-
# turn the normal dictionary back into a WeakValueDictionary
1574-
self._parents = WeakValueDictionary(state['parents'])
1580+
# turn the normal dictionary back into a dictionary with weak
1581+
# values
1582+
self._parents = dict((k, weakref.ref(v)) for (k, v) in
1583+
six.iteritems(state['parents']) if v is not None)
15751584

15761585
def __repr__(self):
15771586
return "TransformWrapper(%r)" % self._child

0 commit comments

Comments
 (0)