Commit 61dcc1d
perf: Optimize oneOf validation and add schema/validator caching (#1197)
## Summary
Follow-up to #1196 targeting the remaining validation bottlenecks,
identified via cProfile on a 135MB Biacore T200 `.bme` file (917
measurements, 35M data points).
- **Fast `oneOf` for typed arrays** — `tDimensionArray` and
`tMeasureArray` are `oneOf` over 3 typed array refs
(number/boolean/string). The default `oneOf` validator validates against
the first matching branch, then re-validates against ALL remaining
branches to confirm uniqueness. We short-circuit by inspecting the first
element's type to select the correct branch directly. **(~6s saved)**
- **Fast `oneOf` for array-vs-object** — `oneOf[tDimensionArray,
tFunction]` patterns: if the instance is a list, it must be the array
branch (tFunction is an object). Skip the redundant validation. **(917
calls saved)**
- **Schema + validator caching** — Cache schema JSON, `RefResolver`, and
`FastDraft202012Validator` instances per schema path to avoid redundant
disk I/O and object construction on repeated validations. **(~1.7s saved
on first call)**
- **Eliminate redundant `fields()` call** — `unstructure()` called
`fields()` twice per dataclass (once for iteration, once to build a set
for custom_information_document check). Reuse the tuple.
### Benchmark (135MB .bme file)
| Phase | Before #1196 | After #1196 | After this PR |
|---|---|---|---|
| unstructure | 13s | 0.2s | 0.2s |
| validate | 147s | 12.8s | **5.0s** |
| **Total** | **~176s** | **~38s** | **~25s** |
## Test plan
- [x] Full test suite passes (1,119 tests)
- [x] Lint + mypy clean
- [x] Verified oneOf fast validator correctly handles:
number/string/boolean arrays, nullable arrays, mixed-type rejection,
non-array oneOf fallback, array-vs-object disambiguation
- [x] Benchmarked end-to-end against 135MB `.bme` file
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent fe88e2b commit 61dcc1d
2 files changed
Lines changed: 129 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
186 | | - | |
| 186 | + | |
| 187 | + | |
187 | 188 | | |
188 | 189 | | |
189 | 190 | | |
| |||
194 | 195 | | |
195 | 196 | | |
196 | 197 | | |
197 | | - | |
198 | 198 | | |
199 | 199 | | |
200 | | - | |
| 200 | + | |
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
113 | 198 | | |
114 | 199 | | |
115 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
116 | 204 | | |
117 | 205 | | |
118 | 206 | | |
| |||
157 | 245 | | |
158 | 246 | | |
159 | 247 | | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
160 | 251 | | |
161 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
162 | 257 | | |
163 | 258 | | |
164 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
165 | 285 | | |
166 | 286 | | |
167 | 287 | | |
168 | 288 | | |
169 | 289 | | |
170 | 290 | | |
171 | | - | |
172 | 291 | | |
173 | 292 | | |
174 | 293 | | |
175 | 294 | | |
176 | 295 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
| 296 | + | |
186 | 297 | | |
| 298 | + | |
| 299 | + | |
187 | 300 | | |
188 | 301 | | |
189 | 302 | | |
0 commit comments