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.
This commit is contained in:
Skgland
2026-04-18 01:29:28 +02:00
parent f7dc1d72f6
commit ff293a56e6
5 changed files with 43 additions and 34 deletions

View 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(

View 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 {

View 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,

View 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(),

View 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 {