Skip to content

Commit 0bb366b

Browse files
bonzinidcbaker
authored andcommitted
utils, backends: add and use unique_list
backends.py has an interesting idiom for keeping the unique elements of a list. This is much faster than OrderedSet: * dict.fromkeys() vs. OrderedSet.__init__(): 35% faster on Python 3.13, 50-60% faster on Python 3.10 * list(d) (d is a dict) vs. list(os) (os is an OrderedSet): up to 25% faster on Python 3.13, up to 15% faster on Python 3.10 though it tapers out after a few hundred elements. Add a function to mesonlib to encapsulate this idiom. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 8b3c6c9 commit 0bb366b

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

mesonbuild/backend/backends.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
File, MachineChoice, MesonException, MesonBugException, OrderedSet,
3030
ExecutableSerialisation, EnvironmentException,
3131
classify_unity_sources, get_compiler_for_source,
32-
get_rsp_threshold,
32+
get_rsp_threshold, unique_list
3333
)
3434
from ..options import OptionKey
3535

@@ -471,11 +471,11 @@ def relpath(todir: str, fromdir: str) -> str:
471471
def flatten_object_list(self, target: build.BuildTarget, proj_dir_to_build_root: str = ''
472472
) -> T.Tuple[T.List[str], T.List[build.BuildTargetTypes]]:
473473
obj_list, deps = self._flatten_object_list(target, target.get_objects(), proj_dir_to_build_root)
474-
return list(dict.fromkeys(obj_list)), deps
474+
return unique_list(obj_list), deps
475475

476476
def determine_ext_objs(self, objects: build.ExtractedObjects) -> T.List[str]:
477477
obj_list, _ = self._flatten_object_list(objects.target, [objects], '')
478-
return list(dict.fromkeys(obj_list))
478+
return unique_list(obj_list)
479479

480480
def _flatten_object_list(self, target: build.BuildTarget,
481481
objects: T.Sequence[T.Union[str, 'File', build.ExtractedObjects]],

mesonbuild/utils/universal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class _VerPickleLoadable(Protocol):
154154
'substitute_values',
155155
'substring_is_in_list',
156156
'typeslistify',
157+
'unique_list',
157158
'verbose_git',
158159
'version_compare',
159160
'version_compare_condition_with_min',
@@ -2081,6 +2082,10 @@ def substring_is_in_list(substr: str, strlist: T.List[str]) -> bool:
20812082
return False
20822083

20832084

2085+
def unique_list(x: T.Iterable[_T]) -> T.List[_T]:
2086+
return list(dict.fromkeys(x))
2087+
2088+
20842089
class OrderedSet(T.MutableSet[_T]):
20852090
"""A set that preserves the order in which items are added, by first
20862091
insertion.

0 commit comments

Comments
 (0)