Merge branch 'main' into simplify-refcount

This commit is contained in:
Luke Boswell 2025-01-09 10:18:00 +11:00 committed by GitHub
commit 37cd04c002
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
730 changed files with 22850 additions and 23870 deletions

View file

@ -76,7 +76,7 @@ comptime {
fn testing_roc_alloc(size: usize, nominal_alignment: u32) callconv(.C) ?*anyopaque {
const real_alignment = 16;
if (nominal_alignment > real_alignment) {
@panic("alignments larger than that of usize are not currently supported");
@panic("alignments larger than that of 2 usize are not currently supported");
}
// We store an extra usize which is the size of the data plus the size of the size, directly before the data.
// We need enough clocks of the alignment size to fit this (usually this will be one)
@ -98,12 +98,16 @@ fn testing_roc_alloc(size: usize, nominal_alignment: u32) callconv(.C) ?*anyopaq
return data_ptr;
}
fn testing_roc_realloc(c_ptr: *anyopaque, new_size: usize, old_size: usize, _: u32) callconv(.C) ?*anyopaque {
const raw_ptr = @as([*]u8, @ptrCast(c_ptr)) - @sizeOf(usize);
fn testing_roc_realloc(c_ptr: *anyopaque, new_size: usize, old_size: usize, nominal_alignment: u32) callconv(.C) ?*anyopaque {
const real_alignment = 16;
if (nominal_alignment > real_alignment) {
@panic("alignments larger than that of 2 usize are not currently supported");
}
const raw_ptr = @as([*]align(real_alignment) u8, @alignCast(@as([*]u8, @ptrCast(c_ptr)) - @sizeOf(usize)));
const slice = raw_ptr[0..(old_size + @sizeOf(usize))];
const new_full_size = new_size + @sizeOf(usize);
var new_raw_ptr = (std.testing.allocator.realloc(slice, new_full_size) catch unreachable).ptr;
var new_raw_ptr = @as([*]u8, @alignCast((std.testing.allocator.realloc(slice, new_full_size) catch unreachable).ptr));
@as([*]usize, @alignCast(@ptrCast(new_raw_ptr)))[0] = new_full_size;
new_raw_ptr += @sizeOf(usize);
const new_ptr = @as(?*anyopaque, @ptrCast(new_raw_ptr));
@ -478,15 +482,16 @@ pub inline fn calculateCapacity(
requested_length: usize,
element_width: usize,
) usize {
// TODO: there are two adjustments that would likely lead to better results for Roc.
// 1. Deal with the fact we allocate an extra u64 for refcount.
// This may lead to allocating page size + 8 bytes.
// That could mean allocating an entire page for 8 bytes of data which isn't great.
// 2. Deal with the fact that we can request more than 1 element at a time.
// fbvector assumes just appending 1 element at a time when using this algorithm.
// As such, they will generally grow in a way that should better match certain memory multiple.
// This is also the normal case for roc, but we could also grow by a much larger amount.
// We may want to round to multiples of 2 or something similar.
// TODO: Deal with the fact we allocate an extra u64 for refcount.
// This may lead to allocating page size + 8 bytes.
// That could mean allocating an entire page for 8 bytes of data which isn't great.
if (requested_length != old_capacity + 1) {
// The user is explicitly requesting n elements.
// Trust the user and just reserve that amount.
return requested_length;
}
var new_capacity: usize = 0;
if (element_width == 0) {
return requested_length;