Skip to content

perf: precompute address hash instead of per-instance lru_cache#1774

Draft
bluetoothbot wants to merge 3 commits into
python-zeroconf:masterfrom
bluetoothbot:koan/fix-ipaddress-hash-caching
Draft

perf: precompute address hash instead of per-instance lru_cache#1774
bluetoothbot wants to merge 3 commits into
python-zeroconf:masterfrom
bluetoothbot:koan/fix-ipaddress-hash-caching

Conversation

@bluetoothbot
Copy link
Copy Markdown
Contributor

@bluetoothbot bluetoothbot commented May 24, 2026

What

Replace the per-instance functools.cache(lambda ...) assigned to self.__hash__ in ZeroconfIPv4Address / ZeroconfIPv6Address with a hash precomputed once into an int slot, exposed through a real __hash__ method.

Why

Same class of bug as Bluetooth-Devices/cached-ipaddress#108. The old code did:

self.__hash__ = cache(lambda: IPv4Address.__hash__(self))

Because __hash__ was listed in __slots__, the memoization did take effect here (unlike the cached-ipaddress case, where it was dead code). But every address object still paid to allocate a full lru_cache wrapper and a self-capturing lambda at construction, plus created a self → slot → wrapper → lambda → self reference cycle the GC has to collect. get_ip_address_object_from_record builds one of these per A/AAAA record on a cache miss, so the construction cost is on the discovery hot path.

How

  • Compute self._hash = IPv4Address.__hash__(self) (resp. IPv6) once in __init__, store in a plain int slot.
  • Add a real __hash__ method returning it — defined on the type, so dunder lookup resolves correctly and no per-instance wrapper is allocated.
  • Swap "__hash__""_hash" in __slots__; drop the now-unused functools.cache import and the type: ignore[method-assign].

No cdef class declaration for these types in ipaddress.pxd, so they stay pure-Python classes under the Cython build; cython -3 compiles the module cleanly.

Testing

  • SKIP_CYTHON=1 poetry run pytest tests/utils/test_ipaddress.py tests/services/test_info.py tests/test_listener.py tests/utils/ — 85 passed, 1 skipped, 1 xpassed.
  • New regression test pins hash equality with stdlib addresses and set-dedup of equal addresses.
  • Pure-Python microbenchmark: IPv4 construction ~14.7µs → ~10.8µs per object; hash stays memoized.

Stacked on the benchmark PR #1773 — this branch includes that commit so CodSpeed can measure the delta. Once #1773 merges, rebasing on master narrows this PR to the fix commit alone.


Quality Report

Changes: 3 files changed, 75 insertions(+), 5 deletions(-)

Code scan: clean

Tests: passed (4 PASSED)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

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.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.77%. Comparing base (bd20c8e) to head (7f7eea6).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1774   +/-   ##
=======================================
  Coverage   99.77%   99.77%           
=======================================
  Files          33       33           
  Lines        3536     3540    +4     
  Branches      498      498           
=======================================
+ Hits         3528     3532    +4     
  Misses          5        5           
  Partials        3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 24, 2026

Merging this PR will improve performance by 48.56%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
✅ 16 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_create_ipv4_addresses 36.5 ms 23.4 ms +55.87%
test_create_ipv6_addresses 45.2 ms 31.9 ms +41.6%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing bluetoothbot:koan/fix-ipaddress-hash-caching (7f7eea6) with master (bd20c8e)

Open in CodSpeed

@bdraco bdraco deployed to release May 24, 2026 19:37 — with GitHub Actions Active
@bdraco
Copy link
Copy Markdown
Member

bdraco commented May 24, 2026

good fix, but can't merge it because we are out of space on pypi. it has to wait a week or so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants