Here’s the short story: in Rust you typically get “Numba‐like” performance out of the box by AOT-compiling your numeric kernels. If you still need runtime code generation (a true JIT) you have a few solid options:
- Rust + Cranelift (the engine behind Wasmtime)
- Rust + LLVM (via inkwell or llvm-sys)
- Tiny JIT crates (e.g. jit or dynasm-rs)
Below is a quick overview:
| Crate / Tool | What it is | Pros | Cons |
|---|
| cranelift_jit | High-performance JIT compiler library from Bytecode Alliance | • Very fast warm-up• Pure Rust | • Lower LLVM-level control |
| inkwell | Safe Rust bindings to LLVM | • Full LLVM feature set | • Heavier dependency |
| llvm-sys | Raw LLVM FFI bindings | • 1:1 LLVM API | • Unsafe, verbose |
| dynasm-rs / jit | Lightweight assembler macros + minimal JIT runtime | • Tiny• Easy to embed | • DIY codegen |
1. Cranelift JIT Example
Cranelift gives you a nice, mostly Rust-native way to JIT small routines. Here’s a toy example that JITs a simple fn(i64,i64)->i64 { a + 3*b }:
use cranelift::prelude::*;
use cranelift_jit::{JITBuilder, JITModule};
fn main() {
// 1) configure a JIT module
let mut jit_builder = JITBuilder::new(cranelift_module::default_libcall_names());
let mut module = JITModule::new(jit_builder);
// 2) signature: fn(i64,i64)->i64
let sig = module.make_signature();
sig.params.push(AbiParam::new(types::I64));
sig.params.push(AbiParam::new(types::I64));
sig.returns.push(AbiParam::new(types::I64));
// 3) declare a function
let func_id = module.declare_function("add3b", Linkage::Export, &sig)
.unwrap();
// 4) define the function body
{
let mut ctx = module.make_context();
ctx.func.signature = sig;
let mut builder_ctx = FunctionBuilderContext::new();
let mut fb = FunctionBuilder::new(&mut ctx.func, &mut builder_ctx);
let block = fb.create_block();
fb.append_block_params_for_function_params(block);
fb.switch_to_block(block);
fb.seal_block(block);
let a = fb.block_params(block)[0];
let b = fb.block_params(block)[1];
let three = fb.ins().iconst(types::I64, 3);
let t = fb.ins().imul(b, three);
let sum = fb.ins().iadd(a, t);
fb.ins().return_(&[sum]);
fb.finalize();
module.define_function(func_id, &mut ctx).unwrap();
}
// 5) finalize and get a callable pointer
module.clear_context(&mut module.make_context());
module.finalize_definitions();
let code = module.get_finalized_function(func_id);
// 6) cast to a Fn pointer and call
let func = unsafe { std::mem::transmute::<_, fn(i64,i64)->i64>(code) };
println!("2 + 3*7 = {}", func(2, 7)); // prints 23
}
2. LLVM via Inkwell
If you want exactly the LLVM toolchain that Numba sits atop, Inkwell gives you pretty ergonomic Rust bindings:
# Cargo.toml
[dependencies]
inkwell = "0.1"
Then you can build IR, create an ExecutionEngine, JIT, and execute it. It’s more verbose than Cranelift, but you get the full LLVM optimizer suite.
3. “Mini-JIT” Crates
– dynasm-rs: write machine code with assembly-like macros;
– jit: super-minimal “compile and exec” support.
These are great if you only need a handful of op-codes at runtime.
Do you really need a JIT?
Rust’s standard workflow—compile once with cargo build --release—often wins on raw speed and portability. If your goal is to iterate on numeric kernels interactively (Numba-style), consider:
• Embedding Python + Numba via PyO3, calling back into Rust for heavy loops.
• Compiling Rust to WebAssembly and using Wasmtime/Wasmer for rapid reloads.
Let me know which path sounds best for your use case (pure Rust, LLVM JIT, WebAssembly JIT, Python hybrid, …), and I can point you to more detailed recipes! 😊