From: Mark Date: Thu, 29 Feb 2024 23:15:28 +0000 (-0700) Subject: make indexer downcast Integers to Fixnums when possible, be slightly more judicious... X-Git-Tag: v0.10.0~159 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=42f48278546ec79a045b97e3e998e65708f4907d;p=scryer-prolog.git make indexer downcast Integers to Fixnums when possible, be slightly more judicious about allocating Integers instead of Fixnums (#2340) --- diff --git a/src/indexing.rs b/src/indexing.rs index 5956dcfc..4eb39e2d 100644 --- a/src/indexing.rs +++ b/src/indexing.rs @@ -1113,16 +1113,10 @@ pub(crate) fn constant_key_alternatives( constants.push(Literal::Atom(atom)); } /* + // constant_to_literal takes care of the downward conversion from Integer to Fixnum + // if possible. Literal::Fixnum(ref n) => { - constants.push(Literal::Integer(arena_alloc!(n, arena))); //Rc::new(Integer::from(*n)))); - - /* - if *n >= 0 { - if let Ok(n) = usize::try_from(*n) { - constants.push(Literal::Usize(n)); - } - } - */ + constants.push(Literal::Integer(arena_alloc!(n, arena))); } */ Literal::Integer(ref n) => { diff --git a/src/machine/arithmetic_ops.rs b/src/machine/arithmetic_ops.rs index 3de47a60..8af9280c 100644 --- a/src/machine/arithmetic_ops.rs +++ b/src/machine/arithmetic_ops.rs @@ -641,12 +641,10 @@ pub(crate) fn shr(n1: Number, n2: Number, arena: &mut Arena) -> Result> n2, arena)) + Ok(Number::arena_from(n1_i >> n2, arena)) } else { - Ok(Number::arena_from(n1 >> usize::max_value(), arena)) + Ok(Number::arena_from(n1_i >> usize::max_value(), arena)) } } (Number::Fixnum(n1), Number::Integer(n2)) => { @@ -698,11 +696,10 @@ pub(crate) fn shl(n1: Number, n2: Number, arena: &mut Arena) -> Result { - Literal::Integer(n) + let result = (&*n).try_into(); + + match result { + Ok(fixnum) => if let Ok(n) = Fixnum::build_with_checked(fixnum) { + Literal::Fixnum(n) + } else { + Literal::Integer(n) + }, + Err(_) => Literal::Integer(n) + } } _ => { unreachable!()