use crate::core::AbsoluteRow;
use crate::execution::matching::graph::ordered_transitions::OrderedTransitionTable;
use crate::nfa::{AnchorEnvironment, AutomatonStateId};
use std::sync::Arc;
use crate::core::vm::MAX_REGISTERS;
use crate::execution::matching::row_loop::types::PackedNavigationLayout;
pub(crate) struct LaneScanProgram {
pub(crate) transitions: Arc<OrderedTransitionTable>,
pub(crate) closure_accepting: Arc<[Option<AutomatonStateId>]>,
pub(crate) boundary_anchors: LaneBoundaryAnchors,
pub(crate) cycle_positions: Box<[u64]>,
pub(crate) cycles: Box<[Box<[u32]>]>,
pub(crate) capacity: LaneCapacity,
pub(crate) optimistic_history: bool,
pub(super) initial_registers: [u64; MAX_REGISTERS],
pub(super) register_words: usize,
pub(super) navigation_layout: Option<Arc<PackedNavigationLayout>>,
pub(super) navigation_words: usize,
pub(crate) payload_symbols: u64,
pub(super) idempotent_last_symbols: u64,
pub(super) direct_linear: bool,
pub(crate) suffix_reuse: Option<DirectSuffixReuse>,
pub(super) state_meta: Box<[DirectStateMeta]>,
pub(super) direct_edges: Box<[DirectEdge]>,
pub(crate) direct_payload_work: u8,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct DirectSuffixReuse {
pub(crate) body_mask: u64,
pub(super) start: u32,
pub(super) len: u16,
}
#[derive(Clone, Copy)]
pub(super) struct DirectStateMeta {
pub(super) symbols: u64,
pub(super) start: u32,
pub(super) len: u16,
pub(super) max_work: u16,
}
impl DirectStateMeta {
#[inline]
pub(super) fn can_step_static_packed(
self,
payload: u64,
cached: u64,
last_only: u64,
) -> bool {
payload & self.symbols & !cached & !last_only == 0
}
}
#[derive(Clone, Copy)]
pub(super) struct DirectEdge {
pub(super) next_state: u16,
pub(super) symbol: u8,
pub(super) flags: u8,
pub(super) work: u8,
}
impl DirectEdge {
pub(super) const PAYLOAD: u8 = 1;
pub(super) const ACCEPTING: u8 = 2;
pub(super) const CONTINUING: u8 = 4;
pub(super) const AFTER_ACCEPT: u8 = 8;
pub(super) const IDEMPOTENT_LAST_ONLY: u8 = 16;
}
impl std::fmt::Debug for LaneScanProgram {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LaneScanProgram")
.field("states", &self.closure_accepting.len())
.field("edges", &self.transitions.edge_count())
.field("cycles", &self.cycles.len())
.field("navigation_words", &self.navigation_words)
.field("capacity", &self.capacity)
.finish()
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub(crate) struct SymbolFacts {
pub(crate) first: AbsoluteRow,
pub(crate) last: AbsoluteRow,
pub(crate) count: u64,
}
#[derive(Clone, Copy)]
pub(crate) struct LaneState {
pub(crate) state_id: AutomatonStateId,
pub(crate) history_runs: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LaneRejection {
Graph,
Evaluator,
State,
Path,
Capacity,
}
impl From<crate::execution::matching::graph::reachability::ReachabilityRejection>
for LaneRejection
{
fn from(
reason: crate::execution::matching::graph::reachability::ReachabilityRejection,
) -> Self {
match reason {
crate::execution::matching::graph::reachability::ReachabilityRejection::Graph => Self::Graph,
crate::execution::matching::graph::reachability::ReachabilityRejection::Nullable => Self::Path,
crate::execution::matching::graph::reachability::ReachabilityRejection::Capacity => Self::Capacity,
}
}
}
impl std::fmt::Display for LaneRejection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let reason = match self {
Self::Graph => "graph",
Self::Evaluator => "evaluator",
Self::State => "state",
Self::Path => "path",
Self::Capacity => "capacity",
};
f.write_str(reason)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct LaneCapacity {
pub(crate) live_lanes: usize,
pub(crate) path_runs_per_lane: usize,
pub(crate) encoded_program_bytes: usize,
pub(crate) source_payload_floor_bytes: usize,
pub(crate) scan_resource_bytes: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LaneBoundaryAnchors {
None,
Boundary { end_anchored: bool },
}
impl LaneBoundaryAnchors {
#[inline]
pub(crate) const fn requires_partition_start(self) -> bool {
matches!(self, Self::Boundary { .. })
}
#[inline]
pub(crate) const fn requires_partition_end(self) -> bool {
matches!(self, Self::Boundary { end_anchored: true })
}
#[inline]
pub(crate) fn dispatch_environment(
self,
state_id: AutomatonStateId,
start_state: AutomatonStateId,
) -> Option<AnchorEnvironment> {
match self {
Self::None => None,
Self::Boundary { .. } => Some(if state_id == start_state {
AnchorEnvironment::PhysicalStart
} else {
AnchorEnvironment::PhysicalInterior
}),
}
}
#[inline]
pub(crate) const fn accept_environment(self) -> Option<AnchorEnvironment> {
match self {
Self::None => None,
Self::Boundary { end_anchored: true } => Some(AnchorEnvironment::VirtualEnd),
Self::Boundary {
end_anchored: false,
} => Some(AnchorEnvironment::PhysicalInterior),
}
}
}
impl LaneScanProgram {
#[inline]
pub(crate) const fn requires_partition_start(&self) -> bool {
self.boundary_anchors.requires_partition_start()
}
#[inline]
pub(crate) const fn requires_partition_end(&self) -> bool {
self.boundary_anchors.requires_partition_end()
}
#[inline]
pub(crate) fn dispatch_environment(
&self,
state_id: AutomatonStateId,
) -> Option<AnchorEnvironment> {
self.boundary_anchors
.dispatch_environment(state_id, self.transitions.start_state())
}
#[inline]
pub(crate) const fn accept_environment(&self) -> Option<AnchorEnvironment> {
self.boundary_anchors.accept_environment()
}
pub(crate) fn encoded_size_bytes(
transitions: &OrderedTransitionTable,
boundary_anchors: LaneBoundaryAnchors,
closure_accepting_len: usize,
cycle_positions: &[u64],
cycles: &[Box<[u32]>],
) -> Option<usize> {
let transitions = transitions.retained_size_bytes().ok()?;
let mut total = size_of::<Self>()
.checked_add(2 * size_of::<usize>())
.and_then(|n| n.checked_add(transitions))
.and_then(|n| n.checked_add(2 * size_of::<usize>()))
.and_then(|n| n.checked_add(size_of_val(cycle_positions)))
.and_then(|n| n.checked_add(size_of_val(cycles)));
if boundary_anchors.accept_environment().is_some() {
total = total.and_then(|n| {
closure_accepting_len
.checked_mul(size_of::<Option<AutomatonStateId>>())
.and_then(|bytes| {
n.checked_add(2 * size_of::<usize>())?.checked_add(bytes)
})
});
}
for cycle in cycles {
total = total.and_then(|n| n.checked_add(size_of_val(cycle.as_ref())));
}
total
}
pub(crate) fn cycle_position(
&self,
state_id: AutomatonStateId,
) -> Option<(u32, u32)> {
let packed = *self.cycle_positions.get(state_id.index())?;
(packed != u64::MAX).then_some(((packed >> 32) as u32, packed as u32))
}
}