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.
pull/7607/head
Paolo Bonzini 5 years ago
parent 3d4fb02e29
commit 372f420778
  1. 5
      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:

Loading…
Cancel
Save