Skip to content

Shared: Use jemalloc as global allocator in Rust-coded extractors - #23334

Open
redsun82 wants to merge 12 commits into
mainfrom
redsun82-congenial-guacamole
Open

redsun82 wants to merge 12 commits into
mainfrom
redsun82-congenial-guacamole

Conversation

@redsun82

@redsun82 redsun82 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 710a3da2205b2afc4c5762281e9977655d5874da

Dimension Assessment Rationale
Change confidence 7.5/10 ↑ Bazel opt builds of the Rust, Ruby and unified extractors pass on linux x86_64 at 96302b5 (dbg too at 1493072); at 1493072, native Windows (MSVC) Bazel build of Rust and Ruby passes.
↑ jemalloc 5.3.1 initialises in all three linux binaries (unified with narenas:1) and in the Windows Ruby binary; a 300-file Windows Ruby extraction succeeds (at 1493072).
↑ //misc/bazel/jemalloc:jemalloc_test covers alloc, zeroed alloc, realloc grow/shrink and dealloc for 8 to 4096-byte alignments; its shrink-below-alignment case failed before 62f3db2; a new public rust.yml job runs it.
↑ The C functions given to tree-sitter abort on null like tree-sitter's defaults (same size/count > 0 conditions); a child-process test forces each failure with an oversized request and fails if the check is removed. Not exercised under real memory pressure.
↑ Linux TRAP at 1493072 matches a system-allocator build: rails (Ruby, 3502 files), Alamofire (unified, 104), ripgrep source TRAP (Rust). Rust dependency-crate TRAP differs run to run with the system allocator too.
↑ Cargo builds have no jemalloc, even with --all-features; Cargo.lock and vendored crates are unchanged.
↓ Internal bundled-toolchain linux, macOS and Windows CI pending at this head; a Java integration job (62f3db2) and the public rust-ast-generator job (96302b5) failed on network downloads, not builds; the allocator shim passed internal linux CI at 625a554.
↓ macOS not built at this head (arm64 standalone xcrun_nm path verified by a spike at 4429ba9); linux arm64 not built or run.

In Bazel builds, uses jemalloc as #[global_allocator] in the Rust, Ruby and unified extractors, on all platforms. The Ruby extractor also routes tree-sitter's C allocations to it via tree_sitter::set_allocator. Cargo builds keep the system allocator and do not build jemalloc.

jemalloc comes from the BCR jemalloc module, built with the _rjem_ prefix and without registering as the macOS default malloc zone, so it never replaces the process-wide malloc. Other C (including Ruby's external scanner) and Swift allocations are unchanged.

Bazel details:

  • codeql_rust_binary(jemalloc = True) adds //misc/bazel/jemalloc, a small GlobalAlloc adapter (as in tikv-jemallocator), and sets --cfg=codeql_jemalloc.
  • It also swaps in an allocator shim. When linking through cc_common with mangled allocator symbols, rules_rust's default shim would clash with #[global_allocator].
  • The standalone macOS C++ toolchain declares no nm, which jemalloc needs, so .bazelrc points it at Xcode's.

The unified extractor sets narenas:1. With jemalloc's defaults its peak RSS was ~60% above the system allocator, also on macOS.

The QL extractor is built only by Cargo, so it keeps the system allocator.

Measured on linux x86_64, 8 threads, system allocator vs this PR:

Input user+sys CPU peak RSS
Rust: tokio (earlier head) 103 s → 83 s (user) 1.96 → 1.66 GB
Rust: ripgrep 108 s → 93 s 1.17 → 1.01 GB
Ruby: rails equal within noise 270 → 245 MB
Unified: Alamofire equal within noise 259 → 251 MB

Not covered, and not measured: the C/C++ build-mode-none driver, the Python parser helpers, the Rust autobuilder and dev tools.

Paolo Tranquilli and others added 2 commits December 2, 2026 14:09
Applies to the Rust, Ruby, QL and unified extractors on non-Windows targets.
In Bazel, jemalloc is built from the BCR `jemalloc` module instead of the
`tikv-jemalloc-sys` configure/make build script.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Matches the Cargo build, where only Rust allocations go through jemalloc.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This comment was marked as outdated.

Other extractors are left for a separate, measured follow-up.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@redsun82 redsun82 changed the title Shared: Use jemalloc as global allocator in Rust extractors Rust: Use jemalloc as global allocator in the extractor Dec 2, 2026
@redsun82
redsun82 requested an adaptive review from Copilot December 2, 2026 15:11

This comment was marked as outdated.

@github-actions github-actions Bot added the Rust Pull requests that update Rust code label Dec 2, 2026
When linking through `cc_common` with mangled allocator symbols, the default
allocator shim clashes with the `__rust_alloc*` symbols emitted for the
`#[global_allocator]`. Give the extractor its own shim providing only the
remaining symbol.

On macOS the C++ toolchain declares no `nm`, which jemalloc needs to generate
its private namespace header, so point jemalloc at Xcode's.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This comment was marked as outdated.

Also move the Bazel allocator shim wiring into a `global_allocator` option of
`codeql_rust_binary`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@redsun82 redsun82 changed the title Rust: Use jemalloc as global allocator in the extractor Shared: Use jemalloc as global allocator in Rust-coded extractors Dec 2, 2026
@redsun82
redsun82 requested an adaptive review from Copilot December 2, 2026 16:01

This comment was marked as outdated.

With default per-thread arenas, peak RSS on Alamofire was ~60% above the
system allocator. narenas:1 brings it below that with no measured CPU or
wall-time cost.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This comment was marked as outdated.

Calls tree_sitter::set_allocator with the prefixed jemalloc functions at
the start of main, so the parser's C allocations share the Rust
allocator. The Ruby external scanner keeps using libc for its own
create/destroy pairs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This comment was marked as outdated.

Replace tikv-jemallocator with a small Bazel-only adapter over the BCR
jemalloc, enabled via `--cfg=codeql_jemalloc`, so Cargo builds never build
or link jemalloc. QL is only built with Cargo and keeps the system allocator.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@redsun82
redsun82 requested an adaptive review from Copilot December 3, 2026 09:17
@redsun82
redsun82 marked this pull request as ready for review December 3, 2026 09:20
@redsun82
redsun82 requested review from a team as code owners December 3, 2026 09:20
@github-actions github-actions Bot removed the QL-for-QL label Dec 3, 2026
Shrinking a block below its alignment otherwise picks plain `realloc`,
which may return a pointer aligned only to the smaller size class.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This comment was marked as outdated.

tree-sitter's default allocators abort when out of memory, but custom
ones replace them outright.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Also match tree-sitter's `calloc` check, which only tests `count`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This comment was marked as outdated.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Replacing allocation across three extractors combines unsafe Rust/C memory contracts, tree-sitter callbacks, and nightly linker-shim assumptions that require substantive cross-platform review.

No open issues 🎉

1 resolved since last review

Review effort: Max (adaptive)

This branch has not been deployed

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

Labels

Ruby Rust Pull requests that update Rust code Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants