Switched order of checking layout and list length for list reverse implementation

This commit is contained in:
Chad Stearns 2020-07-12 17:11:26 -04:00
parent fdf11689b6
commit 1221ef3c76

View file

@ -1649,27 +1649,27 @@ fn run_low_level<'a, 'ctx, 'env>(
let (list, list_layout) = &args[0]; let (list, list_layout) = &args[0];
let wrapper_struct = match list_layout {
build_expr(env, layout_ids, scope, parent, list).into_struct_value(); Layout::Builtin(Builtin::List(elem_layout)) => {
let wrapper_struct =
build_expr(env, layout_ids, scope, parent, list).into_struct_value();
let builder = env.builder; let builder = env.builder;
let ctx = env.context; let ctx = env.context;
let list_len = load_list_len(builder, wrapper_struct); let list_len = load_list_len(builder, wrapper_struct);
// list_len > 0 // list_len > 0
// We do this check to avoid allocating memory. If the input // We do this check to avoid allocating memory. If the input
// list is empty, then we can just return an empty list. // list is empty, then we can just return an empty list.
let comparison = builder.build_int_compare( let comparison = builder.build_int_compare(
IntPredicate::UGT, IntPredicate::UGT,
list_len, list_len,
ctx.i64_type().const_int(0, false), ctx.i64_type().const_int(0, false),
"greaterthanzero", "greaterthanzero",
); );
let build_then = || { let build_then = || {
match list_layout {
Layout::Builtin(Builtin::List(elem_layout)) => {
// Allocate space for the new array that we'll copy into. // Allocate space for the new array that we'll copy into.
let elem_type = let elem_type =
basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes); basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
@ -1785,26 +1785,26 @@ fn run_low_level<'a, 'ctx, 'env>(
collection(ctx, ptr_bytes), collection(ctx, ptr_bytes),
"cast_collection", "cast_collection",
) )
} };
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
_ => { let build_else = || empty_list(env);
unreachable!("Invalid List layout for List.get: {:?}", list_layout);
} let struct_type = collection(ctx, env.ptr_bytes);
build_basic_phi2(
env,
parent,
comparison,
build_then,
build_else,
BasicTypeEnum::StructType(struct_type),
)
} }
}; Layout::Builtin(Builtin::EmptyList) => empty_list(env),
_ => {
let build_else = || empty_list(env); unreachable!("Invalid List layout for List.reverse {:?}", list_layout);
}
let struct_type = collection(ctx, env.ptr_bytes); }
build_basic_phi2(
env,
parent,
comparison,
build_then,
build_else,
BasicTypeEnum::StructType(struct_type),
)
} }
ListPush => { ListPush => {
// List.push List elem, elem -> List elem // List.push List elem, elem -> List elem