Skip to content

Commit 25abe40

Browse files
bonzinijpakkane
authored andcommitted
arglist: optimize __init__()
"Inline" CompilerArgs.__iter__() into CompilerArgs.__init__(), so that replace list(Iterable) is replaced by the much faster list(List). Before: ncalls tottime cumtime 19268 0.163 3.586 arglist.py:97(__init__) After: ncalls tottime cumtime 18674 0.211 3.442 arglist.py:97(__init__) Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 92c517e commit 25abe40

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

mesonbuild/arglist.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ class CompilerArgs(T.MutableSequence[str]):
9797
def __init__(self, compiler: T.Union['Compiler', 'StaticLinker'],
9898
iterable: T.Optional[T.Iterable[str]] = None):
9999
self.compiler = compiler
100+
101+
if isinstance(iterable, CompilerArgs):
102+
iterable.flush_pre_post()
103+
# list(iter(x)) is over two times slower than list(x), so
104+
# pass the underlying list to list() directly, instead of an iterator
105+
iterable = iterable._container
100106
self._container: T.List[str] = list(iterable) if iterable is not None else []
101107
self.pre: T.Deque[str] = collections.deque()
102108
self.post: T.Deque[str] = collections.deque()
@@ -140,6 +146,7 @@ def flush_pre_post(self) -> None:
140146
self.post.clear()
141147

142148
def __iter__(self) -> T.Iterator[str]:
149+
# see also __init__, where this method is essentially inlined
143150
self.flush_pre_post()
144151
return iter(self._container)
145152

0 commit comments

Comments
 (0)