Skip to content

Commit db4ef3e

Browse files
perf: keep the clean computed read free of try and constant loads
Only the evaluation branches can throw, so only they sit in a try that keeps the reader linked; the clean read is unchanged from before. The Dirty | Failed mask is a literal because Failed is a module const that unbundled ESM would otherwise load on every read. Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
1 parent f1c3347 commit db4ef3e

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

‎src/index.ts‎

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -360,41 +360,48 @@ function flush(): void {
360360

361361
function computedOper<T>(this: ComputedNode<T>): T {
362362
const flags = this.flags;
363-
try {
364-
if (
365-
flags & (ReactiveFlags.Dirty | Failed)
366-
|| (
367-
flags & ReactiveFlags.Pending
368-
&& (
369-
checkDirty(this.deps!, this)
370-
|| (this.flags = flags & ~ReactiveFlags.Pending, false)
371-
)
363+
if (
364+
// 144 = Dirty | Failed, spelled out so the hot read tests one literal.
365+
flags & 144
366+
|| !flags
367+
|| (
368+
flags & ReactiveFlags.Pending
369+
&& (
370+
checkDirty(this.deps!, this)
371+
|| (this.flags = flags & ~ReactiveFlags.Pending, false)
372372
)
373-
) {
374-
if (updateComputed(this)) {
373+
)
374+
) {
375+
try {
376+
if (!flags) {
377+
this.flags = ReactiveFlags.Mutable | ReactiveFlags.RecursedCheck;
378+
const prevSub = setActiveSub(this);
379+
try {
380+
this.value = this.getter();
381+
} catch (error) {
382+
this.flags |= Failed;
383+
throw error;
384+
} finally {
385+
activeSub = prevSub;
386+
this.flags &= ~ReactiveFlags.RecursedCheck;
387+
}
388+
} else if (updateComputed(this)) {
375389
const subs = this.subs;
376390
if (subs !== undefined) {
377391
shallowPropagate(subs);
378392
}
379393
}
380-
} else if (!flags) {
381-
this.flags = ReactiveFlags.Mutable | ReactiveFlags.RecursedCheck;
382-
const prevSub = setActiveSub(this);
383-
try {
384-
this.value = this.getter();
385-
} catch (error) {
386-
this.flags |= Failed;
387-
throw error;
388-
} finally {
389-
activeSub = prevSub;
390-
this.flags &= ~ReactiveFlags.RecursedCheck;
394+
} catch (error) {
395+
const sub = activeSub;
396+
if (sub !== undefined) {
397+
link(this, sub, cycle);
391398
}
399+
throw error;
392400
}
393-
} finally {
394-
const sub = activeSub;
395-
if (sub !== undefined) {
396-
link(this, sub, cycle);
397-
}
401+
}
402+
const sub = activeSub;
403+
if (sub !== undefined) {
404+
link(this, sub, cycle);
398405
}
399406
return this.value!;
400407
}

0 commit comments

Comments
 (0)