]> Repositorios git - scryer-prolog.git/commitdiff
fix large enum variant size difference warning of PermVarAllocation
authorSkgland <[email protected]>
Fri, 17 Apr 2026 23:29:28 +0000 (01:29 +0200)
committerSkgland <[email protected]>
Sat, 25 Apr 2026 14:19:53 +0000 (16:19 +0200)
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
src/forms.rs
src/iterators.rs
src/machine/disjuncts.rs
src/variable_records.rs

index 6988420ffb0722f1d91aceff8b6e5e029994c3c6..d6cd50bdfb11b5cb0ac06a71a4c507c95c02cdb6 100644 (file)
@@ -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<usize, BitVec, FxBuildHasher>; // key: var_num, value: branch arm occurrences.
 
@@ -25,12 +26,12 @@ pub struct BranchOccurrences {
     pub deep_safety: BitSet<usize>,
     pub num_branches: usize,
     pub current_branch_idx: usize,
-    pub current_branch_num: BranchNumber,
+    pub current_branch_num: Arc<BranchNumber>,
     pub subsumed_hits: SubsumedBranchHits,
 }
 
 impl BranchOccurrences {
-    fn new(current_branch_num: BranchNumber, num_branches: usize) -> Self {
+    fn new(current_branch_num: Arc<BranchNumber>, 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<BranchNumber>, 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<BranchNumber>) {
         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(
index 310228e460e36b838c8bb1ede143c4d960b4b1f1..9f9d00a3adefdd5688647d4e00f795701e24072b 100644 (file)
@@ -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<BranchNumber>,
+        branch_nums: Vec<Arc<BranchNumber>>,
         arms: Vec<VecDeque<ChunkedTerms>>,
     },
     Chunk {
index 0aa7bd3895a7005d5e14e6abe281d39abad24f78..d457740a5eadc2db9b083fcc2c7ac2f5d766577c 100644 (file)
@@ -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<ChunkedTerms>, usize),
     RemainingBranches(
-        &'a Vec<BranchNumber>,
+        &'a Vec<Arc<BranchNumber>>,
         &'a Vec<VecDeque<ChunkedTerms>>,
         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<BranchNumber>,
         num_branches: usize,
     },
     NextBranch {
-        branch_num: &'a BranchNumber,
+        branch_num: &'a Arc<BranchNumber>,
     },
     BranchEnd {
         depth: usize,
index 09eec4a1080764045f90106c8063055f1e4f6751..65698e36da8fb50e3b01dbd1ccb172d9e9ca3e4b 100644 (file)
@@ -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<BranchNumber>,
     chunks: Vec<ChunkInfo>,
 }
 
 impl BranchInfo {
-    fn new(branch_num: BranchNumber) -> Self {
+    fn new(branch_num: Arc<BranchNumber>) -> Self {
         Self {
             branch_num,
             chunks: vec![],
@@ -68,7 +69,7 @@ impl DerefMut for BranchMap {
     }
 }
 
-type RootSet = IndexSet<BranchNumber>;
+type RootSet = IndexSet<Arc<BranchNumber>>;
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 pub struct ClassifyInfo {
@@ -92,14 +93,14 @@ enum TraversalState {
     Term(Term),
     OverrideGlobalCutVar(usize),
     ResetGlobalCutVarOverride(Option<usize>),
-    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<BranchNumber>), // set current_branch_num, add it to the root set
+    RepBranchNum(Arc<BranchNumber>), // 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<BranchNumber>,
     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<Item = BranchInfo>) -> 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<BranchNumber>,
     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(),
index e8a867df6a35e346eab17fe57b8e7c281ebf5020..fde9fbcfe07205d10fb872feeec1a1a21dc3d743 100644 (file)
@@ -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<BranchNumber>,
 }
 
 impl BranchDesignator {