Commit 62cfe34
authored
perf(matrices): cache MatrixAccessor properties with cached_property (#716)
Convert the remaining `@property` methods on `MatrixAccessor` (`c`, `Q`, `sol`,
`dual`) to `@cached_property`. After #630, vlabels/clabels/A/b/sense/lb/ub are
materialised once in `__init__`, but these four properties still recomputed on
every access. Each `MatrixAccessor` is short-lived (rebuilt on every
`model.matrices` call), so cache invalidation is moot: the instance never sees
a mutated model.
Also fix a latent double-build in `expressions.BaseExpression._map_solution`:
`m.matrices.sol` followed by `m.matrices.vlabels` constructed two accessors
(each running `_build_vars`+`_build_cons`). Bind once to `M`.
Benchmark on a 360k-var / 1.2k-constraint model (Python 3.12, scipy 1.17,
numpy 2.4):
cold (first) warm (repeat)
master: 1.0 ms 1.1 ms (recomputed)
patch: 1.2 ms 0.4 us (~2500x on warm)
Repeat access happens in `solvers.Highs._build_solver_model` (M.c / M.Q both
read at lines 1278/1299 and again in Gurobi/Mosek paths at 1586-1587, 2860-2861)
and anywhere a caller assigns `M = model.matrices` then reads multiple times.1 parent d8d52b4 commit 62cfe34
2 files changed
Lines changed: 7 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
969 | 969 | | |
970 | 970 | | |
971 | 971 | | |
972 | | - | |
| 972 | + | |
| 973 | + | |
973 | 974 | | |
974 | 975 | | |
975 | 976 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | | - | |
| 100 | + | |
100 | 101 | | |
101 | 102 | | |
102 | 103 | | |
| |||
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
124 | | - | |
| 125 | + | |
125 | 126 | | |
126 | 127 | | |
127 | 128 | | |
| |||
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
133 | | - | |
| 134 | + | |
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
| |||
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
149 | | - | |
| 150 | + | |
150 | 151 | | |
151 | 152 | | |
152 | 153 | | |
| |||
0 commit comments