|
| 1 | +package checker |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/microsoft/TypeScript/tsc/internal/core" |
| 8 | +) |
| 9 | + |
| 10 | +// The inference walk memo. |
| 11 | +// |
| 12 | +// Some walks at the top of inferTypes are long and are repeated with the same inputs, for example |
| 13 | +// inferring from a schema constraint type to the return type of a schema combinator, once per |
| 14 | +// call to that combinator. When every inference info is still in its initial state, a walk's |
| 15 | +// inputs are just the source, the target, the priority, the contravariance and the type |
| 16 | +// parameters, and its outcome is the candidates, priority and topLevel it writes to each info. |
| 17 | +// |
| 18 | +// A walk's outcome is stored only when the walk computed nothing new and read no transient |
| 19 | +// state: it created no type, symbol or signature, instantiated nothing, added no diagnostic, |
| 20 | +// observed no in-progress resolution, signature resolution, flow loop or flow type cache entry, |
| 21 | +// and did not start inside an instantiation or variance computation. Such a walk read only |
| 22 | +// finished values besides its inputs, so the same inputs take the same path again and write |
| 23 | +// the same outcome. Walks shorter than inferMemoMinSteps are cheaper to repeat than to store. |
| 24 | + |
| 25 | +const inferMemoMinSteps = 16 |
| 26 | + |
| 27 | +type inferMemoOutcome struct { |
| 28 | + candidates []*Type |
| 29 | + contraCandidates []*Type |
| 30 | + priority InferencePriority |
| 31 | + topLevel bool |
| 32 | +} |
| 33 | + |
| 34 | +type inferMemo struct { |
| 35 | + entries map[string][]inferMemoOutcome |
| 36 | + key []byte |
| 37 | + steps uint32 |
| 38 | +} |
| 39 | + |
| 40 | +// The checker state a walk must leave alone to be stored. |
| 41 | +type inferMemoEffects struct { |
| 42 | + created uint32 |
| 43 | + instantiationCount uint32 |
| 44 | + totalInstantiationCount uint32 |
| 45 | + diagnostics uint32 |
| 46 | + transientReads uint32 |
| 47 | + instantiationCountReset uint32 |
| 48 | +} |
| 49 | + |
| 50 | +func (c *Checker) inferMemoEffects() inferMemoEffects { |
| 51 | + return inferMemoEffects{ |
| 52 | + created: c.TypeCount + c.SymbolCount + c.SignatureCount, |
| 53 | + instantiationCount: c.instantiationCount, |
| 54 | + totalInstantiationCount: c.TotalInstantiationCount, |
| 55 | + diagnostics: c.diagnosticAdds, |
| 56 | + transientReads: c.transientReads, |
| 57 | + instantiationCountReset: c.instantiationCountResets, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Records that the current computation observed state that is only valid for the moment, such |
| 62 | +// as an in-progress resolution. Results computed with such reads must not be memoized. |
| 63 | +func (c *Checker) noteTransientRead() { |
| 64 | + c.transientReads++ |
| 65 | +} |
| 66 | + |
| 67 | +func (c *Checker) inferFromTypesMemo(n *InferenceState, source *Type, target *Type) { |
| 68 | + m := &c.inferMemo |
| 69 | + // Relations made inside a variance computation report reliability to it. |
| 70 | + if len(c.varianceStack) != 0 || c.skipDirectInferenceNodes.Len() != 0 || |
| 71 | + !core.Every(n.inferences, isInitialInferenceInfo) { |
| 72 | + c.inferFromTypes(n, source, target) |
| 73 | + return |
| 74 | + } |
| 75 | + m.key = binary.LittleEndian.AppendUint32(m.key[:0], uint32(source.id)) |
| 76 | + m.key = binary.LittleEndian.AppendUint32(m.key, uint32(target.id)) |
| 77 | + m.key = binary.LittleEndian.AppendUint32(m.key, uint32(n.priority)) |
| 78 | + if n.contravariant { |
| 79 | + m.key = append(m.key, 1) |
| 80 | + } else { |
| 81 | + m.key = append(m.key, 0) |
| 82 | + } |
| 83 | + for _, info := range n.inferences { |
| 84 | + m.key = binary.LittleEndian.AppendUint32(m.key, uint32(info.typeParameter.id)) |
| 85 | + } |
| 86 | + if outcomes, ok := m.entries[string(m.key)]; ok { |
| 87 | + changed := false |
| 88 | + for i, info := range n.inferences { |
| 89 | + outcome := &outcomes[i] |
| 90 | + if outcome.candidates != nil || outcome.contraCandidates != nil || |
| 91 | + outcome.priority != info.priority || outcome.topLevel != info.topLevel { |
| 92 | + changed = true |
| 93 | + } |
| 94 | + info.candidates = outcome.candidates |
| 95 | + info.contraCandidates = outcome.contraCandidates |
| 96 | + info.priority = outcome.priority |
| 97 | + info.topLevel = outcome.topLevel |
| 98 | + } |
| 99 | + if changed { |
| 100 | + clearCachedInferences(n.inferences) |
| 101 | + } |
| 102 | + return |
| 103 | + } |
| 104 | + // The walk may infer into nested calls that reuse the key buffer. |
| 105 | + key := string(m.key) |
| 106 | + // Inside an instantiation the walk may take cached instantiations without counting them, |
| 107 | + // while the same walk elsewhere would make and count them. |
| 108 | + inInstantiation := len(c.activeMappers) != 0 |
| 109 | + before := c.inferMemoEffects() |
| 110 | + stepsBefore := m.steps |
| 111 | + c.inferFromTypes(n, source, target) |
| 112 | + if m.steps-stepsBefore < inferMemoMinSteps || inInstantiation || c.inferMemoEffects() != before { |
| 113 | + return |
| 114 | + } |
| 115 | + outcomes := make([]inferMemoOutcome, len(n.inferences)) |
| 116 | + for i, info := range n.inferences { |
| 117 | + outcomes[i] = inferMemoOutcome{ |
| 118 | + candidates: slices.Clip(slices.Clone(info.candidates)), |
| 119 | + contraCandidates: slices.Clip(slices.Clone(info.contraCandidates)), |
| 120 | + priority: info.priority, |
| 121 | + topLevel: info.topLevel, |
| 122 | + } |
| 123 | + } |
| 124 | + if m.entries == nil { |
| 125 | + m.entries = make(map[string][]inferMemoOutcome) |
| 126 | + } |
| 127 | + m.entries[key] = outcomes |
| 128 | +} |
| 129 | + |
| 130 | +func isInitialInferenceInfo(info *InferenceInfo) bool { |
| 131 | + return info.typeParameter != nil && info.candidates == nil && info.contraCandidates == nil && !info.isFixed && |
| 132 | + info.priority == InferencePriorityMaxValue && info.topLevel && info.impliedArity == -1 |
| 133 | +} |
0 commit comments