Update List.reserve to no longer use Nat

This commit is contained in:
Richard Feldman 2024-01-22 23:48:21 -05:00
parent e207a7ce53
commit 27474d4ed8
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
4 changed files with 14 additions and 8 deletions

View file

@ -482,16 +482,22 @@ pub fn listWithCapacity(
pub fn listReserve( pub fn listReserve(
list: RocList, list: RocList,
alignment: u32, alignment: u32,
spare: usize, spare: u64,
element_width: usize, element_width: usize,
update_mode: UpdateMode, update_mode: UpdateMode,
) callconv(.C) RocList { ) callconv(.C) RocList {
const old_length = list.len(); const original_len = list.len();
if ((update_mode == .InPlace or list.isUnique()) and list.getCapacity() >= list.len() + spare) { const cap = @as(u64, @intCast(list.getCapacity()));
const desired_cap = @as(u64, @intCast(original_len)) +| spare;
if ((update_mode == .InPlace or list.isUnique()) and cap >= desired_cap) {
return list; return list;
} else { } else {
var output = list.reallocate(alignment, old_length + spare, element_width); // Make sure on 32-bit targets we don't accidentally wrap when we cast our U64 desired capacity to U32.
output.length = old_length; const reserve_size: u64 = @min(desired_cap, @as(u64, @intCast(std.math.maxInt(usize))));
var output = list.reallocate(alignment, @as(usize, @intCast(reserve_size)), element_width);
output.length = original_len;
return output; return output;
} }
} }

View file

@ -169,7 +169,7 @@ pub(crate) fn list_get_unsafe<'a, 'ctx>(
) )
} }
/// List.reserve : List elem, Nat -> List elem /// List.reserve : List elem, U64 -> List elem
pub(crate) fn list_reserve<'a, 'ctx>( pub(crate) fn list_reserve<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>, env: &Env<'a, 'ctx, '_>,
layout_interner: &STLayoutInterner<'a>, layout_interner: &STLayoutInterner<'a>,

View file

@ -688,7 +688,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
list_prepend(env, layout_interner, original_wrapper, elem, elem_layout) list_prepend(env, layout_interner, original_wrapper, elem, elem_layout)
} }
ListReserve => { ListReserve => {
// List.reserve : List elem, Nat -> List elem // List.reserve : List elem, U64 -> List elem
debug_assert_eq!(args.len(), 2); debug_assert_eq!(args.len(), 2);
let (list, list_layout) = scope.load_symbol_and_layout(&args[0]); let (list, list_layout) = scope.load_symbol_and_layout(&args[0]);

View file

@ -514,7 +514,7 @@ impl<'a> LowLevelCall<'a> {
// (return pointer) i32 // (return pointer) i32
// list: RocList i32 // list: RocList i32
// alignment: u32 i32 // alignment: u32 i32
// spare: usize i32 // spare: u64 i64
// element_width: usize i32 // element_width: usize i32
// update_mode: UpdateMode i32 // update_mode: UpdateMode i32