nativeview-codegen 0.1.0-rc.1

Multi-language ABI, policy, flat-op codec, and hybrid Direct-FFI code generator for Native View Engine.
Documentation
//! Heap-allocation ceilings for host ABI codegen, counted by a thread-local `CountingAlloc`.
//!
//! The view-models borrow the `nativeview-abi` descriptors and `TableDesc`s directly (no owned
//! `HybridFileSpec` bridge), and every template renders into one pre-sized buffer. What remains are
//! the small per-name `String`s of the spelling helpers: codegen is a cold build step, so those
//! are not worth a zero-allocation rewrite.
//!
//! Ceilings scale with the generated output (a minimum of output bytes per allocation, ~20% under
//! the measured density: C header ~30, tables ~37, all files ~32), so the ABI growing (new slots,
//! fields, value sets) never trips them, while reintroducing an owned intermediate spec or
//! per-field validation churn (more allocations for the same output) does. Absolute counts used
//! to fail on every ABI addition. Byte totals count every buffer growth (ceiling 5x the output).


use nativeview_codegen::{generate_all_abi_files, generate_c_abi_header, generate_table_files};
use nativeview_tables::tables::CORE_TABLE_DESCS;

use nativeview_test_support::{AllocStats, measure_allocs};
nativeview_test_support::install_counting_allocator!();

/// At least `min_bytes_per_alloc` output bytes per heap allocation.
fn assert_density(what: &str, allocs: usize, out: usize, min_bytes_per_alloc: usize) {
    assert!(
        allocs * min_bytes_per_alloc <= out,
        "{what} took {allocs} allocations for {out} output bytes \
         ({} bytes/allocation, minimum {min_bytes_per_alloc})",
        out / allocs.max(1)
    );
}

#[test]
fn c_abi_header_allocation_ceiling() {
    let _ = generate_c_abi_header(); // warm one-time statics
    let (header, AllocStats { allocs, bytes, .. }) = measure_allocs(generate_c_abi_header);
    let header = header.expect("C header");
    eprintln!(
        "generate_c_abi_header: {allocs} allocations, {bytes} bytes for {} output bytes",
        header.len()
    );
    assert_density("C header", allocs, header.len(), 23);
    assert!(
        bytes <= 5 * header.len(),
        "C header allocated {bytes} bytes for {} output bytes",
        header.len()
    );
}

#[test]
fn table_files_allocation_ceiling() {
    let (files, AllocStats { allocs, bytes, .. }) =
        measure_allocs(|| generate_table_files(CORE_TABLE_DESCS));
    let out: usize = files.expect("tables").iter().map(|f| f.content.len()).sum();
    eprintln!(
        "generate_table_files(17 core): {allocs} allocations, {bytes} bytes for {out} output bytes"
    );
    // 17 core tables mirrored across five host languages (deduplicated value sets).
    assert_density("tables", allocs, out, 29);
    assert!(
        bytes <= 5 * out,
        "tables allocated {bytes} bytes for {out} output bytes"
    );
}

#[test]
fn every_host_file_allocation_ceiling() {
    let (files, AllocStats { allocs, bytes, .. }) =
        measure_allocs(|| generate_all_abi_files(CORE_TABLE_DESCS));
    let out: usize = files
        .expect("all files")
        .iter()
        .map(|f| f.content.len())
        .sum();
    eprintln!("generate_all_abi_files: {allocs} allocations, {bytes} bytes for {out} output bytes");
    assert_density("all files", allocs, out, 26);
    assert!(
        bytes <= 5 * out,
        "all files allocated {bytes} bytes for {out} output bytes"
    );
}