Skip to content

Commit

Permalink
optimize op_gr for small int
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Feb 2, 2024
1 parent 8bdf69b commit bc714c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,7 @@ impl Allocator {
}
}
(ObjectType::SmallAtom, val) => Some(val as u32),
(ObjectType::Pair, _) => {
panic!("small_number() called on pair");
}
(ObjectType::Pair, _) => None,
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/more_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,20 @@ pub fn op_mod(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {

pub fn op_gr(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
let [v0, v1] = get_args::<2>(a, input, ">")?;
let (v0, v0_len) = int_atom(a, v0, ">")?;
let (v1, v1_len) = int_atom(a, v1, ">")?;
let cost = GR_BASE_COST + (v0_len + v1_len) as Cost * GR_COST_PER_BYTE;
Ok(Reduction(cost, if v0 > v1 { a.one() } else { a.nil() }))

match (a.small_number(v0), a.small_number(v1)) {
(Some(lhs), Some(rhs)) => {
let cost =
GR_BASE_COST + (len_for_value(lhs) + len_for_value(rhs)) as Cost * GR_COST_PER_BYTE;
Ok(Reduction(cost, if lhs > rhs { a.one() } else { a.nil() }))
}
_ => {
let (v0, v0_len) = int_atom(a, v0, ">")?;
let (v1, v1_len) = int_atom(a, v1, ">")?;
let cost = GR_BASE_COST + (v0_len + v1_len) as Cost * GR_COST_PER_BYTE;
Ok(Reduction(cost, if v0 > v1 { a.one() } else { a.nil() }))
}
}
}

pub fn op_gr_bytes(a: &mut Allocator, input: NodePtr, _max_cost: Cost) -> Response {
Expand Down

0 comments on commit bc714c7

Please sign in to comment.