mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +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.
|
||||
// Otherwise, return the list unaltered.
|
||||
//
|
||||
// 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");
|
||||
let comparison = bounds_check_comparison(builder, elem_index, list_len);
|
||||
|
||||
// If the index is in bounds, clone and mutate in place.
|
||||
let then_val: BasicValueEnum = {
|
||||
|
@ -1272,3 +1266,16 @@ fn clone_list<'a, 'ctx, 'env>(
|
|||
|
||||
(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