Repository navigation
Conversation
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>
Other extractors are left for a separate, measured follow-up. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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>
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>
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>
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>
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>
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>
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>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🔵 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
710a3da2205b2afc4c5762281e9977655d5874da↑ 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_testcovers alloc, zeroed alloc, realloc grow/shrink and dealloc for 8 to 4096-byte alignments; its shrink-below-alignment case failed before 62f3db2; a new publicrust.ymljob 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.lockand 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_nmpath 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 viatree_sitter::set_allocator. Cargo builds keep the system allocator and do not build jemalloc.jemalloc comes from the BCR
jemallocmodule, built with the_rjem_prefix and without registering as the macOS default malloc zone, so it never replaces the process-widemalloc. Other C (including Ruby's external scanner) and Swift allocations are unchanged.Bazel details:
codeql_rust_binary(jemalloc = True)adds//misc/bazel/jemalloc, a smallGlobalAllocadapter (as intikv-jemallocator), and sets--cfg=codeql_jemalloc.cc_commonwith mangled allocator symbols, rules_rust's default shim would clash with#[global_allocator].nm, which jemalloc needs, so.bazelrcpoints 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:
Not covered, and not measured: the C/C++ build-mode-none driver, the Python parser helpers, the Rust autobuilder and dev tools.