mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
Extract bounds_check_comparison
This commit is contained in:
parent
ef38095003
commit
3e71b5a38d
1 changed files with 14 additions and 7 deletions
|
@ -1102,13 +1102,7 @@ fn call_with_args<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
// Bounds check: only proceed if index < length.
|
// Bounds check: only proceed if index < length.
|
||||||
// Otherwise, return the list unaltered.
|
// Otherwise, return the list unaltered.
|
||||||
//
|
let comparison = bounds_check_comparison(builder, elem_index, list_len);
|
||||||
// Note: Check for index < length as the "true" condition,
|
|
||||||
// to avoid misprediction. (In practice this should usually pass,
|
|
||||||
// and CPUs generally default to predicting that a forward jump
|
|
||||||
// shouldn't be taken; that is, they predict "else" won't be taken.)
|
|
||||||
let comparison =
|
|
||||||
builder.build_int_compare(IntPredicate::ULT, elem_index, list_len, "bounds_check");
|
|
||||||
|
|
||||||
// If the index is in bounds, clone and mutate in place.
|
// If the index is in bounds, clone and mutate in place.
|
||||||
let then_val: BasicValueEnum = {
|
let then_val: BasicValueEnum = {
|
||||||
|
@ -1272,3 +1266,16 @@ fn clone_list<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
(struct_val.into_struct_value(), ptr_val)
|
(struct_val.into_struct_value(), ptr_val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bounds_check_comparison<'ctx>(
|
||||||
|
builder: &Builder<'ctx>,
|
||||||
|
elem_index: IntValue<'ctx>,
|
||||||
|
len: IntValue<'ctx>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
//
|
||||||
|
// Note: Check for index < length as the "true" condition,
|
||||||
|
// to avoid misprediction. (In practice this should usually pass,
|
||||||
|
// and CPUs generally default to predicting that a forward jump
|
||||||
|
// shouldn't be taken; that is, they predict "else" won't be taken.)
|
||||||
|
builder.build_int_compare(IntPredicate::ULT, elem_index, len, "bounds_check")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue