-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(reactivity): use bitwise dep markers to optimize re-tracking (#4017
- Loading branch information
1 parent
803f966
commit d42cd9a
Showing
6 changed files
with
274 additions
and
49 deletions.
There are no files selected for viewing
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
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 @@ | ||
import { ReactiveEffect, getTrackOpBit } from './effect' | ||
|
||
export type Dep = Set<ReactiveEffect> & TrackedMarkers | ||
|
||
/** | ||
* wasTracked and newTracked maintain the status for several levels of effect | ||
* tracking recursion. One bit per level is used to define wheter the dependency | ||
* was/is tracked. | ||
*/ | ||
type TrackedMarkers = { wasTracked: number; newTracked: number } | ||
|
||
export function createDep(effects?: ReactiveEffect[]): Dep { | ||
const dep = new Set<ReactiveEffect>(effects) as Dep | ||
dep.wasTracked = 0 | ||
dep.newTracked = 0 | ||
return dep | ||
} | ||
|
||
export function wasTracked(dep: Dep): boolean { | ||
return hasBit(dep.wasTracked, getTrackOpBit()) | ||
} | ||
|
||
export function newTracked(dep: Dep): boolean { | ||
return hasBit(dep.newTracked, getTrackOpBit()) | ||
} | ||
|
||
export function setWasTracked(dep: Dep) { | ||
dep.wasTracked = setBit(dep.wasTracked, getTrackOpBit()) | ||
} | ||
|
||
export function setNewTracked(dep: Dep) { | ||
dep.newTracked = setBit(dep.newTracked, getTrackOpBit()) | ||
} | ||
|
||
export function resetTracked(dep: Dep) { | ||
const trackOpBit = getTrackOpBit() | ||
dep.wasTracked = clearBit(dep.wasTracked, trackOpBit) | ||
dep.newTracked = clearBit(dep.newTracked, trackOpBit) | ||
} | ||
|
||
function hasBit(value: number, bit: number): boolean { | ||
return (value & bit) > 0 | ||
} | ||
|
||
function setBit(value: number, bit: number): number { | ||
return value | bit | ||
} | ||
|
||
function clearBit(value: number, bit: number): number { | ||
return value & ~bit | ||
} |
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
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
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
Oops, something went wrong.