#![allow(clippy::useless_conversion)]
#![allow(unsafe_op_in_unsafe_fn)]
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
#[pyfunction]
pub fn bench_rust_chunking(py: Python<'_>, data: &[u8]) -> PyResult<(f64, String, usize)> {
py.allow_threads(|| {
let chunker = crate::layer2::chunk_pruning::ChunkerProcessPCard::default();
let t0 = std::time::Instant::now();
let (manifest, _store) = chunker.chunk_bytes(data).map_err(PyRuntimeError::new_err)?;
let qref = manifest.qualified_ref().map_err(PyRuntimeError::new_err)?;
Ok((t0.elapsed().as_secs_f64() * 1000.0, qref, manifest.chunks.len()))
})
}
#[pyfunction]
pub fn bench_rust_lattice_40p_39t(py: Python<'_>) -> PyResult<(f64, bool, usize)> {
py.allow_threads(|| {
use crate::cpn::mcard_token::{PlaceMCard, TransitionMCard};
use crate::cpn::place_type_lattice::lattice_core::PlaceTypeLattice;
let places: Vec<PlaceMCard> = (0..40).map(|i| PlaceMCard::new(format!("p{i}"))).collect();
let transitions: Vec<TransitionMCard> = (0..39)
.map(|i| {
TransitionMCard::create(
&format!("t{i}"),
vec![format!("p{i}")],
vec![format!("p{}", i + 1)],
"any",
"any",
None,
"id",
None,
None,
)
.map_err(PyRuntimeError::new_err)
})
.collect::<PyResult<_>>()?;
let t0 = std::time::Instant::now();
let lat = PlaceTypeLattice::from_net(&places, &transitions, None);
let ok = lat.verify_lattice_laws();
Ok((t0.elapsed().as_secs_f64() * 1000.0, ok, 40))
})
}
#[pyfunction]
pub fn bench_rust_handles_sqlite(py: Python<'_>) -> PyResult<(f64, String, usize)> {
py.allow_threads(|| {
let fs = crate::mcard_fs::MCardFileSystem::new_in_memory()
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
let t0 = std::time::Instant::now();
let mut last = String::new();
for i in 0..250 {
let rec = fs
.write_raw_bytes(&format!("bench/h/{}.bin", i % 25), format!("val-{i}").as_bytes(), Some("blake3"))
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
last = rec.hash;
}
let err_count = if last.is_empty() { 1 } else { 0 };
Ok((t0.elapsed().as_secs_f64() * 1000.0, last, err_count))
})
}
#[pyfunction]
pub fn bench_rust_sync(py: Python<'_>) -> PyResult<(f64, String, usize)> {
py.allow_threads(|| {
use std::collections::{BTreeMap, BTreeSet};
use crate::layer0::mcard_record::MCardRecord;
use crate::layer2::sheaf_cohomology::{
compute_sheaf_cohomology_dims, CellularSheafCover, SheafEdgeRestriction,
SheafTriangleRestriction, SheafVertexStalk,
};
use crate::layer2::sync::codec::frame_record_page;
use crate::layer2::sync::open_petri_net::{OpenPetriNet, OpenPlace, OpenTransition};
use crate::layer2::sync::open_petri_pullback::{
compose_marked_open_petri_nets, BoundaryGlueSpec, CospanLegs,
};
let mut records: Vec<MCardRecord> = (0..120usize)
.map(|idx| {
let size = if idx < 80 { 256 } else if idx < 110 { 4096 } else { 16384 };
let payload: Vec<u8> = (0..size).map(|b| ((idx * 19 + b * 7 + 11) & 0xff) as u8).collect();
let hex = blake3::hash(&payload).to_hex().to_string();
MCardRecord {
hash: format!("blake3:{hex}"),
content: payload,
g_time: format!("blake3:0|1760000{idx:06}|node-bench"),
}
})
.collect();
records.sort_by(|a, b| (&a.g_time, &a.hash).cmp(&(&b.g_time, &b.hash)));
let i4 = vec![vec![1, 0, 0, 0], vec![0, 1, 0, 0], vec![0, 0, 1, 0], vec![0, 0, 0, 1]];
let vertices = (0..15).map(|i| SheafVertexStalk { id: format!("v{i}"), dimension: 4 }).collect();
let mut edges: Vec<SheafEdgeRestriction> = (0..15)
.map(|i| SheafEdgeRestriction {
id: format!("er{i}"),
source: format!("v{i}"),
target: format!("v{}", (i + 1) % 15),
dimension: 4,
source_map: i4.clone(),
target_map: i4.clone(),
})
.collect();
edges.extend((0..15).map(|i| SheafEdgeRestriction {
id: format!("ec{i}"),
source: format!("v{i}"),
target: format!("v{}", (i + 2) % 15),
dimension: 4,
source_map: i4.clone(),
target_map: i4.clone(),
}));
let triangles = (0..15)
.map(|i| SheafTriangleRestriction {
id: format!("t{i}"),
vertices: [format!("v{i}"), format!("v{}", (i + 1) % 15), format!("v{}", (i + 2) % 15)],
dimension: 4,
edge_ab: format!("er{i}"),
edge_bc: format!("er{}", (i + 1) % 15),
edge_ac: format!("ec{i}"),
map_ab: i4.clone(),
map_bc: i4.clone(),
map_ac: i4.clone(),
})
.collect();
let cover = CellularSheafCover { name: "strip_15v_30e_15t_d4".into(), vertices, edges, triangles };
let net1 = OpenPetriNet {
name: "left".into(),
places: vec![
OpenPlace { id: "p_in".into(), initial_tokens: 1, label: None },
OpenPlace { id: "p_mid".into(), initial_tokens: 1, label: None },
],
transitions: vec![OpenTransition {
id: "t1".into(),
inputs: vec!["p_in".into()],
outputs: vec!["p_mid".into()],
read_arcs: None,
}],
inputs: vec!["p_in".into()],
outputs: vec!["p_mid".into()],
};
let net2 = OpenPetriNet {
name: "right".into(),
places: vec![
OpenPlace { id: "q_mid".into(), initial_tokens: 1, label: None },
OpenPlace { id: "q_out".into(), initial_tokens: 0, label: None },
],
transitions: vec![OpenTransition {
id: "t2".into(),
inputs: vec!["q_mid".into()],
outputs: vec!["q_out".into()],
read_arcs: None,
}],
inputs: vec!["q_mid".into()],
outputs: vec!["q_out".into()],
};
let glue = BoundaryGlueSpec::Cospan(CospanLegs {
left_leg: BTreeMap::from([("boundary".into(), "p_mid".into())]),
right_leg: BTreeMap::from([("boundary".into(), "q_mid".into())]),
});
let known = BTreeSet::new();
let t0 = std::time::Instant::now();
let (mut cur, mut total_rec, mut total_b) = (None, 0usize, 0usize);
loop {
let page = frame_record_page(&records, &known, cur.as_ref(), 40, 262144);
total_rec += page.records.len();
total_b += page.total_bytes;
if !page.has_more || page.next_cursor.is_none() {
break;
}
cur = page.next_cursor;
}
let comp = compose_marked_open_petri_nets(&net1, &net2, Some(&glue), false)
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
let dims = compute_sheaf_cohomology_dims(&cover).map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
Ok((
t0.elapsed().as_secs_f64() * 1000.0,
format!("{}:{}:{}:{}", total_rec, comp.composite.places.len(), dims.dim_h0, dims.dim_h1),
total_b,
))
})
}