avoid storing refcount instead load it. That optimizes beter for some reason

This commit is contained in:
Brendan Hansknecht 2025-01-01 14:03:49 -08:00
parent 7e6b004f36
commit 0d800252f3
No known key found for this signature in database
GPG key ID: 0EA784685083E75B

View file

@ -209,27 +209,26 @@ pub fn increfRcPtrC(ptr_to_refcount: *isize, amount: isize) callconv(.C) void {
}
// Ensure that the refcount is not whole program lifetime.
const refcount: isize = ptr_to_refcount.*;
if (!rcConstant(refcount)) {
if (!rcConstant(ptr_to_refcount.*)) {
// Note: we assume that a refcount will never overflow.
// As such, we do not need to cap incrementing.
switch (RC_TYPE) {
.normal => {
if (DEBUG_INCDEC and builtin.target.cpu.arch != .wasm32) {
const old = @as(usize, @bitCast(refcount));
const old = @as(usize, @bitCast(ptr_to_refcount.*));
const new = old + @as(usize, @intCast(amount));
std.debug.print("{} + {} = {}!\n", .{ old, amount, new });
}
ptr_to_refcount.* = refcount +% amount;
ptr_to_refcount.* +%= amount;
},
.atomic => {
// If the first bit of the refcount is set, this variable is atomic.
if (refcount & REFCOUNT_IS_ATOMIC_MASK != 0) {
if (ptr_to_refcount.* & REFCOUNT_IS_ATOMIC_MASK != 0) {
_ = @atomicRmw(isize, ptr_to_refcount, .Add, amount, .monotonic);
} else {
ptr_to_refcount.* = refcount +% amount;
ptr_to_refcount.* +%= amount;
}
},
.none => unreachable,
@ -372,32 +371,32 @@ inline fn decref_ptr_to_refcount(
const alignment = @max(ptr_width, element_alignment);
// Ensure that the refcount is not whole program lifetime.
const refcount: isize = refcount_ptr[0];
if (!rcConstant(refcount)) {
if (!rcConstant(refcount_ptr[0])) {
switch (RC_TYPE) {
.normal => {
const old = @as(usize, @bitCast(refcount));
refcount_ptr[0] = refcount -% 1;
const new = @as(usize, @bitCast(refcount -% 1));
const old = @as(usize, @bitCast(refcount_ptr[0]));
if (DEBUG_INCDEC and builtin.target.cpu.arch != .wasm32) {
const new = @as(usize, @bitCast(refcount_ptr[0] -% 1));
std.debug.print("{} - 1 = {}!\n", .{ old, new });
}
if (refcount == 1) {
refcount_ptr[0] -%= 1;
if (old == 1) {
free_ptr_to_refcount(refcount_ptr, alignment, elements_refcounted);
}
},
.atomic => {
// If the first bit of the refcount is set, this variable is atomic.
if (refcount & REFCOUNT_IS_ATOMIC_MASK != 0) {
if (refcount_ptr[0] & REFCOUNT_IS_ATOMIC_MASK != 0) {
const last = @atomicRmw(isize, &refcount_ptr[0], .Sub, 1, .monotonic);
if (last & REFCOUNT_VALUE_MASK == 1) {
free_ptr_to_refcount(refcount_ptr, alignment, elements_refcounted);
}
} else {
refcount_ptr[0] = refcount -% 1;
if (refcount & REFCOUNT_VALUE_MASK == 1) {
const old = @as(usize, @bitCast(refcount_ptr[0]));
refcount_ptr[0] -%= 1;
if (old & REFCOUNT_VALUE_MASK == 1) {
free_ptr_to_refcount(refcount_ptr, alignment, elements_refcounted);
}
}