Commit 03a5df08 authored by Andreas Zeidler's avatar Andreas Zeidler

avoid pre-allocating the entire sequence, which adds unnecessary overhead in...

avoid pre-allocating the entire sequence, which adds unnecessary overhead in most cases.  instead use a dictionary to remember the already mapped sequence items.
parent beedc480
...@@ -146,24 +146,18 @@ class LazyMap(Lazy): ...@@ -146,24 +146,18 @@ class LazyMap(Lazy):
def __init__(self, func, seq, length=None): def __init__(self, func, seq, length=None):
self._seq = seq self._seq = seq
self._data = {}
self._func = func self._func = func
if length is not None: if length is not None:
self._len = length self._len = length
else: else:
self._len = len(seq) self._len = len(seq)
self._marker = object()
self._data = [self._marker] * self._len
def __getitem__(self, index): def __getitem__(self, index):
data = self._data data = self._data
try: if index in data:
s = self._seq
except AttributeError:
return data[index] return data[index]
value = data[index] = self._func(self._seq[index])
value = data[index]
if value is self._marker:
value = data[index] = self._func(s[index])
return value return value
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment