From 372f420778f8f4b55b207f9a6be2c20e2bd22c38 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 17 Aug 2020 15:39:50 +0200 Subject: File: precompute hash Most files are going to be looked up into a set or dictionary. Precompute the hash so that we only need to do so once and we can also use it to quickly weed out unequal objects. On a QEMU build, the time spent in __eq__ and __hash goes respectively from 3.110s to 2.162s and from 0.648s to 0.299s. Even larger gains are obtained by the next patch. --- mesonbuild/mesonlib.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'mesonbuild/mesonlib.py') diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index 4b8cce8..760b235 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -242,6 +242,7 @@ class File: self.is_built = is_built self.subdir = subdir self.fname = fname + self.hash = hash((is_built, subdir, fname)) def __str__(self) -> str: return self.relative_name() @@ -291,10 +292,12 @@ class File: def __eq__(self, other) -> bool: if not isinstance(other, File): return NotImplemented + if self.hash != other.hash: + return False return (self.fname, self.subdir, self.is_built) == (other.fname, other.subdir, other.is_built) def __hash__(self) -> int: - return hash((self.fname, self.subdir, self.is_built)) + return self.hash @lru_cache(maxsize=None) def relative_name(self) -> str: -- cgit v1.1