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

@@ -6141,8 +6141,8 @@ impl Machine {
let a2 = self.deref_register(2);
let n = match Number::try_from((a2, &self.machine_st.arena.f64_tbl)) {
Ok(Number::Fixnum(bp)) => Integer::from(bp.get_num() as usize),
Ok(Number::Integer(n)) => (*n).clone(),
Ok(Number::Fixnum(bp)) => bp.get_num() as u128,
Ok(Number::Integer(n)) => u128::try_from(&*n).unwrap(),
_ => {
let stub = functor_stub(atom!("call_with_inference_limit"), 3);
let err = self.machine_st.type_error(ValidType::Integer, a2);
@@ -6153,21 +6153,21 @@ impl Machine {
let bp = unsafe { a1.to_fixnum_or_cut_point_unchecked() }.get_num() as usize;
let a3 = self.deref_register(3);
let count = self.machine_st.cwil.add_limit(n, bp).clone();
let count = self.machine_st.cwil.add_limit(n, bp);
self.inference_count(a3, count);
Ok(())
}
#[inline(always)]
pub(crate) fn inference_count(&mut self, count_var: HeapCellValue, count: Integer) {
if let Some(value) = <&Integer as TryInto<i64>>::try_into(&count)
pub(crate) fn inference_count(&mut self, count_var: HeapCellValue, count: u128) {
if let Some(value) = TryInto::<i64>::try_into(count)
.ok()
.and_then(|i| Fixnum::build_with_checked(i).ok())
{
self.machine_st.unify_fixnum(value, count_var);
} else {
let count = arena_alloc!(count, &mut self.machine_st.arena);
let count = arena_alloc!(Integer::from(count), &mut self.machine_st.arena);
self.machine_st.unify_big_int(count, count_var);
}
}
@@ -6321,11 +6321,11 @@ impl Machine {
let a2 = self.deref_register(2);
let block = unsafe { a1.to_fixnum_or_cut_point_unchecked() }.get_num() as usize;
let count = self.machine_st.cwil.remove_limit(block).clone();
if let Ok(value) = Fixnum::build_with_checked(&count) {
let count = self.machine_st.cwil.remove_limit(block);
if let Ok(value) = Fixnum::build_with_checked(count) {
self.machine_st.unify_fixnum(value, a2);
} else {
let count = arena_alloc!(count.clone(), &mut self.machine_st.arena);
let count = arena_alloc!(Integer::from(count), &mut self.machine_st.arena);
self.machine_st.unify_big_int(count, a2);
}
}