Skip to content

perf(system): store value instead of version number in Link for dirty check #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export function computed<T>(getter: (cachedValue?: T) => T): Computed<T> {
}

export class Computed<T = any> implements IComputed, ISignal<T> {
cachedValue: T | undefined = undefined;
version = 0;
currentValue: T | undefined = undefined;

// Dependency
subs: Link | undefined = undefined;
Expand All @@ -36,38 +35,31 @@ export class Computed<T = any> implements IComputed, ISignal<T> {
this.flags &= ~SubscriberFlags.ToCheckDirty;
}
}
const currentValue = this.currentValue!;
if (activeTrackId) {
if (this.lastTrackedId !== activeTrackId) {
this.lastTrackedId = activeTrackId;
link(this, activeSub!).version = this.version;
link(this, activeSub!).value = currentValue;
}
} else if (activeScopeTrackId) {
if (this.lastTrackedId !== activeScopeTrackId) {
this.lastTrackedId = activeScopeTrackId;
link(this, activeEffectScope!).version = this.version;
link(this, activeEffectScope!).value = currentValue;
}
}
return this.cachedValue!;
return currentValue;
}

update(): boolean {
update(): T {
const prevSub = activeSub;
const prevTrackId = activeTrackId;
setActiveSub(this, nextTrackId());
startTrack(this);
const oldValue = this.cachedValue;
let newValue: T;
try {
newValue = this.getter(oldValue);
return this.currentValue = this.getter(this.currentValue);
} finally {
setActiveSub(prevSub, prevTrackId);
endTrack(this);
}
if (oldValue !== newValue) {
this.cachedValue = newValue;
this.version++;
return true;
}
return false;
}
}
54 changes: 29 additions & 25 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export interface IEffect extends Subscriber {
}

export interface IComputed extends Dependency, Subscriber {
version: number;
update(): boolean;
update(): any;
}

export interface Dependency {
currentValue?: any;
subs: Link | undefined;
subsTail: Link | undefined;
lastTrackedId?: number;
Expand All @@ -23,7 +23,7 @@ export interface Subscriber {
export interface Link {
dep: Dependency | IComputed | (Dependency & IEffect);
sub: Subscriber | IComputed | (Dependency & IEffect) | IEffect;
version: number;
value: any;
// Reuse to link prev stack in checkDirty
// Reuse to link prev stack in propagate
prevSub: Link | undefined;
Expand Down Expand Up @@ -79,9 +79,8 @@ export function link(dep: Dependency, sub: Subscriber): Link {
if (nextDep !== undefined && nextDep.dep === dep) {
sub.depsTail = nextDep;
return nextDep;
} else {
return linkNewDep(dep, sub, nextDep, currentDep);
}
return linkNewDep(dep, sub, nextDep, currentDep);
}

function linkNewDep(dep: Dependency, sub: Subscriber, nextDep: Link | undefined, depsTail: Link | undefined): Link {
Expand All @@ -97,7 +96,7 @@ function linkNewDep(dep: Dependency, sub: Subscriber, nextDep: Link | undefined,
newLink = {
dep,
sub,
version: 0,
value: undefined,
nextDep,
prevSub: undefined,
nextSub: undefined,
Expand Down Expand Up @@ -243,47 +242,51 @@ function isValidLink(subLink: Link, sub: Subscriber) {
}

// See https://github.com/stackblitz/alien-signals#about-propagate-and-checkdirty-functions
export function checkDirty(deps: Link): boolean {
export function checkDirty(link: Link): boolean {
let stack = 0;
let dirty: boolean;
let nextDep: Link | undefined;

top: do {
dirty = false;
const dep = deps.dep;
const dep = link.dep;
if ('update' in dep) {
if (dep.version !== deps.version) {
dirty = true;
} else {
const depFlags = dep.flags;
if (depFlags & SubscriberFlags.Dirty) {
dirty = dep.update();
} else if (depFlags & SubscriberFlags.ToCheckDirty) {
dep.subs!.prevSub = deps;
deps = dep.deps!;
++stack;
continue;
const depFlags = dep.flags;
if (depFlags & SubscriberFlags.Dirty) {
if (dep.update() !== link.value) {
dirty = true;
}
} else if (depFlags & SubscriberFlags.ToCheckDirty) {
dep.subs!.prevSub = link;
link = dep.deps!;
++stack;
continue;
} else if (dep.currentValue !== link.value) {
dirty = true;
}
} else if ('currentValue' in dep) {
if (dep.currentValue !== link.value) {
dirty = true;
}
}
if (dirty || (nextDep = deps.nextDep) === undefined) {
if (dirty || (nextDep = link.nextDep) === undefined) {
if (stack) {
let sub = deps.sub as IComputed;
let sub = link.sub as IComputed;
do {
--stack;
const subSubs = sub.subs!;
const prevLink = subSubs.prevSub!;
subSubs.prevSub = undefined;
if (dirty) {
if (sub.update()) {
if (sub.update() !== prevLink.value) {
sub = prevLink.sub as IComputed;
continue;
}
} else {
sub.flags &= ~SubscriberFlags.ToCheckDirty;
}
deps = prevLink.nextDep!;
if (deps !== undefined) {
link = prevLink.nextDep!;
if (link !== undefined) {
continue top;
}
sub = prevLink.sub as IComputed;
Expand All @@ -292,7 +295,7 @@ export function checkDirty(deps: Link): boolean {
}
return dirty;
}
deps = nextDep;
link = nextDep;
} while (true);
}

Expand Down Expand Up @@ -343,6 +346,7 @@ function clearTrack(link: Link): void {
link.dep = undefined;
// @ts-expect-error
link.sub = undefined;
link.value = undefined;
link.nextDep = linkPool;
linkPool = link;

Expand Down
Loading