-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Caches for ClusterType and ClusterBehavior
This should improve CPU and memory usage when dynamically configuring endpoints, though I'm unsure how significant the improvement will be.
- Loading branch information
Showing
4 changed files
with
139 additions
and
16 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
packages/node/src/behavior/cluster/ClusterBehaviorCache.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @license | ||
* Copyright 2022-2025 Project CHIP Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Behavior } from "#behavior/Behavior.js"; | ||
import { ClusterBehavior } from "#index.js"; | ||
import { ClusterType } from "#types"; | ||
import { Schema } from "../supervision/Schema.js"; | ||
|
||
/** | ||
* To save memory we cache behavior implementations specialized for specific clusters. This allows for efficient | ||
* configuration of behaviors with conditional runtime logic. | ||
* | ||
* We use the cluster and schema as cache keys so this relies on similar caching for those items. | ||
*/ | ||
const typeCache = new WeakMap< | ||
Behavior.Type, | ||
WeakMap<ClusterType, WeakMap<Schema, WeakRef<ClusterBehavior.Type<any>>>> | ||
>(); | ||
|
||
export namespace ClusterBehaviorCache { | ||
export function get(cluster: ClusterType, base: Behavior.Type, schema: Schema) { | ||
const baseCache = typeCache.get(base); | ||
if (baseCache === undefined) { | ||
return; | ||
} | ||
|
||
const clusterCache = baseCache.get(cluster); | ||
if (clusterCache === undefined) { | ||
return; | ||
} | ||
|
||
return clusterCache.get(schema)?.deref(); | ||
} | ||
|
||
export function set(cluster: ClusterType, base: Behavior.Type, schema: Schema, type: ClusterBehavior.Type) { | ||
let baseCache = typeCache.get(base); | ||
if (baseCache === undefined) { | ||
typeCache.set(base, (baseCache = new WeakMap())); | ||
} | ||
|
||
let clusterCache = baseCache.get(cluster); | ||
if (clusterCache === undefined) { | ||
baseCache.set(cluster, (clusterCache = new WeakMap())); | ||
} | ||
|
||
clusterCache.set(schema, new WeakRef(type)); | ||
} | ||
} |
This file contains 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
32 changes: 32 additions & 0 deletions
32
packages/node/test/behavior/cluster/ClusterBehaviorCacheTest.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license | ||
* Copyright 2022-2025 Project CHIP Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { OnOffServer } from "#behaviors/on-off"; | ||
|
||
describe("ClusterBehaviorCache", () => { | ||
it("caches for with", () => { | ||
const Type1 = OnOffServer.with("Lighting"); | ||
const Type2 = OnOffServer.with("Lighting"); | ||
expect(Type1).equals(Type2); | ||
}); | ||
|
||
it("doesn't confuse base with variant", () => { | ||
const Type1 = OnOffServer.with("Lighting"); | ||
expect(Type1).not.equals(OnOffServer); | ||
}); | ||
|
||
it("doesn't confuse multiple variants", () => { | ||
const Type1 = OnOffServer.with("Lighting"); | ||
const Type2 = OnOffServer.with("DeadFrontBehavior"); | ||
expect(Type1).not.equals(Type2); | ||
}); | ||
|
||
it("is not sensitive to feature order", () => { | ||
const Type1 = OnOffServer.with("Lighting", "DeadFrontBehavior"); | ||
const Type2 = OnOffServer.with("DeadFrontBehavior", "Lighting"); | ||
expect(Type1).equals(Type2); | ||
}); | ||
}); |
This file contains 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