Commit aaa5a9a
authored
perf: optimize LineCache to reduce allocations (#2903)
Refactor LineCache internals to reduce memory allocations when reading
source context for stacktrace frames:
- Use Hash#fetch instead of ||= to avoid double hash lookup in getlines
- Remove valid_path?/getline indirection — inline bounds checking via
line_at helper
- Build pre/post context arrays directly with Array.new instead of
creating a single large array and slicing it
- Add set_frame_context method that sets frame attributes directly,
avoiding the intermediate [pre, context_line, post] array allocation
- Cache context results per (filename, lineno) since the same frames
repeat across exceptions — avoids recreating identical arrays
The public get_file_context API is preserved for custom LineCache
implementations.1 parent 949bc6b commit aaa5a9a
1 file changed
+18
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
| 16 | + | |
16 | 17 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
25 | | - | |
26 | | - | |
27 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
35 | 41 | | |
36 | 42 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | 43 | | |
47 | 44 | | |
0 commit comments