perf: add slots=True to value and device dataclasses - #142
Draft
bluetoothbot wants to merge 1 commit into
Draft
Conversation
DeviceKey and the *Value/Event dataclasses are instantiated once per sensor reading in BLE parsing hot paths. Adding slots=True drops the per-instance __dict__, cutting memory and speeding attribute access, with no behavior change (frozen, eq, and hash semantics preserved). description.py is intentionally left alone: its multiple-inheritance hierarchy (SensorDescription(BaseSensorDescription, KeyedBaseDescription)) triggers a 'multiple bases have instance lay-out conflict' under slots. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #142 +/- ##
=======================================
Coverage 94.37% 94.37%
=======================================
Files 12 12
Lines 480 480
Branches 5 5
=======================================
Hits 453 453
Misses 26 26
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add
slots=Trueto the frozen dataclasses invalue.py(BaseValue,SensorValue,BinarySensorValue,Event) anddevice.py(DeviceKey).Why
These are the per-reading value objects: every sensor/binary/event update constructs a
DeviceKeyplus a*Value. In BLE parsing hot paths they're created in volume.slots=Trueremoves the per-instance__dict__, lowering memory footprint and speeding attribute access — relevant on the constrained devices this library targets.How
slots=Truecomposes cleanly withfrozen=True; the dataclass slot layout is computed per-class from each class's own fields, so theBaseValue→ subclass hierarchy stays correct (verified:eq,hash, immutability all preserved).description.pyis deliberately not changed — its diamond multiple inheritance (SensorDescription(BaseSensorDescription, KeyedBaseDescription)) raisesmultiple bases have instance lay-out conflictunder slots.Testing
New
tests/test_value.py: asserts instances carry no__dict__, stay frozen, and preserve equality/hash/field round-trip. Full suite green (16 passed),ruffclean.Quality Report
Changes: 3 files changed, 77 insertions(+), 5 deletions(-)
Code scan: clean
Tests: passed (16 passed)
Branch hygiene: clean
Generated by Kōan