From ff293a56e6ae4ddac9cbcf0da45a3a03b07b386b Mon Sep 17 00:00:00 2001 From: Skgland Date: Sat, 18 Apr 2026 01:29:28 +0200 Subject: [PATCH] fix large enum variant size difference warning of PermVarAllocation by wrapping BranchNumber in an Arc. PermVarAllocation::Done had size 208 and is now down to 32. A Box rather than an Arc would be smaller, but it looks like BranchNumber/BranchDesignator are clones a bunch so I expect it to be beneficial to reduce allocations both of the Box itself as well as its content. --- src/debray_allocator.rs | 19 +++++++++-------- src/forms.rs | 3 ++- src/iterators.rs | 7 ++++--- src/machine/disjuncts.rs | 45 ++++++++++++++++++++++------------------ src/variable_records.rs | 3 ++- 5 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/debray_allocator.rs b/src/debray_allocator.rs index 6988420f..d6cd50bd 100644 --- a/src/debray_allocator.rs +++ b/src/debray_allocator.rs @@ -15,6 +15,7 @@ use indexmap::IndexMap; use std::cell::Cell; use std::collections::VecDeque; use std::ops::{Deref, DerefMut}; +use std::sync::Arc; pub type BranchHits = IndexMap; // key: var_num, value: branch arm occurrences. @@ -25,12 +26,12 @@ pub struct BranchOccurrences { pub deep_safety: BitSet, pub num_branches: usize, pub current_branch_idx: usize, - pub current_branch_num: BranchNumber, + pub current_branch_num: Arc, pub subsumed_hits: SubsumedBranchHits, } impl BranchOccurrences { - fn new(current_branch_num: BranchNumber, num_branches: usize) -> Self { + fn new(current_branch_num: Arc, num_branches: usize) -> Self { Self { hits: BranchHits::with_hasher(FxBuildHasher::default()), shallow_safety: BitSet::default(), @@ -98,7 +99,7 @@ impl BranchStack { } } - pub(crate) fn add_branch_stack(&mut self, branch_num: BranchNumber, num_branches: usize) { + pub(crate) fn add_branch_stack(&mut self, branch_num: Arc, num_branches: usize) { self.push(BranchOccurrences::new(branch_num, num_branches)); } @@ -112,7 +113,7 @@ impl BranchStack { } #[inline] - pub(crate) fn incr_current_branch(&mut self, branch_num: BranchNumber) { + pub(crate) fn incr_current_branch(&mut self, branch_num: Arc) { let branch_occurrences = self.last_mut().unwrap(); branch_occurrences.current_branch_idx += 1; branch_occurrences.current_branch_num = branch_num; @@ -201,7 +202,7 @@ impl DebrayAllocator { }, ); - let branch_designator = self.branch_stack.current_branch_designator(); + let branch_designator = Arc::new(self.branch_stack.current_branch_designator()); let (deep_safety, shallow_safety) = match self.branch_stack.last_mut() { Some(latest_branch) => { @@ -506,7 +507,7 @@ impl DebrayAllocator { } pub(crate) fn mark_safe_var_unconditionally(&mut self, var_num: usize) { - let branch_designator = self.branch_stack.current_branch_designator(); + let branch_designator = Arc::new(self.branch_stack.current_branch_designator()); match &mut self.var_data.records[var_num].allocation { VarAlloc::Perm( @@ -530,7 +531,7 @@ impl DebrayAllocator { } fn mark_safe_var(&mut self, var_num: usize, lvl: Level, term_loc: GenContext) { - let branch_designator = self.branch_stack.current_branch_designator(); + let branch_designator = Arc::new(self.branch_stack.current_branch_designator()); match &mut self.var_data.records[var_num].allocation { VarAlloc::Perm( @@ -574,7 +575,7 @@ impl DebrayAllocator { r: RegType, arg_c: usize, ) -> Instruction { - let branch_designator = self.branch_stack.current_branch_designator(); + let branch_designator = Arc::new(self.branch_stack.current_branch_designator()); match &mut self.var_data.records[var_num].allocation { VarAlloc::Perm( @@ -610,7 +611,7 @@ impl DebrayAllocator { var_num: usize, r: RegType, ) -> Instruction { - let branch_designator = self.branch_stack.current_branch_designator(); + let branch_designator = Arc::new(self.branch_stack.current_branch_designator()); match &mut self.var_data.records[var_num].allocation { VarAlloc::Perm( diff --git a/src/forms.rs b/src/forms.rs index 310228e4..9f9d00a3 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -25,6 +25,7 @@ use std::fmt; use std::hash::{Hash, Hasher}; use std::ops::{AddAssign, Deref, DerefMut}; use std::path::PathBuf; +use std::sync::Arc; pub type PredicateKey = (Atom, usize); // name, arity. @@ -194,7 +195,7 @@ impl BranchNumber { #[derive(Debug)] pub enum ChunkedTerms { Branch { - branch_nums: Vec, + branch_nums: Vec>, arms: Vec>, }, Chunk { diff --git a/src/iterators.rs b/src/iterators.rs index 0aa7bd38..d457740a 100644 --- a/src/iterators.rs +++ b/src/iterators.rs @@ -7,6 +7,7 @@ use std::cell::Cell; use std::collections::VecDeque; use std::iter::*; use std::rc::Rc; +use std::sync::Arc; use std::vec::Vec; #[allow(clippy::borrowed_box)] @@ -320,7 +321,7 @@ pub(crate) fn breadth_first_iter( enum ClauseIteratorState<'a> { RemainingChunks(&'a VecDeque, usize), RemainingBranches( - &'a Vec, + &'a Vec>, &'a Vec>, usize, ), @@ -329,11 +330,11 @@ enum ClauseIteratorState<'a> { #[derive(Debug, Clone)] pub(crate) enum ClauseItem<'a> { FirstBranch { - branch_num: &'a BranchNumber, + branch_num: &'a Arc, num_branches: usize, }, NextBranch { - branch_num: &'a BranchNumber, + branch_num: &'a Arc, }, BranchEnd { depth: usize, diff --git a/src/machine/disjuncts.rs b/src/machine/disjuncts.rs index 09eec4a1..65698e36 100644 --- a/src/machine/disjuncts.rs +++ b/src/machine/disjuncts.rs @@ -15,6 +15,7 @@ use std::cell::Cell; use std::collections::VecDeque; use std::hash::Hash; use std::ops::{Deref, DerefMut}; +use std::sync::Arc; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct VarInfo { @@ -34,12 +35,12 @@ pub struct ChunkInfo { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct BranchInfo { - branch_num: BranchNumber, + branch_num: Arc, chunks: Vec, } impl BranchInfo { - fn new(branch_num: BranchNumber) -> Self { + fn new(branch_num: Arc) -> Self { Self { branch_num, chunks: vec![], @@ -68,7 +69,7 @@ impl DerefMut for BranchMap { } } -type RootSet = IndexSet; +type RootSet = IndexSet>; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ClassifyInfo { @@ -92,14 +93,14 @@ enum TraversalState { Term(Term), OverrideGlobalCutVar(usize), ResetGlobalCutVarOverride(Option), - AddBranchNum(BranchNumber), // set current_branch_num, add it to the root set - RepBranchNum(BranchNumber), // replace current_branch_num and the latest in the root set + AddBranchNum(Arc), // set current_branch_num, add it to the root set + RepBranchNum(Arc), // replace current_branch_num and the latest in the root set } #[derive(Debug)] pub struct VariableClassifier { call_policy: CallPolicy, - current_branch_num: BranchNumber, + current_branch_num: Arc, current_chunk_num: usize, current_chunk_type: ChunkType, branch_map: BranchMap, @@ -156,22 +157,26 @@ pub type ClassifyFactResult = (Term, VarData); pub type ClassifyRuleResult = (Term, ChunkedTermVec, VarData); fn merge_branch_seq(branches: impl Iterator) -> BranchInfo { - let mut branch_info = BranchInfo::new(BranchNumber::default()); + let mut branch_info = BranchInfo::new(Arc::new(BranchNumber::default())); for mut branch in branches { branch_info.branch_num = branch.branch_num; branch_info.chunks.append(&mut branch.chunks); } - branch_info.branch_num.delta = branch_info.branch_num.delta * Integer::from(2); - branch_info.branch_num.branch_num -= &branch_info.branch_num.delta; + let new_delta = branch_info.branch_num.delta.clone() * Integer::from(2); + + branch_info.branch_num = Arc::new(BranchNumber { + branch_num: branch_info.branch_num.branch_num.clone() - &new_delta, + delta: new_delta, + }); branch_info } fn flatten_into_disjunct( build_stack: &mut ChunkedTermVec, - branch_num: BranchNumber, + branch_num: Arc, preceding_len: usize, ) { let branch_vec = build_stack.drain(preceding_len + 1..).collect(); @@ -188,7 +193,7 @@ impl VariableClassifier { pub fn new(call_policy: CallPolicy) -> Self { Self { call_policy, - current_branch_num: BranchNumber::default(), + current_branch_num: Arc::new(BranchNumber::default()), current_chunk_num: 0, current_chunk_type: ChunkType::Head, branch_map: BranchMap(BranchMapInt::new()), @@ -542,7 +547,7 @@ impl VariableClassifier { let tail = terms.pop().unwrap(); let head = terms.pop().unwrap(); - let first_branch_num = self.current_branch_num.split(); + let first_branch_num = Arc::new(self.current_branch_num.split()); let branches: Vec<_> = std::iter::once(head) .chain(unfold_by_str(tail, atom!(";")).into_iter()) .collect(); @@ -553,18 +558,18 @@ impl VariableClassifier { let succ_branch_number = branch_numbers[idx - 1].incr_by_delta(); branch_numbers.push(if idx + 1 < branches.len() { - succ_branch_number.split() + Arc::new(succ_branch_number.split()) } else { - succ_branch_number + Arc::new(succ_branch_number) }); } let build_stack_len = build_stack.len(); build_stack.reserve_branch(branches.len()); - state_stack.push(TraversalState::RepBranchNum( + state_stack.push(TraversalState::RepBranchNum(Arc::new( self.current_branch_num.halve_delta(), - )); + ))); let iter = branches.into_iter().zip(branch_numbers.into_iter()); let final_disjunct_loc = state_stack.len(); @@ -619,14 +624,14 @@ impl VariableClassifier { let not_term = terms.pop().unwrap(); let build_stack_len = build_stack.len(); - let first_branch_num = self.current_branch_num.split(); - let second_branch_num = first_branch_num.incr_by_delta(); + let first_branch_num = Arc::new(self.current_branch_num.split()); + let second_branch_num = Arc::new(first_branch_num.incr_by_delta()); build_stack.reserve_branch(2); - state_stack.push(TraversalState::RepBranchNum( + state_stack.push(TraversalState::RepBranchNum(Arc::new( self.current_branch_num.halve_delta(), - )); + ))); state_stack.push(TraversalState::BuildFinalDisjunct(build_stack_len)); state_stack.push(TraversalState::Term(Term::Clause( Cell::default(), diff --git a/src/variable_records.rs b/src/variable_records.rs index e8a867df..fde9fbcf 100644 --- a/src/variable_records.rs +++ b/src/variable_records.rs @@ -7,6 +7,7 @@ use indexmap::{IndexMap, IndexSet}; use num_order::NumOrd; use std::ops::{Deref, DerefMut}; +use std::sync::Arc; #[derive(Debug, Clone)] pub struct TempVarData { @@ -17,7 +18,7 @@ pub struct TempVarData { #[derive(Debug, Clone, PartialEq, Eq)] pub struct BranchDesignator { - pub branch_num: BranchNumber, + pub branch_num: Arc, } impl BranchDesignator { -- 2.54.0