Skip to content

Conversation

@Nerivec
Copy link
Collaborator

@Nerivec Nerivec commented Jul 19, 2025

Throwing on getAttribute was used mostly for flow control, not great for perf. Also, most of the time, that was bypassed by the use of hasAttribute (now removed), which resulted in unnecessary dupe get logic.

Invalid attributes for endpoint/group read are now ignored instead of failing the whole request. Added some logging for it, and test coverage (wasn't covered before when it was throwing since the change didn't fail tests).

Note: will require checking a couple of places where getAttribute is used in ZHC & Z2M (will at least fail typing).

Also:

  • cleaned up a lot of frame matching (use Zcl.Frame.create instead of writing the whole object by hand).
  • updated vitest, for some reason it was quite behind.

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 19, 2025

CodSpeed Performance Report

Merging #1455 will improve performances by 98.36%

Comparing Nerivec:remove-throwing (b107e63) with master (a7fbacf)

Summary

⚡ 4 improvements
✅ 8 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
device.endpoint command 508.2 µs 321.1 µs +58.27%
device.endpoint defaultRsp 450.3 µs 320.5 µs +40.47%
device.endpoint write basic 756.8 µs 381.5 µs +98.36%
group read basic 271.1 µs 223 µs +21.58%

@Nerivec Nerivec marked this pull request as ready for review July 20, 2025 13:31
@Nerivec Nerivec requested a review from Copilot July 20, 2025 13:40
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes unnecessary exception throwing from the getAttribute method, changing it from a throwing function to one that returns undefined for invalid attributes. This change improves performance by eliminating flow control via exceptions and removes duplicate logic that was previously handled by the hasAttribute method.

  • Changed getAttribute to return Attribute | undefined instead of throwing for invalid attributes
  • Removed the hasAttribute method that was previously used to avoid exceptions
  • Updated all callers to handle undefined return values and log warnings for invalid attributes in read operations

Reviewed Changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
test/zspec/zcl/utils.test.ts Updated tests to expect undefined instead of thrown exceptions for invalid attributes
test/zspec/zcl/frame.test.ts Changed null parameters to undefined for consistency
test/zcl.test.ts Updated attribute checking tests and improved variable naming
test/controller.test.ts Refactored frame creation to use Zcl.Frame.create and added null checks for attributes
src/zspec/zcl/utils.ts Removed throwing behavior and hasAttribute method from cluster implementation
src/zspec/zcl/definition/tstype.ts Updated Cluster interface to reflect new getAttribute signature
src/zspec/zcl/buffaloZcl.ts Updated attribute lookup logic to handle undefined returns
src/controller/model/group.ts Added null checks and warning logging for invalid attributes
src/controller/model/endpoint.ts Added comprehensive null checks throughout attribute access patterns
src/controller/model/device.ts Updated custom read response logic to handle undefined attributes
src/controller/helpers/zclFrameConverter.ts Simplified attribute name resolution using nullish coalescing
package.json Updated vitest dependency version
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported
Comments suppressed due to low confidence (1)

test/controller.test.ts:7265

  • Debug console.log statement should be removed from test code as it adds unnecessary output during test execution.
        });

@Nerivec Nerivec requested a review from Koenkk July 22, 2025 11:39
@Nerivec
Copy link
Collaborator Author

Nerivec commented Jul 22, 2025

Corrected a flaw in the benchmarking, so last edit from codspeed report is not representative (previous edit is the right one, albeit with the flaw).

@Koenkk Koenkk changed the title fix: remove unnecessary throwing fix!: remove unnecessary throwing Jul 25, 2025
@Koenkk Koenkk merged commit e7087d5 into Koenkk:master Jul 25, 2025
2 checks passed
@Koenkk
Copy link
Owner

Koenkk commented Jul 25, 2025

For future reference, this PR has the following breaking changes:

  • cluster. hasAttribute() has been removed
  • cluster.getAttribute() now returns undefined instead of throwing an exception when called with a non-existing attribute

@Nerivec Nerivec deleted the remove-throwing branch July 25, 2025 18:20
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