Commit 9341463
Resolver: Avoid intermediate allocations in matchSubpathFromMainFields, improve overall resolver perf ~5%
Summary:
`matchSubpathFromMainFields` is called on every resolution attempt (per source-extension via `redirectModulePath`, and per package entry point), and on every call it allocated a `.map()` array, a `.filter()` array, and an `Object.assign({}, ...spread)` - even in the overwhelmingly common case where the package declares no object-valued main field (no "browser" map) and the function returns `null`.
Instead, rebuild the merged replacement map with a single reverse loop over `mainFields` (preserving the prior "earlier mainFields win on key conflict" semantics) such that nothing is allocated (`null` is still returned) unless and until an object-valued field is actually found. The list of subpath variants is likewise only built in that rare matched case.
This improves the perf of `matchSubpathFromMainFields` by ~20% and ~5% on resolutions overall, based on Meta's product graph.
```
- **[Performance]**: Refactor to reduce allocs for a ~5% faster resolver
Reviewed By: huntie
Differential Revision: D107024445
fbshipit-source-id: cec2261dc588590eb12bb413a7bbb6fc6869401c1 parent 7529585 commit 9341463
1 file changed
Lines changed: 20 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
177 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
178 | 185 | | |
179 | | - | |
180 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
181 | 194 | | |
182 | | - | |
| 195 | + | |
183 | 196 | | |
184 | 197 | | |
185 | 198 | | |
186 | | - | |
187 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
188 | 202 | | |
189 | 203 | | |
190 | 204 | | |
191 | | - | |
192 | 205 | | |
193 | 206 | | |
194 | | - | |
195 | 207 | | |
196 | 208 | | |
197 | 209 | | |
| |||
0 commit comments