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

@ -144,7 +144,7 @@ pub const RocDec = extern struct {
return (c -% 48) <= 9;
}
pub fn toStr(self: RocDec) RocStr {
pub fn to_str(self: RocDec) RocStr {
// Special case
if (self.num == 0) {
return RocStr.init("0.0", 3);
@ -1031,97 +1031,97 @@ test "fromStr: .123.1" {
try expectEqual(dec, null);
}
test "toStr: 100.00" {
test "to_str: 100.00" {
var dec: RocDec = .{ .num = 100000000000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "100.0"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 123.45" {
test "to_str: 123.45" {
var dec: RocDec = .{ .num = 123450000000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "123.45"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: -123.45" {
test "to_str: -123.45" {
var dec: RocDec = .{ .num = -123450000000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "-123.45"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 123.0" {
test "to_str: 123.0" {
var dec: RocDec = .{ .num = 123000000000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "123.0"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: -123.0" {
test "to_str: -123.0" {
var dec: RocDec = .{ .num = -123000000000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "-123.0"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 0.45" {
test "to_str: 0.45" {
var dec: RocDec = .{ .num = 450000000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "0.45"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: -0.45" {
test "to_str: -0.45" {
var dec: RocDec = .{ .num = -450000000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "-0.45"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 0.00045" {
test "to_str: 0.00045" {
var dec: RocDec = .{ .num = 450000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "0.00045"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: -0.00045" {
test "to_str: -0.00045" {
var dec: RocDec = .{ .num = -450000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "-0.00045"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: -111.123456" {
test "to_str: -111.123456" {
var dec: RocDec = .{ .num = -111123456000000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "-111.123456"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 123.1111111" {
test "to_str: 123.1111111" {
var dec: RocDec = .{ .num = 123111111100000000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "123.1111111"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 123.1111111111111 (big str)" {
test "to_str: 123.1111111111111 (big str)" {
var dec: RocDec = .{ .num = 123111111111111000000 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
errdefer res_roc_str.decref();
defer res_roc_str.decref();
@ -1129,9 +1129,9 @@ test "toStr: 123.1111111111111 (big str)" {
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 123.111111111111444444 (max number of decimal places)" {
test "to_str: 123.111111111111444444 (max number of decimal places)" {
var dec: RocDec = .{ .num = 123111111111111444444 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
errdefer res_roc_str.decref();
defer res_roc_str.decref();
@ -1139,9 +1139,9 @@ test "toStr: 123.111111111111444444 (max number of decimal places)" {
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 12345678912345678912.111111111111111111 (max number of digits)" {
test "to_str: 12345678912345678912.111111111111111111 (max number of digits)" {
var dec: RocDec = .{ .num = 12345678912345678912111111111111111111 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
errdefer res_roc_str.decref();
defer res_roc_str.decref();
@ -1149,9 +1149,9 @@ test "toStr: 12345678912345678912.111111111111111111 (max number of digits)" {
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: std.math.maxInt" {
test "to_str: std.math.maxInt" {
var dec: RocDec = .{ .num = std.math.maxInt(i128) };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
errdefer res_roc_str.decref();
defer res_roc_str.decref();
@ -1159,9 +1159,9 @@ test "toStr: std.math.maxInt" {
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: std.math.minInt" {
test "to_str: std.math.minInt" {
var dec: RocDec = .{ .num = std.math.minInt(i128) };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
errdefer res_roc_str.decref();
defer res_roc_str.decref();
@ -1169,9 +1169,9 @@ test "toStr: std.math.minInt" {
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
}
test "toStr: 0" {
test "to_str: 0" {
var dec: RocDec = .{ .num = 0 };
var res_roc_str = dec.toStr();
var res_roc_str = dec.to_str();
const res_slice: []const u8 = "0.0"[0..];
try expectEqualSlices(u8, res_slice, res_roc_str.asSlice());
@ -1443,8 +1443,8 @@ pub fn fromStr(arg: RocStr) callconv(.C) num_.NumParseResult(i128) {
}
}
pub fn toStr(arg: RocDec) callconv(.C) RocStr {
return @call(.always_inline, RocDec.toStr, .{arg});
pub fn to_str(arg: RocDec) callconv(.C) RocStr {
return @call(.always_inline, RocDec.to_str, .{arg});
}
pub fn fromF64C(arg: f64) callconv(.C) i128 {

View file

@ -50,7 +50,7 @@ comptime {
exportDecFn(dec.toF64, "to_f64");
exportDecFn(dec.toI128, "to_i128");
exportDecFn(dec.fromI128, "from_i128");
exportDecFn(dec.toStr, "to_str");
exportDecFn(dec.to_str, "to_str");
for (INTEGERS) |T| {
dec.exportFromInt(T, ROC_BUILTINS ++ ".dec.from_int.");

View file

@ -2053,7 +2053,7 @@ test "strTrim: null byte" {
defer original_with_capacity.decref();
try expectEqual(@as(usize, 1), original_with_capacity.len());
try expectEqual(@as(usize, 64), original_with_capacity.getCapacity());
try expectEqual(@as(usize, 41), original_with_capacity.getCapacity());
const trimmed = strTrim(original.clone());
defer trimmed.decref();

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;