Skip to content

Commit 35ccfd3

Browse files
committed
Look for pending effects only in results that may have them
Holding a check's effects during variance cycles leaves results with effects still to replay, and a few keep them for long, for example when the only read that would replay them was discarded with a variance measurement. While any result had pending effects, every read of a cached result took the slow path that looks for them, so a single one slowed all the checking after it (seen in xstate). The keys of results given pending effects are now kept in a filter, and a read takes the slow path only for a key that may have them.
1 parent a58647e commit 35ccfd3

3 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,8 @@ type memoStore struct {
10821082
stateFilter stateKeyFilter // Memo keys that may have a memo state; see memoState
10831083
errorFilter errorKeyFilter // Memo keys that may have reports; see observeMemo
10841084
memoPendingCount int // Number of memoStates with unreplayed effects
1085+
pendingFilter errorKeyFilter // Memo keys that may have pending effects; see mayBePending
1086+
pendingStates []*memoState // States given pending effects since pendingFilter was built; see addPending
10851087
memoErrorStates int // Number of memoStates with reports; see setReported
10861088

10871089
mu sync.Mutex
@@ -21788,7 +21790,8 @@ func (c *Checker) getDeclaredPropertyOfResolvingType(t *Type, name string) *ast.
2178821790
func (c *Checker) getValueMemberOfResolvedObjectType(t *Type, name string, skipObjectFunctionPropertyAugment bool) (*ast.Symbol, bool) {
2178921791
// While memos are observed, reading the resolved members must be
2179021792
// observed too, which the full lookup does.
21791-
if c.observingMemos || t.flags != TypeFlagsObject || t.objectFlags&(ObjectFlagsMapped|ObjectFlagsMembersResolved) != ObjectFlagsMembersResolved ||
21793+
if t.flags != TypeFlagsObject || t.objectFlags&(ObjectFlagsMapped|ObjectFlagsMembersResolved) != ObjectFlagsMembersResolved ||
21794+
c.observingMemos && (!c.checking || c.mayBePending(memoKeyBits(memoKeyOf(t, memoStructuredMembers)))) ||
2179221795
t.symbol != nil && t.symbol.Flags()&ast.SymbolFlagsValueModule != 0 {
2179321796
return nil, false
2179421797
}

‎tsc/internal/checker/checker_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ var unmemoizedCacheFields = map[string]string{
762762
"memoStore.memoErrorStates": unmemoizedMemoState,
763763
"memoStore.stateFilter": unmemoizedMemoState,
764764
"memoStore.errorFilter": unmemoizedMemoState,
765+
"memoStore.pendingFilter": unmemoizedMemoState,
766+
"memoStore.pendingStates": unmemoizedMemoState,
765767
"memoStore.mu": unmemoizedMemoState,
766768
"memoStore.diagnostics": unmemoizedChecking,
767769
"memoStore.suggestionDiagnostics": unmemoizedChecking,

‎tsc/internal/checker/memo.go‎

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func (f *memoEffects) covers(deps []*memoEffects) bool {
191191
type memoState struct {
192192
pending *memoEffects // Effects not yet replayed by a check
193193
errors *[]*currentNodeError // Errors the computation reported at the current node, out of line since few have any; see setErrors
194+
bits uint64 // memoKeyBits of the result's key, for pendingFilter
194195
}
195196

196197
// memo is a cached result. Cached checker state must live in a memo so that
@@ -497,7 +498,7 @@ func (m *memoMap[K, V]) observe(c *Checker, k K) {
497498
if m.side.memoStates == nil && len(m.side.resolvingKeys) == 0 {
498499
return
499500
}
500-
if c.checking && c.memoPendingCount == 0 && !m.side.hasErrors {
501+
if c.checking && !m.side.hasErrors && (c.memoPendingCount == 0 || !c.mayBePending(memoKeyBits(m.entryKey(k)))) {
501502
// Only reports are replayed; see observeMemo.
502503
return
503504
}
@@ -617,7 +618,7 @@ func (m *memoMap[K, V]) begin(c *Checker, k K) {
617618
c.resolving.count++
618619
c.appendResolution(key, m, len(side.resolvingKeys)-1, int32(len(c.instantiationStack)))
619620
c.currentResolution().resolving = true
620-
if c.checking && c.memoPendingCount == 0 && c.memoTop|c.currentNodeResolution != 0 && !c.checkingRecords() {
621+
if c.checking && !c.mayBePending(memoKeyBits(key)) && c.memoTop|c.currentNodeResolution != 0 && !c.checkingRecords() {
621622
// beginCheckedMemo for the common case, without the calls. The memo
622623
// state of the result is looked up only if needed; see
623624
// resolutionState.
@@ -699,7 +700,7 @@ func (m *memoMap[K, V]) state(k K) *memoState {
699700
if s.memoStates == nil {
700701
s.memoStates = make(map[K]*memoState)
701702
}
702-
state = &memoState{}
703+
state = &memoState{bits: memoKeyBits(m.entryKey(k))}
703704
s.memoStates[k] = state
704705
if s.keyBits != nil {
705706
s.stateKeys.add(s.keyBits(k), s.memoStates, s.keyBits)
@@ -752,6 +753,47 @@ func (c *Checker) observing() bool {
752753
// starting to check.
753754
func (c *Checker) syncObserving() {
754755
c.observingMemos = !c.checking || c.memoPendingCount != 0
756+
if len(c.pendingStates) > 4*c.memoPendingCount+64 || c.memoPendingCount == 0 && len(c.pendingStates) != 0 {
757+
c.rebuildPendingFilter()
758+
}
759+
}
760+
761+
// addPending records that state has just been given pending effects.
762+
func (c *Checker) addPending(state *memoState) {
763+
i := state.bits >> (64 - errorFilterBits)
764+
c.pendingFilter[i/64] |= 1 << (i % 64)
765+
c.pendingStates = append(c.pendingStates, state)
766+
}
767+
768+
// mayBePending reports whether the result whose key has memoKeyBits bits may
769+
// have pending effects. A few results can keep pending effects for long, for
770+
// example when the only read replaying them was discarded with a variance
771+
// measurement, so a filter of those keys keeps every other read cheap.
772+
func (c *Checker) mayBePending(bits uint64) bool {
773+
if c.memoPendingCount == 0 {
774+
return false
775+
}
776+
i := bits >> (64 - errorFilterBits)
777+
return c.pendingFilter[i/64]&(1<<(i%64)) != 0
778+
}
779+
780+
//go:noinline
781+
func (c *Checker) rebuildPendingFilter() {
782+
clear(c.pendingFilter[:])
783+
states := c.pendingStates
784+
c.pendingStates = c.pendingStates[:0]
785+
if c.memoPendingCount != 0 {
786+
seen := make(map[*memoState]struct{}, c.memoPendingCount)
787+
for _, state := range states {
788+
if state.pending != nil {
789+
if _, ok := seen[state]; !ok {
790+
seen[state] = struct{}{}
791+
c.addPending(state)
792+
}
793+
}
794+
}
795+
}
796+
clear(states[len(c.pendingStates):])
755797
}
756798

757799
// readsWithoutMemo reports whether a memoized computation identified by key,
@@ -760,7 +802,7 @@ func (c *Checker) syncObserving() {
760802
// only reads, unless it re-enters itself in progress, with the result
761803
// published before it is complete.
762804
func (c *Checker) readsWithoutMemo(key memoKey) bool {
763-
if c.observing() {
805+
if !c.checking || c.mayBePending(memoKeyBits(key)) {
764806
return false
765807
}
766808
_, resolving := c.resolvingGet(key)
@@ -781,7 +823,7 @@ func (c *Checker) beginMemo(key memoKey) {
781823
if resolving {
782824
c.linkResolution()
783825
}
784-
if c.checking && c.memoPendingCount == 0 && c.memoTop|c.currentNodeResolution != 0 && !c.checkingRecords() {
826+
if c.checking && !c.mayBePending(memoKeyBits(key)) && c.memoTop|c.currentNodeResolution != 0 && !c.checkingRecords() {
785827
// beginCheckedMemo for the common case, without the calls. The memo
786828
// state of the result is looked up only if needed; see
787829
// resolutionState.
@@ -860,7 +902,7 @@ func (c *Checker) memoState(key memoKey) *memoState {
860902
if c.memoStates == nil {
861903
c.memoStates = make(map[memoKey]*memoState)
862904
}
863-
state = &memoState{}
905+
state = &memoState{bits: memoKeyBits(key)}
864906
c.memoStates[key] = state
865907
c.stateFilter.add(memoKeyBits(key), c.memoStates, memoKeyBits)
866908
}
@@ -991,6 +1033,7 @@ func (c *Checker) linkState(state *memoState, f *memoEffects, log bool) {
9911033
// Replaying f is replaying a set of deps holding only f.
9921034
state.pending = f
9931035
c.memoPendingCount++
1036+
c.addPending(state)
9941037
c.syncObserving()
9951038
if c.recordingFromCheck {
9961039
c.flushBuf = append(c.flushBuf, state)
@@ -1028,7 +1071,7 @@ func (c *Checker) observeMemo(key memoKey) {
10281071
if !c.memoActive() {
10291072
return
10301073
}
1031-
if c.checking && c.memoPendingCount == 0 {
1074+
if c.checking && !c.mayBePending(memoKeyBits(key)) {
10321075
// Only reports are replayed, which few results have, so a filter
10331076
// small enough to stay in cache rules out most.
10341077
if i := errorFilterIndex(key); c.errorFilter[i/64]&(1<<(i%64)) == 0 {
@@ -1295,6 +1338,7 @@ func (c *Checker) retainEffects(state *memoState, f *memoEffects) {
12951338
f.addDep(state.pending)
12961339
} else {
12971340
c.memoPendingCount++
1341+
c.addPending(state)
12981342
c.syncObserving()
12991343
if c.recordingFromCheck {
13001344
c.flushBuf = append(c.flushBuf, state)
@@ -2097,6 +2141,7 @@ func (c *Checker) logPendingDiscard(state *memoState) {
20972141
c.syncObserving()
20982142
case old != nil && state.pending == nil:
20992143
c.memoPendingCount++
2144+
c.addPending(state)
21002145
c.syncObserving()
21012146
}
21022147
state.pending = old

0 commit comments

Comments
 (0)