Performance Guide
Optimization strategies for Rod.
Performance Guide
To get the most out of Rod, you must minimize the number of times data crosses the language boundary.
1. Batching (The "Golden Rule")
If you have a list of items, never validate them in a loop. Use the specialized batch APIs to process the entire array in a single FFI call.
// Native Rust is already fast,
// loops are fine.
for item in items {
schema.validate(&wrap(item))?;
}// ✅ FAST: 1 Bridge Call
const mask = schema.checkBatch(largeArray);
// ❌ SLOW: 10,000 Bridge Calls
largeArray.map(item => schema.parse(item));# Batch processing for Python
# is under development.2. Use Discriminated Unions
Standard Unions are $O(N)$ because Rod must try every schema. Discriminated Unions are $O(1)$ because Rod jumps straight to the correct validator using a key.
// ✅ FAST
discriminated_union("type", vec![...])
// ❌ SLOW
union(vec![...])// ✅ FAST
rod.discriminatedUnion("type", [...])
// ❌ SLOW
rod.union([...])# ✅ FAST
rod.discriminated_union("type", [...])3. String Optimizations
Prefer specialized string helpers over general Regex. Rod uses optimized Rust string matching for these.
string().starts_with("http") // Fastrod.string().startsWith("http") // Fastrod.string().starts_with("http") // Fast