Skip to content

perf: add slots=True to value and device dataclasses - #142

Draft
bluetoothbot wants to merge 1 commit into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/dataclass-slots
Draft

perf: add slots=True to value and device dataclasses#142
bluetoothbot wants to merge 1 commit into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/dataclass-slots

Conversation

@bluetoothbot

@bluetoothbot bluetoothbot commented Jun 5, 2026

Copy link
Copy Markdown

What

Add slots=True to the frozen dataclasses in value.py (BaseValue, SensorValue, BinarySensorValue, Event) and device.py (DeviceKey).

Why

These are the per-reading value objects: every sensor/binary/event update constructs a DeviceKey plus a *Value. In BLE parsing hot paths they're created in volume. slots=True removes the per-instance __dict__, lowering memory footprint and speeding attribute access — relevant on the constrained devices this library targets.

How

  • slots=True composes cleanly with frozen=True; the dataclass slot layout is computed per-class from each class's own fields, so the BaseValue → subclass hierarchy stays correct (verified: eq, hash, immutability all preserved).
  • description.py is deliberately not changed — its diamond multiple inheritance (SensorDescription(BaseSensorDescription, KeyedBaseDescription)) raises multiple bases have instance lay-out conflict under 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), ruff clean.


Quality Report

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

Code scan: clean

Tests: passed (16 passed)

Branch hygiene: clean

Generated by Kōan

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

codecov Bot commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.37%. Comparing base (31bd826) to head (541b957).
⚠️ Report is 1 commits behind head on main.

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.
📢 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.

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.

1 participant