Skip to content

fix(extract): push channel-walker children in one linear cursor pass - #3187

Merged
DeusData merged 2 commits into
DeusData:mainfrom
GDemay:fix/extract-channels-linear-child-push
Dec 4, 2026
Merged

DeusData merged 2 commits into
DeusData:mainfrom
GDemay:fix/extract-channels-linear-child-push

Conversation

@GDemay

@GDemay GDemay commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

In internal/cbm/extract_channels.c, the two JavaScript/TypeScript walkers (scan_string_consts_js and extract_channels_js) already push children using ts_nstack_push_children(&stack, node), which enumerates children in a single O(n) TSTreeCursor pass 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, and extract_channels_rust — still pushed children with:

uint32_t count = ts_node_child_count(node);
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
    ts_nstack_push(&stack, ts_node_child(node, (uint32_t)i));
}

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 in cbm_extract_channels, which runs on every file of those languages during indexing.

Changes

  • internal/cbm/extract_channels.c: replace all eight reverse ts_node_child loops with ts_nstack_push_children(&stack, node) (+8/-32 lines, identical pop order).
  • tests/test_extraction.c: add extract_channels_wide_flat_is_linear, a scaling-ratio regression test exercising cbm_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):

Language -O2 Before -O2 After Speedup ASan+UBSan -O1 Before ASan+UBSan -O1 After
Python (2 walks) 3,571.63 ms 5.09 ms 702x 27,960.84 ms 34.80 ms
Elixir 3,837.38 ms 5.56 ms 690x 26,939.70 ms 29.48 ms
Ruby 1,284.46 ms 1.80 ms 714x 10,793.26 ms 15.51 ms
C# 1,288.02 ms 2.08 ms 619x 10,832.16 ms 17.45 ms
Java 1,261.38 ms 2.23 ms 566x 10,844.58 ms 18.73 ms
Go 1,297.22 ms 2.67 ms 486x 10,854.49 ms 22.73 ms
Rust 1,950.62 ms 5.36 ms 364x 13,844.29 ms 40.06 ms

Validation

  • Reproduce-first (unpatched extract_channels.c, ASan+UBSan): extract_channels_wide_flat_is_linear fails immediately on Python (2064us -> 650175us, >50x base 5000us).
  • Patched (ASan+UBSan, GCC 13.3.0):
    • extract_channels_wide_flat_is_linear: PASS (~4.6 s total across all 7 languages).
    • Full extraction + mem suites: 662 passed, 0 failed.
  • Static & lint checks:
    • scripts/test.sh --contracts-only: all contract steps passed.
    • clang-format --dry-run --Werror internal/cbm/extract_channels.c and --lines check on added lines in tests/test_extraction.c: clean.
    • cppcheck --enable=warning,style,performance,portability (CI flags) on internal/cbm/extract_channels.c: clean.
    • clang-tidy memory-analyzer checks on internal/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.sh and bash scripts/check-dco.sh upstream/main..HEAD: passed.

Checklist

  • Every commit is signed off (git commit -s) — required, CI rejects unsigned commits (DCO, see CONTRIBUTING.md)
  • Tests pass locally (extraction + mem suites under ASan+UBSan and --contracts-only)
  • Lint passes (cppcheck, clang-format, clang-tidy memory checks, lint-no-suppress, lint-memory-core, lint-vendored-scanners)
  • New behavior is covered by a test (reproduce-first for bug fixes)

Authored with AI assistance (Antigravity) on behalf of @GDemay; the commit carries Guillaume's DCO sign-off.

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>
@GDemay
GDemay requested a review from DeusData as a code owner December 4, 2026 18:21
@DeusData

DeusData commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Thank you, @GDemay! Moving the eight remaining walkers onto ts_nstack_push_children is the right fix: one cursor pass per node instead of ts_node_child(node, i), which is O(i), with the same pop order the JS walkers already rely on. Your numbers show why it matters for wide generated files.

We also checked that it changes nothing in the graph. We indexed six real repositories with main and with this branch, one for each walker language we have a checkout for (etcd for Go, celery for Python, vagrant for Ruby, cargo for Rust, eladmin for Java, jellyfin for C#). Every edge was identical: about 517,000 edges compared by source, target, type, strategy and confidence, including the channel edges on etcd and eladmin.

One change to the test before it can land. extract_channels_wide_flat_is_linear fails when the large case takes more than 50x the small case's wall-clock time. This project has a firm rule that no test verdict depends on timing: shared CI runners and the sanitizer builds vary enough that a time ratio eventually flips, and a red caused by a busy runner is not a finding. (Our complexity guard, tests/test_complexity.c, gates on counter ratios for the same reason.) Could you keep the functional part, building the wide-flat fixtures for all seven languages and asserting that the trailing channel site is still extracted (including the Python constant resolution), but drop the timing comparison and the cbm_clock_gettime measurement from the test? The timing table in the description is the right place for the speed-up. The wide fixtures still prove that the walker reaches the last sibling in source order.

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>
@GDemay

GDemay commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Reply authored with AI assistance (Antigravity) on behalf of @GDemay.

Thanks for the thorough review and cross-repo edge check! Updated in 0944a434:

  • Dropped cbm_clock_gettime() and the wall-clock scaling ratio check, renaming the test to extract_channels_wide_flat_reaches_trailing_site.
  • Kept the wide-flat functional assertions for all seven languages (Python with constant resolution plus 3,000 flat siblings; Go, Java, C#, Ruby, Elixir, and Rust with 10,000 flat siblings) confirming each walker reaches and extracts the trailing channel site in source order.
  • Re-verified locally under ASan+UBSan (extraction + mem: 662 passed, 0 failed), scripts/test.sh --contracts-only, clang-format, and DCO.

@DeusData
DeusData merged commit 9467326 into DeusData:main Dec 4, 2026
26 checks passed
@DeusData

DeusData commented Dec 4, 2026

Copy link
Copy Markdown
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants