Commit 38b4c63
authored
perf: internalize matched spec patterns during spec lookup (#14142)
This PR speeds up `mvcgen'` spec lookup by internalizing each matched
spec pattern into the `SymM` share table on first lookup, so its
instance arguments become pointer-equal to the program's and need not be
re-internalized on every later lookup.
`findSpecs` selects which spec applies by matching the program against
the candidate patterns held in the discrimination tree. Those patterns
are built at `@[spec]` registration and not internalized into the
`mvcgen'`-ambient `SymM` share table, so their instance arguments, the
`MonadStateOf`/`MonadReaderOf`/`MonadExceptOf` instances of
`get`/`set`/`read`/`throw` and the like, are structurally equal to the
program's but pointer-distinct, and the matcher re-internalizes them on
each match.
The first time a spec is tried, `findSpecs` internalizes its pattern and
stores it back in the database. The internalized pattern is structurally
unchanged, so it keys the discrimination tree at the same place and
replaces the old entry in place. Later lookups detect the internalized
pattern in O(1) with `Sym.isShared` (a membership query on the share
table) and skip the work. The updated database is threaded through the
`Scope`, which `solve` already carries.
`Sym.isShared` and `Pattern.shareCommon` are defined in the
spec-database module so the spec database and the backward-rule caches
share one definition.
Benchmark wall-clock for the `mvcgen'` step (`tests/bench/mvcgen/sym`,
single-threaded, min of 5):
| case | before | after |
| --- | --- | --- |
| ReaderState(1000) | 197 ms | 171 ms |
| AddSubCancelDeep(700) | 178 ms | 144 ms |
| GetThrowSet(600) | 126 ms | 119 ms |
| MatchIota(150) | 36 ms | 33 ms |
| AddSubCancel(1000) | 179 ms | 176 ms |1 parent 48ea259 commit 38b4c63
3 files changed
Lines changed: 40 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
294 | 294 | | |
295 | 295 | | |
296 | 296 | | |
297 | | - | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
298 | 304 | | |
299 | 305 | | |
300 | 306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
29 | 47 | | |
30 | 48 | | |
31 | 49 | | |
| |||
110 | 128 | | |
111 | 129 | | |
112 | 130 | | |
113 | | - | |
| 131 | + | |
114 | 132 | | |
115 | 133 | | |
116 | 134 | | |
117 | 135 | | |
118 | 136 | | |
119 | 137 | | |
120 | | - | |
| 138 | + | |
121 | 139 | | |
122 | 140 | | |
| 141 | + | |
123 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
124 | 153 | | |
125 | | - | |
126 | | - | |
| 154 | + | |
| 155 | + | |
127 | 156 | | |
128 | 157 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | 29 | | |
46 | 30 | | |
47 | 31 | | |
| |||
0 commit comments