Skip to content

Commit 775eaf3

Browse files
committed
feat(zen): read path optimizations for extreme performance
- Fast path for CLEAN state (skip _updateIfNecessary call) - Inline track logic in Signal.get (avoid function call overhead) - Extract state once in read() (avoid repeated bitwise ops) - Optimized state checks with early returns Target improvements: - Single Read: >20M ops/sec (from 18.1M) - close gap with Solid 22.4M - Extreme Read (10000x): >80K ops/sec (from 64K) - close gap with Solid 318K - Computed Value Access: improve from 16.7M ops/sec All 48 tests pass.
1 parent 23996a2 commit 775eaf3

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

.changeset/read-optimizations.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@sylphx/zen': minor
3+
---
4+
5+
Read path optimizations for extreme performance
6+
7+
OPTIMIZATIONS:
8+
- Fast path for CLEAN state (skip _updateIfNecessary call)
9+
- Inline track logic in Signal.get (avoid function call overhead)
10+
- Extract state once in read() (avoid repeated bitwise ops)
11+
- Optimized state checks with early returns
12+
13+
BENCHMARK TARGETS:
14+
- Single Read: Target >20M ops/sec (from 18.1M) - close gap with Solid 22.4M
15+
- Extreme Read (10000x): Target >80K ops/sec (from 64K) - close gap with Solid 318K
16+
- Moderate Read (100x): Maintain 4.9M ops/sec
17+
- Computed Value Access: Improve from 16.7M ops/sec
18+
19+
These micro-optimizations reduce overhead in the hottest code paths (signal reads and computed value access).

packages/zen/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@sylphx/zen",
3-
"version": "3.34.0",
4-
"description": "Zen state management library - extreme minimalism, extreme speed. V3.33: Deep chain optimizations (early exit, CLEAN state setting, length caching).",
3+
"version": "3.35.0",
4+
"description": "Zen state management library - extreme minimalism, extreme speed. V3.35: Read path optimizations (fast CLEAN path, inline track, state extraction).",
55
"type": "module",
66
"main": "./dist/index.cjs",
77
"module": "./dist/index.js",

packages/zen/src/zen.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,15 @@ class Computation<T> implements SourceType, ObserverType, Owner {
242242
}
243243

244244
read(): T {
245-
// Track this source in current observer
245+
// OPTIMIZATION: Fast path for CLEAN state
246+
const state = this._state & 3;
247+
246248
if (currentObserver) {
247249
track(this);
248-
249-
// OPTIMIZATION: Only check if necessary
250-
if (this._state & 3) {
250+
if (state !== STATE_CLEAN) {
251251
this._updateIfNecessary();
252252
}
253-
} else if (this._state & 3) {
254-
// No observer - still need to update if dirty
253+
} else if (state !== STATE_CLEAN && state !== STATE_DISPOSED) {
255254
this._updateIfNecessary();
256255
}
257256

@@ -485,8 +484,20 @@ class Signal<T> implements SourceType {
485484
}
486485

487486
get value(): T {
487+
// OPTIMIZATION: Inline track check
488488
if (currentObserver) {
489-
track(this);
489+
// OPTIMIZATION: Compare with old sources first
490+
if (
491+
!newSources &&
492+
currentObserver._sources &&
493+
currentObserver._sources[newSourcesIndex] === this
494+
) {
495+
newSourcesIndex++;
496+
} else if (!newSources) {
497+
newSources = [this];
498+
} else if (this !== newSources[newSources.length - 1]) {
499+
newSources.push(this);
500+
}
490501
}
491502
return this._value;
492503
}

0 commit comments

Comments
 (0)