hft-core 0.1.3

LLM-driven high-frequency trading platform on Solana - PEV loop, Smart Order Routing, SignerContext keypair isolation, and AVM benchmarking via Rig (ARC)
Documentation
//! AVM vs EVM execution micro-benchmark.
//!
//! Runs 10 000 iterations of two simulated execution engines and reports
//! nanoseconds-per-operation and the resulting speedup ratio:
//!
//! | Engine | Simulation method | Typical result |
//! |---|---|---|
//! | AVM (Agave JIT) | `#[inline(always)]`, stack-only arithmetic | ~1–3 ns/op |
//! | EVM (bytecode)  | `#[inline(never)]`, heap allocation per call | ~10–30 ns/op |
//!
//! The heap allocation in `evm_execute_simulated` models the cost of
//! fetching and decoding EVM bytecode from memory on every iteration - a
//! realistic representation of interpreter dispatch overhead.
//!
//! > **Note:** This is a *synthetic* benchmark. Real AVM vs EVM numbers will
//! > differ depending on instruction mix, cache state, and hardware.

use std::time::Instant;

use tracing::info;

/// Run the AVM vs EVM benchmark and log the resulting speedup factor.
///
/// Iterates each simulated engine 10 000 times, computes the average
/// nanoseconds per operation for each, and logs the ratio as a structured
/// tracing event.
///
/// # Errors
///
/// Currently infallible; returns `Ok(())`.
pub fn run() -> anyhow::Result<()> {
    info!("[AVM] Starting AVM vs EVM execution benchmark");

    // AVM: JIT-compiled, zero heap allocation per iteration.
    let t0 = Instant::now();
    for _ in 0..10_000 {
        let _ = avm_execute_simulated();
    }
    let avm_ns = t0.elapsed().as_nanos() / 10_000;

    // EVM: bytecode interpretation with per-iteration heap allocation.
    let t1 = Instant::now();
    for _ in 0..10_000 {
        let _ = evm_execute_simulated();
    }
    let evm_ns = t1.elapsed().as_nanos() / 10_000;

    #[allow(clippy::cast_precision_loss)]
    // speedup ratio; precision loss beyond 2^52 ns (~52 days) is acceptable
    let speedup = evm_ns as f64 / avm_ns as f64;
    info!(
        avm_ns_per_op = avm_ns,
        evm_ns_per_op = evm_ns,
        speedup_factor = format!("{:.1}x", speedup),
        "[AVM] Benchmark complete - AVM is {:.1}x faster than EVM",
        speedup
    );

    Ok(())
}

/// Simulate one iteration of AVM JIT-compiled execution.
///
/// Marked `#[inline(always)]` and uses only stack-local arithmetic to model
/// the near-zero dispatch overhead of a JIT-compiled instruction sequence.
/// No heap allocations occur.
#[inline]
fn avm_execute_simulated() -> u64 {
    let mut acc = 0u64;
    for i in 0..100u64 {
        acc = acc.wrapping_add(i.wrapping_mul(7));
    }
    acc
}

/// Simulate one iteration of EVM bytecode interpretation.
///
/// Marked `#[inline(never)]` and allocates a `Vec` on every call to model the
/// overhead of fetching and decoding bytecode from memory on each invocation,
/// as a naive EVM interpreter would.
#[inline(never)]
fn evm_execute_simulated() -> u64 {
    let mut acc = 0u64;
    // Heap allocation simulates per-instruction bytecode fetch.
    let ops = vec![1u64, 2, 3, 4, 5];
    for op in &ops {
        for i in 0..20u64 {
            acc = acc.wrapping_add(op.wrapping_mul(i));
        }
    }
    acc
}