@@ -191,6 +191,7 @@ func (f *memoEffects) covers(deps []*memoEffects) bool {
191191type 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.
753754func (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.
762804func (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