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:
@@ -15,6 +15,7 @@ use indexmap::IndexMap;
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub type BranchHits = IndexMap<usize, BitVec, FxBuildHasher>; // key: var_num, value: branch arm occurrences.
|
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 deep_safety: BitSet<usize>,
|
||||||
pub num_branches: usize,
|
pub num_branches: usize,
|
||||||
pub current_branch_idx: usize,
|
pub current_branch_idx: usize,
|
||||||
pub current_branch_num: BranchNumber,
|
pub current_branch_num: Arc<BranchNumber>,
|
||||||
pub subsumed_hits: SubsumedBranchHits,
|
pub subsumed_hits: SubsumedBranchHits,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BranchOccurrences {
|
impl BranchOccurrences {
|
||||||
fn new(current_branch_num: BranchNumber, num_branches: usize) -> Self {
|
fn new(current_branch_num: Arc<BranchNumber>, num_branches: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
hits: BranchHits::with_hasher(FxBuildHasher::default()),
|
hits: BranchHits::with_hasher(FxBuildHasher::default()),
|
||||||
shallow_safety: BitSet::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));
|
self.push(BranchOccurrences::new(branch_num, num_branches));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +113,7 @@ impl BranchStack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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();
|
let branch_occurrences = self.last_mut().unwrap();
|
||||||
branch_occurrences.current_branch_idx += 1;
|
branch_occurrences.current_branch_idx += 1;
|
||||||
branch_occurrences.current_branch_num = branch_num;
|
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() {
|
let (deep_safety, shallow_safety) = match self.branch_stack.last_mut() {
|
||||||
Some(latest_branch) => {
|
Some(latest_branch) => {
|
||||||
@@ -506,7 +507,7 @@ impl DebrayAllocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn mark_safe_var_unconditionally(&mut self, var_num: usize) {
|
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 {
|
match &mut self.var_data.records[var_num].allocation {
|
||||||
VarAlloc::Perm(
|
VarAlloc::Perm(
|
||||||
@@ -530,7 +531,7 @@ impl DebrayAllocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn mark_safe_var(&mut self, var_num: usize, lvl: Level, term_loc: GenContext) {
|
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 {
|
match &mut self.var_data.records[var_num].allocation {
|
||||||
VarAlloc::Perm(
|
VarAlloc::Perm(
|
||||||
@@ -574,7 +575,7 @@ impl DebrayAllocator {
|
|||||||
r: RegType,
|
r: RegType,
|
||||||
arg_c: usize,
|
arg_c: usize,
|
||||||
) -> Instruction {
|
) -> 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 {
|
match &mut self.var_data.records[var_num].allocation {
|
||||||
VarAlloc::Perm(
|
VarAlloc::Perm(
|
||||||
@@ -610,7 +611,7 @@ impl DebrayAllocator {
|
|||||||
var_num: usize,
|
var_num: usize,
|
||||||
r: RegType,
|
r: RegType,
|
||||||
) -> Instruction {
|
) -> 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 {
|
match &mut self.var_data.records[var_num].allocation {
|
||||||
VarAlloc::Perm(
|
VarAlloc::Perm(
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ use std::fmt;
|
|||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::ops::{AddAssign, Deref, DerefMut};
|
use std::ops::{AddAssign, Deref, DerefMut};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub type PredicateKey = (Atom, usize); // name, arity.
|
pub type PredicateKey = (Atom, usize); // name, arity.
|
||||||
|
|
||||||
@@ -194,7 +195,7 @@ impl BranchNumber {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ChunkedTerms {
|
pub enum ChunkedTerms {
|
||||||
Branch {
|
Branch {
|
||||||
branch_nums: Vec<BranchNumber>,
|
branch_nums: Vec<Arc<BranchNumber>>,
|
||||||
arms: Vec<VecDeque<ChunkedTerms>>,
|
arms: Vec<VecDeque<ChunkedTerms>>,
|
||||||
},
|
},
|
||||||
Chunk {
|
Chunk {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use std::cell::Cell;
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::iter::*;
|
use std::iter::*;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
#[allow(clippy::borrowed_box)]
|
#[allow(clippy::borrowed_box)]
|
||||||
@@ -320,7 +321,7 @@ pub(crate) fn breadth_first_iter(
|
|||||||
enum ClauseIteratorState<'a> {
|
enum ClauseIteratorState<'a> {
|
||||||
RemainingChunks(&'a VecDeque<ChunkedTerms>, usize),
|
RemainingChunks(&'a VecDeque<ChunkedTerms>, usize),
|
||||||
RemainingBranches(
|
RemainingBranches(
|
||||||
&'a Vec<BranchNumber>,
|
&'a Vec<Arc<BranchNumber>>,
|
||||||
&'a Vec<VecDeque<ChunkedTerms>>,
|
&'a Vec<VecDeque<ChunkedTerms>>,
|
||||||
usize,
|
usize,
|
||||||
),
|
),
|
||||||
@@ -329,11 +330,11 @@ enum ClauseIteratorState<'a> {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) enum ClauseItem<'a> {
|
pub(crate) enum ClauseItem<'a> {
|
||||||
FirstBranch {
|
FirstBranch {
|
||||||
branch_num: &'a BranchNumber,
|
branch_num: &'a Arc<BranchNumber>,
|
||||||
num_branches: usize,
|
num_branches: usize,
|
||||||
},
|
},
|
||||||
NextBranch {
|
NextBranch {
|
||||||
branch_num: &'a BranchNumber,
|
branch_num: &'a Arc<BranchNumber>,
|
||||||
},
|
},
|
||||||
BranchEnd {
|
BranchEnd {
|
||||||
depth: usize,
|
depth: usize,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ use std::cell::Cell;
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct VarInfo {
|
pub struct VarInfo {
|
||||||
@@ -34,12 +35,12 @@ pub struct ChunkInfo {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct BranchInfo {
|
pub struct BranchInfo {
|
||||||
branch_num: BranchNumber,
|
branch_num: Arc<BranchNumber>,
|
||||||
chunks: Vec<ChunkInfo>,
|
chunks: Vec<ChunkInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BranchInfo {
|
impl BranchInfo {
|
||||||
fn new(branch_num: BranchNumber) -> Self {
|
fn new(branch_num: Arc<BranchNumber>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
branch_num,
|
branch_num,
|
||||||
chunks: vec![],
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct ClassifyInfo {
|
pub struct ClassifyInfo {
|
||||||
@@ -92,14 +93,14 @@ enum TraversalState {
|
|||||||
Term(Term),
|
Term(Term),
|
||||||
OverrideGlobalCutVar(usize),
|
OverrideGlobalCutVar(usize),
|
||||||
ResetGlobalCutVarOverride(Option<usize>),
|
ResetGlobalCutVarOverride(Option<usize>),
|
||||||
AddBranchNum(BranchNumber), // set current_branch_num, add it to the root set
|
AddBranchNum(Arc<BranchNumber>), // set current_branch_num, add it to the root set
|
||||||
RepBranchNum(BranchNumber), // replace current_branch_num and the latest in the root set
|
RepBranchNum(Arc<BranchNumber>), // replace current_branch_num and the latest in the root set
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct VariableClassifier {
|
pub struct VariableClassifier {
|
||||||
call_policy: CallPolicy,
|
call_policy: CallPolicy,
|
||||||
current_branch_num: BranchNumber,
|
current_branch_num: Arc<BranchNumber>,
|
||||||
current_chunk_num: usize,
|
current_chunk_num: usize,
|
||||||
current_chunk_type: ChunkType,
|
current_chunk_type: ChunkType,
|
||||||
branch_map: BranchMap,
|
branch_map: BranchMap,
|
||||||
@@ -156,22 +157,26 @@ pub type ClassifyFactResult = (Term, VarData);
|
|||||||
pub type ClassifyRuleResult = (Term, ChunkedTermVec, VarData);
|
pub type ClassifyRuleResult = (Term, ChunkedTermVec, VarData);
|
||||||
|
|
||||||
fn merge_branch_seq(branches: impl Iterator<Item = BranchInfo>) -> BranchInfo {
|
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 {
|
for mut branch in branches {
|
||||||
branch_info.branch_num = branch.branch_num;
|
branch_info.branch_num = branch.branch_num;
|
||||||
branch_info.chunks.append(&mut branch.chunks);
|
branch_info.chunks.append(&mut branch.chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
branch_info.branch_num.delta = branch_info.branch_num.delta * Integer::from(2);
|
let new_delta = branch_info.branch_num.delta.clone() * Integer::from(2);
|
||||||
branch_info.branch_num.branch_num -= &branch_info.branch_num.delta;
|
|
||||||
|
branch_info.branch_num = Arc::new(BranchNumber {
|
||||||
|
branch_num: branch_info.branch_num.branch_num.clone() - &new_delta,
|
||||||
|
delta: new_delta,
|
||||||
|
});
|
||||||
|
|
||||||
branch_info
|
branch_info
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flatten_into_disjunct(
|
fn flatten_into_disjunct(
|
||||||
build_stack: &mut ChunkedTermVec,
|
build_stack: &mut ChunkedTermVec,
|
||||||
branch_num: BranchNumber,
|
branch_num: Arc<BranchNumber>,
|
||||||
preceding_len: usize,
|
preceding_len: usize,
|
||||||
) {
|
) {
|
||||||
let branch_vec = build_stack.drain(preceding_len + 1..).collect();
|
let branch_vec = build_stack.drain(preceding_len + 1..).collect();
|
||||||
@@ -188,7 +193,7 @@ impl VariableClassifier {
|
|||||||
pub fn new(call_policy: CallPolicy) -> Self {
|
pub fn new(call_policy: CallPolicy) -> Self {
|
||||||
Self {
|
Self {
|
||||||
call_policy,
|
call_policy,
|
||||||
current_branch_num: BranchNumber::default(),
|
current_branch_num: Arc::new(BranchNumber::default()),
|
||||||
current_chunk_num: 0,
|
current_chunk_num: 0,
|
||||||
current_chunk_type: ChunkType::Head,
|
current_chunk_type: ChunkType::Head,
|
||||||
branch_map: BranchMap(BranchMapInt::new()),
|
branch_map: BranchMap(BranchMapInt::new()),
|
||||||
@@ -542,7 +547,7 @@ impl VariableClassifier {
|
|||||||
let tail = terms.pop().unwrap();
|
let tail = terms.pop().unwrap();
|
||||||
let head = 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)
|
let branches: Vec<_> = std::iter::once(head)
|
||||||
.chain(unfold_by_str(tail, atom!(";")).into_iter())
|
.chain(unfold_by_str(tail, atom!(";")).into_iter())
|
||||||
.collect();
|
.collect();
|
||||||
@@ -553,18 +558,18 @@ impl VariableClassifier {
|
|||||||
let succ_branch_number = branch_numbers[idx - 1].incr_by_delta();
|
let succ_branch_number = branch_numbers[idx - 1].incr_by_delta();
|
||||||
|
|
||||||
branch_numbers.push(if idx + 1 < branches.len() {
|
branch_numbers.push(if idx + 1 < branches.len() {
|
||||||
succ_branch_number.split()
|
Arc::new(succ_branch_number.split())
|
||||||
} else {
|
} else {
|
||||||
succ_branch_number
|
Arc::new(succ_branch_number)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let build_stack_len = build_stack.len();
|
let build_stack_len = build_stack.len();
|
||||||
build_stack.reserve_branch(branches.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(),
|
self.current_branch_num.halve_delta(),
|
||||||
));
|
)));
|
||||||
|
|
||||||
let iter = branches.into_iter().zip(branch_numbers.into_iter());
|
let iter = branches.into_iter().zip(branch_numbers.into_iter());
|
||||||
let final_disjunct_loc = state_stack.len();
|
let final_disjunct_loc = state_stack.len();
|
||||||
@@ -619,14 +624,14 @@ impl VariableClassifier {
|
|||||||
let not_term = terms.pop().unwrap();
|
let not_term = terms.pop().unwrap();
|
||||||
let build_stack_len = build_stack.len();
|
let build_stack_len = build_stack.len();
|
||||||
|
|
||||||
let first_branch_num = self.current_branch_num.split();
|
let first_branch_num = Arc::new(self.current_branch_num.split());
|
||||||
let second_branch_num = first_branch_num.incr_by_delta();
|
let second_branch_num = Arc::new(first_branch_num.incr_by_delta());
|
||||||
|
|
||||||
build_stack.reserve_branch(2);
|
build_stack.reserve_branch(2);
|
||||||
|
|
||||||
state_stack.push(TraversalState::RepBranchNum(
|
state_stack.push(TraversalState::RepBranchNum(Arc::new(
|
||||||
self.current_branch_num.halve_delta(),
|
self.current_branch_num.halve_delta(),
|
||||||
));
|
)));
|
||||||
state_stack.push(TraversalState::BuildFinalDisjunct(build_stack_len));
|
state_stack.push(TraversalState::BuildFinalDisjunct(build_stack_len));
|
||||||
state_stack.push(TraversalState::Term(Term::Clause(
|
state_stack.push(TraversalState::Term(Term::Clause(
|
||||||
Cell::default(),
|
Cell::default(),
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use indexmap::{IndexMap, IndexSet};
|
|||||||
use num_order::NumOrd;
|
use num_order::NumOrd;
|
||||||
|
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TempVarData {
|
pub struct TempVarData {
|
||||||
@@ -17,7 +18,7 @@ pub struct TempVarData {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct BranchDesignator {
|
pub struct BranchDesignator {
|
||||||
pub branch_num: BranchNumber,
|
pub branch_num: Arc<BranchNumber>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BranchDesignator {
|
impl BranchDesignator {
|
||||||
|
|||||||
Reference in New Issue
Block a user