◐ Shell
clean mode source ↗

perf: precompute address hash instead of per-instance lru_cache by bluetoothbot · Pull Request #1774 · python-zeroconf/python-zeroconf

@bluetoothbot

ZeroconfIPv4Address/ZeroconfIPv6Address assigned self.__hash__ to a
functools.cache(lambda ...) in __init__. Because __hash__ sat in
__slots__ the memoization did work, but every address object paid to
build a full lru_cache wrapper plus a self-capturing lambda at
construction time (and a self -> slot -> wrapper -> lambda -> self
reference cycle for the GC to reap).

Compute the hash once into a plain int slot and expose it through a
real __hash__ method. Construction drops ~25-45% in pure-Python micro-
benchmarks; hash() stays memoized via the precomputed slot.