diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2018-07-09 06:11:40 +0100 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2018-07-23 06:55:39 -0700 |
commit | e0fb54f5751e93bcb36eccc00681a9afc35e1c37 (patch) | |
tree | 904cea6452cb5651e429db327417a834f813f8cb /numpy/core/_internal.py | |
parent | a52634d7ea38f74ba286580f96c35190b2b3b549 (diff) | |
download | numpy-e0fb54f5751e93bcb36eccc00681a9afc35e1c37.tar.gz |
MAINT: Improve memory usage in PEP3118 format parsing
Previously a local `Stream` class would be defined every time a format needed parsing.
Classes in cpython create reference cycles, which create load on the GC.
This may or may not resolve gh-6511
Diffstat (limited to 'numpy/core/_internal.py')
-rw-r--r-- | numpy/core/_internal.py | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 9990bacf0..1cf89aab0 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -444,46 +444,46 @@ _pep3118_standard_map = { } _pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys()) -def _dtype_from_pep3118(spec): - class Stream(object): - def __init__(self, s): - self.s = s - self.byteorder = '@' +class _Stream(object): + def __init__(self, s): + self.s = s + self.byteorder = '@' - def advance(self, n): - res = self.s[:n] - self.s = self.s[n:] - return res + def advance(self, n): + res = self.s[:n] + self.s = self.s[n:] + return res - def consume(self, c): - if self.s[:len(c)] == c: - self.advance(len(c)) - return True - return False - - def consume_until(self, c): - if callable(c): - i = 0 - while i < len(self.s) and not c(self.s[i]): - i = i + 1 - return self.advance(i) - else: - i = self.s.index(c) - res = self.advance(i) - self.advance(len(c)) - return res + def consume(self, c): + if self.s[:len(c)] == c: + self.advance(len(c)) + return True + return False - @property - def next(self): - return self.s[0] + def consume_until(self, c): + if callable(c): + i = 0 + while i < len(self.s) and not c(self.s[i]): + i = i + 1 + return self.advance(i) + else: + i = self.s.index(c) + res = self.advance(i) + self.advance(len(c)) + return res - def __bool__(self): - return bool(self.s) - __nonzero__ = __bool__ + @property + def next(self): + return self.s[0] - stream = Stream(spec) + def __bool__(self): + return bool(self.s) + __nonzero__ = __bool__ + +def _dtype_from_pep3118(spec): + stream = _Stream(spec) dtype, align = __dtype_from_pep3118(stream, is_subdtype=False) return dtype |