arglist: optimize flush_pre_post

pre_flush_set and post_flush_set are almost always empty, so we can use
extend() instead of a for...in loop to add the previous elements of
self._container.

We can also skip the conversion from deque to list since pre_flush is
always appended on the right side.

On a QEMU build the time spent in flush_pre_post goes from 1.4 to 0.5
seconds.
pull/7607/head
Paolo Bonzini 4 years ago
parent 7092efabb5
commit b7a4c0793c
  1. 17
      mesonbuild/arglist.py

@ -119,7 +119,7 @@ class CompilerArgs(collections.abc.MutableSequence):
# This correctly deduplicates the entries after _can_dedup definition
# Note: This function is designed to work without delete operations, as deletions are worsening the performance a lot.
def flush_pre_post(self) -> None:
pre_flush = collections.deque() # type: T.Deque[str]
new = list() # type: T.List[str]
pre_flush_set = set() # type: T.Set[str]
post_flush = collections.deque() # type: T.Deque[str]
post_flush_set = set() # type: T.Set[str]
@ -128,7 +128,7 @@ class CompilerArgs(collections.abc.MutableSequence):
for a in self.pre:
dedup = self._can_dedup(a)
if a not in pre_flush_set:
pre_flush.append(a)
new.append(a)
if dedup is Dedup.OVERRIDEN:
pre_flush_set.add(a)
for a in reversed(self.post):
@ -140,12 +140,15 @@ class CompilerArgs(collections.abc.MutableSequence):
#pre and post will overwrite every element that is in the container
#only copy over args that are in _container but not in the post flush or pre flush set
if pre_flush_set or post_flush_set:
for a in self._container:
if a not in post_flush_set and a not in pre_flush_set:
new.append(a)
else:
new.extend(self._container)
new.extend(post_flush)
for a in self._container:
if a not in post_flush_set and a not in pre_flush_set:
pre_flush.append(a)
self._container = list(pre_flush) + list(post_flush)
self._container = new
self.pre.clear()
self.post.clear()

Loading…
Cancel
Save