Skip to content

Commit

Permalink
make indexer downcast Integers to Fixnums when possible, be slightly …
Browse files Browse the repository at this point in the history
…more judicious about allocating Integers instead of Fixnums (#2340)
  • Loading branch information
mthom committed Feb 29, 2024
1 parent f2d3779 commit 42f4827
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 3 additions & 9 deletions src/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
11 changes: 4 additions & 7 deletions src/machine/arithmetic_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,10 @@ pub(crate) fn shr(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
let n1_i = n1.get_num();
let n2_i = n2.get_num();

let n1 = Integer::from(n1_i);

if let Ok(n2) = usize::try_from(n2_i) {
Ok(Number::arena_from(n1 >> 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)) => {
Expand Down Expand Up @@ -698,11 +696,10 @@ pub(crate) fn shl(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
let n1_i = n1.get_num();
let n2_i = n2.get_num();

let n1 = Integer::from(n1_i);

if let Ok(n2) = usize::try_from(n2_i) {
Ok(Number::arena_from(n1 << n2, arena))
Ok(Number::arena_from(n1_i << n2, arena))
} else {
let n1 = Integer::from(n1_i);
Ok(Number::arena_from(n1 << usize::max_value(), arena))
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/machine/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,16 @@ impl MachineState {
Literal::Rational(r)
}
(ArenaHeaderTag::Integer, n) => {
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!()
Expand Down

0 comments on commit 42f4827

Please sign in to comment.