Performance Guide
RustCall.jl provides multiple features to optimize performance when calling Rust code from Julia. This guide explains best practices and optimization tips for improving performance.
Table of Contents
- Compilation Caching
- LLVM Optimization (removed)
- Function Call Optimization
- Memory Management
- Benchmark Results
- Performance Tuning Tips
Compilation Caching
RustCall.jl automatically caches compiled Rust libraries. This eliminates the need to recompile the same code and significantly reduces startup time.
How Caching Works
- Cache key:
RustCall.artifact_keyof aRustCall.ArtifactId— the single identity function of the package (src/artifacts/artifact_id.jl). The record names everything that can change the produced binary: the expanded source, generic type parameters in declaration order, target triple, codegen options, the#[cfg]snapshot, the dependency set (a localpath =dependency contributes its content, not its location), crate features, the tracked build environment, the toolchain fingerprint, and the identity of therustc/cargothat actually runs. Fields are netstring-framed, so no two different requests can concatenate to the same bytes. - Cache location: a Scratch.jl space,
<depot>/scratchspaces/<RustCall UUID>/cache-v\$(CACHE_FORMAT_VERSION)/— writable by construction and accounted for byPkg.gc(). RustCall writes nothing under~/.julia/compiled/, which is Julia's own precompile directory (issue #252).<depot>is the first writable entry ofDEPOT_PATH, so a read-only first depot is not fatal, andRUSTCALL_CACHE_DIRoverrides the location outright.CACHE_FORMAT_VERSIONnames the on-disk layout; bumping it namespaces a new space rather than serving or deleting the old one, andRustCall.sweep_stale_cache_formats()(called byclear_cacheandcleanup_old_cache) removes oldercache-v<n>siblings best effort. - Cost: every input byte is read and hashed on every key computation — file contents are never memoized, because a
(mtime, size)stamp can alias distinct contents and the cost of being wrong is running machine code built from source that no longer exists. Only the resolved Cargo dependency graph is cached (that is the expensive part: acargo treeprocess spawn), and it is invalidated by a content change in any manifest that decides the graph — every crate in it, plus the workspace root each crate belongs to. - Truncation: keys are never truncated.
RustCall.artifact_short_idis the only truncation in the design and exists solely for names a human reads — library names, temporary Cargo project directories, log lines. - Automatic verification: cached libraries carry a SHA-256 checksum, checked before loading.
What is not in the key
Two inputs are tracked best effort and are documented limits, not proofs:
- Build environment.
RustCall.artifact_build_envcaptures a documented allowlist (RUSTFLAGS,CARGO_PROFILE_*,PYO3_*,CC,PKG_CONFIG_PATH, …; never anything that looks like a credential). A build script may read any variable it likes, and the only exhaustive answer is Cargo's own fingerprint. ExtendARTIFACT_BUILD_ENV_*insrc/artifacts/artifact_id.jl— the one place — if you need more. - Files outside a package directory. A
#[path = "../../elsewhere.rs"]module or aninclude_str!above the crate root is compiled in but does not change the path-dependency digest.
A change in either means "stale" and forces a rebuild; no change does not by itself license reuse.
A missing toolchain is an error
The compiler in the key is the compiler that runs: versions come from RustToolChain.rustc() / cargo(), the very commands RustCall invokes. A toolchain that cannot be identified raises a RustError on any path about to compile, rather than caching everything under the string "unknown".
Pinned, lockfile-driven dependency builds
A // cargo-deps: block names version ranges; what Cargo resolves them to is a separate fact, and it is the fact that decides the binary. RustCall therefore persists one Cargo.lock per dependency set and builds against it (issue #256):
- Where.
RustCall.lockfile_path(deps)— orlockfile_path(source)for a block's source text — is<cache dir>/lockfiles/<key>.lock, where the key isartifact_key(cargo_lockfile_id(deps)): the declared dependency set and nothing else (no toolchain), so the same set on any machine looks in the same place. The generated project's package is named from the set (RustCall.cargo_block_package(deps)), so one lockfile fits every block declaring it, and no fixed name is reserved that your own crate might use. A stored file that does not name that package — a hand-edited file, or one written under an older naming scheme — is not this set's resolution and is resolved afresh rather than replayed. - First build. With no persisted lockfile,
cargo generate-lockfileresolves the set once and the result is stored. Every later build — of this block or any other with the same dependencies — copies the file in and runscargo build --locked, so Cargo builds exactly the pinned graph or fails; it never re-resolves behind the cache key. - Identity. The lockfile's content is part of the block's
ArtifactId(cargo-lock). A changed resolution is a different artifact; a cache hit can never answer for another graph. - Sharing and refreshing. Copy or commit the file to reproduce a build on another machine; delete it to resolve afresh.
clear_cacheleaves lockfiles alone — they are inputs of a build, not outputs — andRustCall.clear_lockfiles()is the one operation that discards them all. - One resolution wins. Two processes (or machines sharing the store) that both find it empty publish through an exclusive-create claim file beside the entry: the first to claim publishes, the other waits and replays the published file, so both build one graph. A claim is never expired by age; if a build ever reports that another process holds the claim and nothing was published, and no other process is resolving that set, a previous one died holding it — delete the named
.claimfile (or runclear_lockfiles()). - Offline.
RUSTCALL_OFFLINE=1adds--offlineto every Cargo invocation. With a warm registry cache the pinned build succeeds without the network; with a cold one Cargo fails at once with its own message (surfaced as aCargoBuildError), rather than hanging on a download.
@rust_crate wrapper crates depend on your crate by path and are built as their own Cargo root; a PyO3 wrapper carries your crate's Cargo.lock and [patch] table (see PyO3 crates). Persisting their resolution is not covered here.
Cache Management
using RustCall
# Check cache size
size = RustCall.get_cache_size()
println("Cache size: $(size / 1024 / 1024) MB")
# List cached libraries
libraries = RustCall.list_cached_libraries()
println("Cached libraries: $(length(libraries))")
# Cleanup old cache (older than 30 days)
RustCall.cleanup_old_cache(30)
# Clear cache completely
RustCall.clear_cache()
# Also remove the tree the pre-#252 layout left in `~/.julia/compiled/`. Off by
# default: that directory is Julia's own precompile directory for RustCall, so
# RustCall only ever deletes entries it can prove it wrote, and never the
# directory itself.
RustCall.clear_cache(sweep_legacy = true)Cache Best Practices
- During Development: Keep cache enabled to reduce recompilation time
- Production: Warm up cache beforehand to avoid first-run delays
- CI/CD: Save and restore cache to reduce build time
LLVM Optimization (removed)
@rust_llvm and the LLVM IR integration path — compile_rust_to_llvm_ir, load_llvm_ir, compile_and_register_rust_function, OptimizationConfig, optimize_module! and friends — were deprecated in 0.2.0 and removed in 0.3.0 (#265). RustCall no longer depends on LLVM.jl. Use @rust.
The path was an experiment inspired by Cxx.jl: load the LLVM IR that rustc emits into Julia's LLVM and optimize across the language boundary. It was removed for two reasons, quoting the issue:
- The call path was equivalent to
@rust.@rust_llvmcalled the Rust function through a function pointer with a plainccall; no Rust IR was ever inlined into Julia code, so there was no performance difference. - rustc and Julia do not share an LLVM version. rustc follows LLVM releases every six weeks while Julia pins a major version per release (Julia 1.12 ships LLVM 18, rustc 1.98 emits LLVM 22 IR). The textual IR format is not forward compatible, so newer rustc output could not be parsed reliably by Julia's LLVM — the path already stripped attributes it did not know by regex before parsing, and there is no way to make rustc emit an older IR format.
Optimization of the Rust code itself belongs to rustc (-C opt-level, see RustCall.RustCompiler).
Function Call Optimization
One call mechanism
Every @rust call is a ccall through a function pointer resolved from the library's own handle (one snapshot per call, see the project guide). There is no second, faster call path: the former @rust_llvm used the very same ccall and was removed in 0.3.0 (see above).
# Standard call (recommended)
result = @rust add(Int32(10), Int32(20))::Int32What a call costs
Measured by benchmark/benchmarks_dispatch.jl, against a raw ccall through a pointer resolved once by hand — the floor, since that is what every path eventually does (macOS, Julia 1.12, two-instruction Rust body, so what is left above the floor is dispatch and nothing else):
| call path | before #253 | now | × raw ccall |
|---|---|---|---|
raw ccall (the floor) | 5.2 ns | 5.4 ns | 1.0 |
a #[julia] function's generated wrapper | 9891 ns | 10.0 ns | 1.9 |
@rust f(a, b)::Int32 | 8940 ns | 11.9 ns | 2.2 |
@rust f(a, b) (no annotation) | 9945 ns | 360 ns | 68 |
a @rust_crate module's wrapper | 1029 ns | 21.8 ns | 3.3 |
(the @rust_crate row was measured in a later run, against a 6.5 ns floor on the same machine; its wrapper also went from 14 allocations per call to none.)
and across threads, as calls per second against the same path's one-task figure:
| tasks | raw ccall | generated wrapper, before | now | @rust_crate, before | now |
|---|---|---|---|---|---|
| 1 | 1.00× | 1.00× | 1.00× | 1.00× | 1.00× |
| 2 | 2.06× | 0.94× | 2.24× | 0.87× | 1.99× |
| 4 | 4.01× | 0.85× | 5.18× | 0.70× | 3.89× |
A call used to re-resolve everything it needed, every time: walk the calling module's blocks, take REGISTRY_LOCK, rebuild the symbol strings, look up the handle, the pointer, the panic channel and the return ABI, then compute the ccall signature from the argument types. That was ~9.9 µs and about a hundred allocations in front of a 5 ns call, and because it all happened under one global lock, adding threads made it slower rather than faster.
Since #253 a call site keeps the snapshot it resolved and reuses it while RustCall.ARTIFACT_EPOCH — a counter bumped by every write to RustCall's state — says nothing has changed. The fast path is an atomic load, an integer comparison, and the ccall. This does not weaken the generation rule of #277: what is kept is one whole snapshot, taken under one lock in the one place allowed to take one, and it is dropped the moment the epoch moves, so a call can still never mix an old pointer with a new channel.
A @rust_crate wrapper does the same thing with a cache it can name: a generated crate module is also written out as Julia source by write_bindings_to_file, so its call sites cannot be handed a spliced object. Each declares a RustCall.CrateTargetCache beside its wrapper instead. Nothing about this is visible in the bindings you call — but it is visible in a file you regenerate: the Bindings format: marker at the top of a generated file became 11 with this change (it was 13 by v0.6.6; since #489 it is the release's MAJOR.MINOR).
Annotate the return type, or use #[julia]
The one path that is still far from the floor is @rust f(a, b) with no return-type annotation, and it is inherent rather than unfinished: the return type is read out of the snapshot at run time, so the ccall behind it is reached by a dynamic dispatch and its result cannot be inferred in the caller.
Both ways out are cheap:
result = @rust add(Int32(10), Int32(20))::Int32 # 2.2× a raw ccallor mark the Rust function #[julia] and call the wrapper RustCall generates for it, which knows its own signature at definition time:
rust"""
#[julia]
pub fn add(a: i32, b: i32) -> i32 { a + b }
"""
result = add(Int32(10), Int32(20)) # 1.9× a raw ccallThis is the same advice as before #253 — it is just no longer "slightly slower", it is thirty times slower, and now measured.
No registration step
rust"""...""" blocks compile once and are cached, and the manifest registers every function's symbol and return type when the library is loaded, so there is nothing to pre-register. (compile_and_register_rust_function, the registration step of the removed LLVM path, is gone.) Prefer @rust with explicit argument and return types.
Memory Management
Efficient Use of Ownership Types
Ownership types (RustBox, RustRc, RustArc, RustVec) prevent memory leaks when used appropriately:
# Temporary allocations are automatically cleaned up
box = RustCall.RustBox(Int32(42))
# Automatically dropped after use
# Explicit drop (when early release is needed)
RustCall.drop!(box)Efficient Use of RustVec
RustVec is a type for manipulating Rust's Vec<T> from Julia. Best practices when handling large amounts of data:
# Create RustVec from Julia array
julia_vec = Int32[1, 2, 3, 4, 5]
rust_vec = RustCall.create_rust_vec(julia_vec)
# Efficient bulk copy (recommended)
result = Vector{Int32}(undef, length(rust_vec))
RustCall.copy_to_julia!(rust_vec, result)
# Or use to_julia_vector
result = RustCall.to_julia_vector(rust_vec)
# Element-by-element access (not recommended for large data)
for i in 1:length(rust_vec)
value = rust_vec[i] # FFI call occurs
end
# Explicitly drop after use
RustCall.drop!(rust_vec)RustVec vs Julia Array Selection
| Scenario | Recommendation |
|---|---|
| Computation within Julia | Julia arrays |
| Input to Rust functions | RustVec |
| Output from Rust functions | RustVec → Convert to Julia array |
| Temporary storage of large data | Julia arrays (managed by GC) |
| Data manipulation on Rust side | RustVec |
Avoiding Memory Leaks
# Pattern 1: Use try-finally
box = RustCall.RustBox(Int32(42))
try
# Use
value = box.ptr
finally
RustCall.drop!(box) # Ensure cleanup
end
# Pattern 2: Leverage local scope
function compute()
box = RustCall.RustBox(Int32(42))
# Use
return result
# box is automatically dropped
endBenchmark Results
Basic Operations
The following benchmarks were run on Julia 1.12, Rust 1.92.0, macOS.
| Operation | Julia Native | @rust |
|---|---|---|
| i32 addition | 1.0x | 1.2x |
| i64 addition | 1.0x | 1.2x |
| f64 addition | 1.0x | 1.3x |
| i32 multiplication | 1.0x | 1.2x |
| f64 multiplication | 1.0x | 1.3x |
Complex Computations
| Computation | Julia Native | @rust |
|---|---|---|
| Fibonacci (n=30) | 1.0x | 1.1x |
| Sum Range (1..1000) | 1.0x | 1.2x |
Ownership Type Operations
| Operation | Average Time | Notes |
|---|---|---|
| RustBox create+drop | ~170 ns | Single value allocation/release |
| RustRc create+drop | ~180 ns | With reference counting |
| RustRc clone+drop | ~180 ns | Clone operation |
| RustArc create+drop | ~190 ns | Atomic reference counting |
| RustArc clone+drop | ~200 ns | Thread-safe |
RustVec Operations
| Operation | Average Time | Notes |
|---|---|---|
| RustVec(1000 elements) create | ~1 μs | Conversion from Julia array |
| RustVec copytojulia!(1000 elements) | ~500 ns | Efficient bulk copy |
| RustVec element access | ~50 ns/element | Includes FFI call |
| RustVec push! | ~100 ns | When no reallocation occurs |
Note: These results may vary by environment. Actual performance can vary significantly depending on hardware, OS, and Julia/Rust versions.
Running Benchmarks
# Extractor + rustc compile-time benchmark (cold and warm)
RUSTCALL_EXTRACT=/path/to/rustcall-extract \
julia --project=benchmark benchmark/benchmarks_compile.jl
# Basic benchmarks
julia --project=benchmark benchmark/benchmarks.jl
# Ownership type benchmarks
julia --threads=4 --project=benchmark benchmark/benchmarks_ownership.jl
# Array operation benchmarks
julia --project=benchmark benchmark/benchmarks_arrays.jl
# Generics benchmarks
julia --project=benchmark benchmark/benchmarks_generics.jlInline compilation benchmark (#271)
benchmark/benchmarks_compile.jl measures the extractor-based inline pipeline with one Rust cdylib per sample. Cold samples use a new source identity, so they include extraction and rustc with no compiled-artifact cache hit. Warm samples reuse one source after unloading its image, so they exercise the disk cache and the memoized expansion without measuring an FFI call.
The first recorded run was on macOS (Darwin x86_64, Julia 1.12.7, rustc 1.98.0):
| Platform | Cold: extract + rustc | Warm: cache + memoized expansion |
|---|---|---|
| macOS x86_64 | 406.56 ms | 1.63 ms |
CI measurements from commit f6e84bd on 2026-09-09 (Julia 1.12.7, rustc 1.98.1; 3 cold samples and 5 warm samples):
| Platform | Cold: extract + rustc | Warm: cache + memoized expansion |
|---|---|---|
| Linux x86_64 | 140.06 ms | 35.08 ms |
| macOS aarch64 | 264.79 ms | 3.62 ms |
| Windows x86_64 | 256.59 ms | 1.18 ms |
These are runner-specific measurements, not controlled comparisons between operating systems. The script prints the platform, Julia version, rustc version, and medians so subsequent runs can be compared with their environment recorded.
Performance Tuning Tips
1. Reducing Compilation Time
- Leverage cache: Don't recompile the same code
- Adjust optimization level: Level 1-2 during development, Level 3 in production
- Disable debug info:
emit_debug_info=false
compiler = RustCall.RustCompiler(
optimization_level=2, # 2 is sufficient during development
emit_debug_info=false
)
RustCall.set_default_compiler(compiler)2. Improving Runtime Performance
- Explicit types: Reduce type inference overhead
- Batch processing: Combine multiple calls
# Inefficient: Type inference every time in loop
for i in 1:1000
result = @rust add(i, i+1) # Type inference runs every time
end
# Efficient: Explicit types
for i in 1:1000
result = @rust add(Int32(i), Int32(i+1))::Int32
end3. Optimizing Memory Usage
- Appropriate use of ownership types: Drop immediately when no longer needed
- Appropriate choice of Rc/Arc: Use
Rcfor single-threaded,Arcfor multi-threaded - Cache cleanup: Regularly delete old cache
4. Parallel Processing Optimization
using Base.Threads
# Use Arc to share data between threads
shared_data = RustCall.RustArc(Int32(0))
# Work on multiple threads
@threads for i in 1:1000
local_arc = RustCall.clone(shared_data)
# Work
RustCall.drop!(local_arc)
end5. Profiling
Use Julia's profiling tools to identify bottlenecks:
using Profile
# Start profiling
Profile.clear()
@profile for i in 1:1000
@rust add(Int32(i), Int32(i+1))
end
# Display results
Profile.print()Troubleshooting
When Performance is Lower Than Expected
- Check cache: Verify cache is working correctly
- Check optimization level: Verify optimization level is set appropriately
- Explicit types: Reduce type inference overhead
- Profiling: Identify bottlenecks
When Memory Usage is High
- Check ownership types: Verify they are being dropped appropriately
- Cache cleanup: Delete old cache
- Rc/Arc usage: Avoid unnecessary clones
Summary
To optimize RustCall.jl performance:
- ✅ Leverage cache: Reduce compilation time
- ✅ Adjust optimization level: Select optimization level according to use case
- ✅ Explicit types: Reduce type inference overhead
- ✅ Memory management: Use ownership types appropriately
- ✅ Profiling: Identify and optimize bottlenecks
By following these best practices, you can maximize the performance of applications using RustCall.jl.