Repository navigation
fix(extract): push channel-walker children in one linear cursor pass - #3187
Conversation
The JS/TS channel walkers in extract_channels.c already use ts_nstack_push_children(), which enumerates children in a single O(n) TSTreeCursor pass and reverses the pushed slice to preserve source order. The remaining eight walkers (scan_string_consts_python plus the Python, Go, Java/Kotlin, C#, Ruby, Elixir, and Rust channel walks) still pushed children with reverse ts_node_child(node, i) loops, which is O(n^2) on wide nodes because ts_node_child() walks from child 0 on every call. Switch all eight walkers to ts_nstack_push_children(&stack, node), matching scan_string_consts_js and extract_channels_js, and add a wide-flat linearity regression test covering all seven languages. Signed-off-by: Guillaume Demay <7033942+GDemay@users.noreply.github.com>
|
Thank you, @GDemay! Moving the eight remaining walkers onto We also checked that it changes nothing in the graph. We indexed six real repositories with One change to the test before it can land. With that, it's ready to merge. Thanks again for another careful fix! |
Remove cbm_clock_gettime() measurement and the wall-clock scaling ratio assertion per review feedback, keeping the functional wide-flat fixtures for all seven languages (Python with constant resolution, Go, Java, C#, Ruby, Elixir, and Rust) to verify each walker reaches the trailing channel site in source order. Signed-off-by: Guillaume Demay <7033942+GDemay@users.noreply.github.com>
|
Reply authored with AI assistance (Antigravity) on behalf of @GDemay. Thanks for the thorough review and cross-repo edge check! Updated in
|
|
Merged in 9467326. Thank you, @GDemay, and for turning the test around so quickly! All eight walkers now take one cursor pass per node, the graph stays byte-identical on the six repositories we compared, and the wide-flat test still proves every language reaches the trailing channel site. Four solid fixes from you in a day, much appreciated! |
What does this PR do?
In
internal/cbm/extract_channels.c, the two JavaScript/TypeScript walkers (scan_string_consts_jsandextract_channels_js) already push children usingts_nstack_push_children(&stack, node), which enumerates children in a single O(n)TSTreeCursorpass and reverses the pushed slice to preserve source order.The remaining eight walkers in the same file —
scan_string_consts_python,extract_channels_python,extract_channels_go,extract_channels_java,extract_channels_csharp,extract_channels_ruby,extract_channels_elixir, andextract_channels_rust— still pushed children with:Because
ts_node_child(node, i)is O(i) in tree-sitter, wide AST nodes (such as generated files or comment-heavy modules) hit O(n²) traversal time incbm_extract_channels, which runs on every file of those languages during indexing.Changes
internal/cbm/extract_channels.c: replace all eight reversets_node_childloops withts_nstack_push_children(&stack, node)(+8/-32lines, identical pop order).tests/test_extraction.c: addextract_channels_wide_flat_is_linear, a scaling-ratio regression test exercisingcbm_extract_channels()across all seven languages (Python, Go, Java, C#, Ruby, Elixir, Rust) on 20x wide-flat fixtures while verifying the trailing channel site is still extracted (including Python constant resolution).Benchmarks
Measured
cbm_extract_channels()on pre-parsed files with 20,000 flat comment siblings followed by a channel site (Linux 6.18.65 x86_64, 2x Intel Xeon @ 2.30 GHz, GCC 13.3.0):-O2Before-O2After-O1Before-O1AfterValidation
extract_channels.c, ASan+UBSan):extract_channels_wide_flat_is_linearfails immediately on Python (2064us -> 650175us, >50x base5000us).extract_channels_wide_flat_is_linear: PASS (~4.6 s total across all 7 languages).extraction+memsuites: 662 passed, 0 failed.scripts/test.sh --contracts-only: all contract steps passed.clang-format --dry-run --Werror internal/cbm/extract_channels.cand--linescheck on added lines intests/test_extraction.c: clean.cppcheck --enable=warning,style,performance,portability(CI flags) oninternal/cbm/extract_channels.c: clean.clang-tidymemory-analyzer checks oninternal/cbm/extract_channels.c: clean.make -f Makefile.cbm lint-no-suppress lint-memory-core lint-vendored-scanners: passed.bash scripts/check-no-test-skips.shandbash scripts/check-dco.sh upstream/main..HEAD: passed.Checklist
git commit -s) — required, CI rejects unsigned commits (DCO, see CONTRIBUTING.md)extraction+memsuites under ASan+UBSan and--contracts-only)cppcheck,clang-format,clang-tidymemory checks,lint-no-suppress,lint-memory-core,lint-vendored-scanners)Authored with AI assistance (Antigravity) on behalf of @GDemay; the commit carries Guillaume's DCO sign-off.