mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
simplify/optimize decref logic
This commit is contained in:
parent
7aac6b6750
commit
4e39543054
2 changed files with 16 additions and 40 deletions
|
@ -105,14 +105,13 @@ pub const IntWidth = enum(u8) {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn decrefC(
|
pub fn decrefC(
|
||||||
bytes_or_null: ?[*]u8,
|
bytes_or_null: ?[*]isize,
|
||||||
data_bytes: usize,
|
|
||||||
alignment: u32,
|
alignment: u32,
|
||||||
) callconv(.C) void {
|
) callconv(.C) void {
|
||||||
// IMPORTANT: bytes_or_null is this case is expected to be a pointer to the refcount
|
// IMPORTANT: bytes_or_null is this case is expected to be a pointer to the refcount
|
||||||
// (NOT the start of the data, or the start of the allocation)
|
// (NOT the start of the data, or the start of the allocation)
|
||||||
if (bytes_or_null) |bytes| {
|
if (bytes_or_null) |bytes| {
|
||||||
return @call(.{ .modifier = always_inline }, decref, .{ bytes + @sizeOf(usize), data_bytes, alignment });
|
return @call(.{ .modifier = always_inline }, decref_ptr_to_refcount, .{ bytes, alignment });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,39 +128,20 @@ pub fn decref(
|
||||||
|
|
||||||
const isizes: [*]isize = @ptrCast([*]isize, @alignCast(@sizeOf(isize), bytes));
|
const isizes: [*]isize = @ptrCast([*]isize, @alignCast(@sizeOf(isize), bytes));
|
||||||
|
|
||||||
const refcount = (isizes - 1)[0];
|
decref_ptr_to_refcount(isizes - 1, alignment);
|
||||||
const refcount_isize = @bitCast(isize, refcount);
|
}
|
||||||
|
|
||||||
|
inline fn decref_ptr_to_refcount(
|
||||||
|
refcount_ptr: [*]isize,
|
||||||
|
alignment: u32,
|
||||||
|
) void {
|
||||||
|
const refcount: isize = refcount_ptr[0];
|
||||||
|
const extra_bytes = std.math.max(alignment, @sizeOf(usize));
|
||||||
|
|
||||||
switch (alignment) {
|
|
||||||
16 => {
|
|
||||||
if (refcount == REFCOUNT_ONE_ISIZE) {
|
if (refcount == REFCOUNT_ONE_ISIZE) {
|
||||||
dealloc(bytes - 16, alignment);
|
dealloc(@ptrCast([*]u8, refcount_ptr) - (extra_bytes - @sizeOf(usize)), alignment);
|
||||||
} else if (refcount_isize < 0) {
|
} else if (refcount < 0) {
|
||||||
(isizes - 1)[0] = refcount - 1;
|
refcount_ptr[0] = refcount - 1;
|
||||||
}
|
|
||||||
},
|
|
||||||
8 => {
|
|
||||||
if (refcount == REFCOUNT_ONE_ISIZE) {
|
|
||||||
dealloc(bytes - 8, alignment);
|
|
||||||
} else if (refcount_isize < 0) {
|
|
||||||
(isizes - 1)[0] = refcount - 1;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
4 => {
|
|
||||||
if (refcount == REFCOUNT_ONE_ISIZE) {
|
|
||||||
dealloc(bytes - 4, alignment);
|
|
||||||
} else if (refcount_isize < 0) {
|
|
||||||
(isizes - 1)[0] = refcount - 1;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
// NOTE enums can currently have an alignment of < 8
|
|
||||||
if (refcount == REFCOUNT_ONE_ISIZE) {
|
|
||||||
dealloc(bytes - 8, alignment);
|
|
||||||
} else if (refcount_isize < 0) {
|
|
||||||
(isizes - 1)[0] = refcount - 1;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -220,18 +220,14 @@ impl<'ctx> PointerToRefcount<'ctx> {
|
||||||
|
|
||||||
let alignment = env.context.i32_type().const_int(alignment as _, false);
|
let alignment = env.context.i32_type().const_int(alignment as _, false);
|
||||||
|
|
||||||
// has to be non-zero, or the deallocation is skipped
|
|
||||||
let data_bytes = env.ptr_int().const_int(1, false);
|
|
||||||
|
|
||||||
call_void_bitcode_fn(
|
call_void_bitcode_fn(
|
||||||
env,
|
env,
|
||||||
&[
|
&[
|
||||||
env.builder.build_bitcast(
|
env.builder.build_bitcast(
|
||||||
parent.get_nth_param(0).unwrap(),
|
parent.get_nth_param(0).unwrap(),
|
||||||
env.context.i8_type().ptr_type(AddressSpace::Generic),
|
env.ptr_int().ptr_type(AddressSpace::Generic),
|
||||||
"foo",
|
"foo",
|
||||||
),
|
),
|
||||||
data_bytes.into(),
|
|
||||||
alignment.into(),
|
alignment.into(),
|
||||||
],
|
],
|
||||||
roc_builtins::bitcode::UTILS_DECREF,
|
roc_builtins::bitcode::UTILS_DECREF,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue