Commit 337d64b
Precompute prefix list in Versioner::Path
`Versioner::Path#before` built `[mount_path, Grape::Router.normalize_path(prefix)]`
on every request and iterated it, filtering nil/empty/'/' entries with a guard
clause on each element. Both `mount_path` and `prefix` are fixed at mount time,
so this work is redundant.
Memoize a pre-filtered `@prefixes` array in `initialize` (the middleware dup in
`Middleware::Base#call` shallow-copies ivars, so the frozen array is shared
across request dups for free). The per-request loop simplifies to
`if path_info.start_with?(path)`.
current 1.09 M i/s, 80KB / 2k objects
variant A (memoize normalize) 1.34 M i/s, 80KB / 2k objects
variant B (memoize list) 2.61 M i/s, 40KB / 1k objects (2.38x faster, 2x fewer)
Shipping variant B.
No behavior change; all 2,236 specs pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 66aadad commit 337d64b
2 files changed
Lines changed: 8 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
20 | 25 | | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | | - | |
25 | | - | |
| 29 | + | |
| 30 | + | |
26 | 31 | | |
27 | 32 | | |
28 | 33 | | |
| |||
0 commit comments