Skip to content

Commit 965dc59

Browse files
authored
Merge pull request #16 from Aandreba/cache-opt
Added cache optimizations to the internal layout of the reference counters
2 parents 58a64b6 + f87af60 commit 965dc59

File tree

4 files changed

+245
-382
lines changed

4 files changed

+245
-382
lines changed

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ test:
1212
zig test src/tests.zig
1313

1414
clean:
15-
rm -rf zig-cache
15+
rm -rf .zig-cache
1616
rm -rf zig-out

src/example.zig

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Mutex = Thread.Mutex;
66

77
const ArrayList = std.ArrayList;
88
const Arc = rc.Arc(Data);
9+
const Rc = rc.Rc(@Vector(8, f32));
910

1011
const THREADS = 8;
1112

@@ -21,19 +22,19 @@ const Data = struct {
2122
test "example" {
2223
std.debug.print("\n", .{});
2324
std.debug.print("Data size: {}\n", .{@sizeOf(Data)});
24-
std.debug.print("Heap size: {}\n\n", .{Arc.innerSize()});
25+
std.debug.print("Heap size: {}\n\n", .{Arc.total_size});
2526

2627
std.debug.print("Data align: {}\n", .{@alignOf(Data)});
27-
std.debug.print("Heap align: {}\n\n", .{Arc.innerAlign()});
28+
std.debug.print("Heap align: {}\n\n", .{Arc.internal_alignment});
2829

2930
var value = try Arc.init(std.testing.allocator, .{});
30-
errdefer value.releaseWithFn(Data.deinit, .{});
31+
errdefer if (value.releaseUnwrap()) |inner| inner.deinit();
3132

3233
var handles: [THREADS]Thread = undefined;
3334
var i: usize = 0;
3435
while (i < THREADS) {
3536
const this_value = value.retain();
36-
errdefer this_value.releaseWithFn(Data.deinit, .{});
37+
errdefer if (this_value.releaseUnwrap()) |data| data.deinit();
3738
handles[i] = try Thread.spawn(.{}, thread_exec, .{this_value});
3839
i += 1;
3940
}
@@ -45,14 +46,22 @@ test "example" {
4546
std.debug.print("{d}\n", .{owned_value.data.items});
4647
}
4748

49+
test "st_example" {
50+
std.debug.print("\n", .{});
51+
std.debug.print("Data size: {}\n", .{@sizeOf(@Vector(8, f32))});
52+
std.debug.print("Heap size: {}\n\n", .{Rc.total_size});
53+
54+
std.debug.print("Data align: {}\n", .{@alignOf(@Vector(8, f32))});
55+
std.debug.print("Heap align: {}\n\n", .{Rc.internal_alignment});
56+
}
57+
4858
fn thread_exec(data: Arc) !void {
49-
defer data.releaseWithFn(Data.deinit, .{});
59+
defer if (data.releaseUnwrap()) |inner| inner.deinit();
5060

51-
var rng = std.rand.DefaultPrng.init(@as(u64, @bitCast(@as(i64, @truncate(std.time.nanoTimestamp())))));
61+
var rng = std.Random.DefaultPrng.init(@as(u64, @bitCast(@as(i64, @truncate(std.time.nanoTimestamp())))));
5262

5363
data.value.mutex.lock();
5464
defer data.value.mutex.unlock();
55-
5665
const value = rng.random().int(u64);
5766
try data.value.data.append(value);
5867

0 commit comments

Comments
 (0)