switch from Integer to u128

- u128 is only 16 bytes instead of 24
- incrementing u128 does not involve heap allocations
- u128 should be sufficent
 it would take more than 2 sextilion years to overflow if we would be incrementing it every tick at 5GHz
   i.e. 2^128-1 / 5 GHz / 60 / 60 / 24 / 356 > 2 sextilion

before this ~3.2% of the execution time of the program in https://github.com/mthom/scryer-prolog/issues/3265#issuecomment-4103176469 was spend in the increment_call_count function, after this change it's down to 0.3%
This commit is contained in:
Skgland
2026-03-21 13:36:40 +01:00
parent 79a9b950cb
commit 8aab887388
5 changed files with 22 additions and 23 deletions

View File

@@ -16,7 +16,6 @@ use crate::parser::ast::*;
use crate::read::TermWriteResult;
use crate::types::*;
use crate::parser::dashu::Integer;
use indexmap::IndexMap;
@@ -1112,23 +1111,23 @@ impl MachineState {
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug)]
pub(crate) struct CWIL {
local_count: Integer,
pub(crate) global_count: Integer,
limits: Vec<(Integer, usize)>,
local_count: u128,
pub(crate) global_count: u128,
limits: Vec<(u128, usize)>,
pub(crate) inference_limit_exceeded: bool,
}
impl CWIL {
pub(crate) fn new() -> Self {
CWIL {
local_count: Integer::from(0),
global_count: Integer::from(0),
local_count: 0,
global_count: 0,
limits: vec![],
inference_limit_exceeded: false,
}
}
pub(crate) fn add_limit(&mut self, mut limit: Integer, block: usize) -> &Integer {
pub(crate) fn add_limit(&mut self, mut limit: u128, block: usize) -> u128 {
limit += &self.local_count;
match self.limits.last() {
@@ -1136,23 +1135,23 @@ impl CWIL {
_ => self.limits.push((limit, block)),
}
&self.local_count
self.local_count
}
#[inline(always)]
pub(crate) fn remove_limit(&mut self, block: usize) -> &Integer {
pub(crate) fn remove_limit(&mut self, block: usize) -> u128 {
if let Some((_, bl)) = self.limits.last() {
if bl == &block {
self.limits.pop();
}
}
&self.local_count
self.local_count
}
#[inline(always)]
pub(crate) fn reset(&mut self) {
self.local_count = Integer::from(0);
self.local_count = 0;
self.limits.clear();
self.inference_limit_exceeded = false;
}