feat: items with arc instead of arena

This commit is contained in:
2025-05-25 10:31:58 +02:00
parent 4a9dc12b48
commit 5cfb556dff
4 changed files with 76 additions and 38 deletions

View File

@@ -1,17 +1,12 @@
pub const Tab = struct {
allocator: std.mem.Allocator,
currentLocation: ?*Container,
currentItems: locked(?std.ArrayList(*Item)),
currentItems: locked(?std.ArrayList(Arc(*Item))),
currentLocationChanged: Observable(?*Container),
currentItemsChanged: bool = false,
preCurrentItemsUnload: Observable(*Tab),
threadPool: *std.Thread.Pool,
rootProvider: *RootProvider,
_private: Private,
const Private = struct {
currentItemsAllocator: std.heap.ArenaAllocator,
};
pub fn init(
self: *Tab,
@@ -27,9 +22,6 @@ pub const Tab = struct {
.threadPool = threadPool,
.rootProvider = rootProvider,
.preCurrentItemsUnload = Observable(*Tab).init(allocator),
._private = .{
.currentItemsAllocator = std.heap.ArenaAllocator.init(allocator),
},
};
}
@@ -70,25 +62,21 @@ pub const Tab = struct {
self.preCurrentItemsUnload.notify(self);
const data = self.currentItems.data;
self.currentItems.data = null;
}
//TODO: half the capacity to prevent "leaking" after a huuuge container has been opened once
_ = self._private.currentItemsAllocator.reset(.retain_capacity);
const arena = self._private.currentItemsAllocator.allocator();
var threadSafeAllocator = std.heap.ThreadSafeAllocator{ .child_allocator = arena };
const allocator = threadSafeAllocator.allocator();
errdefer {
_ = self._private.currentItemsAllocator.reset(.free_all);
if (data) |currentItems| {
for (currentItems.items) |*item| {
if (item.releaseUnwrap()) |i| i.deinit();
}
currentItems.deinit();
}
}
{
self.currentItems.mutex.lock();
defer self.currentItems.mutex.unlock();
self.currentItems.data = std.ArrayList(*Item).init(allocator);
self.currentItems.data = std.ArrayList(Arc(*Item)).init(self.allocator);
}
errdefer {
self.currentItems.data.?.deinit();
@@ -101,7 +89,7 @@ pub const Tab = struct {
}
for (location.children.items) |item| {
const resolvedItem = location.item.provider.getItemByFullName(item, &.{ .skipChildInit = true }, allocator) catch |e| {
const resolvedItem = location.item.provider.getItemByFullName(item, &.{ .skipChildInit = true }, self.allocator) catch |e| {
//TODO: save error to container errors
std.debug.print("error {} {s}\r\n", .{ e, item.path });
continue;
@@ -110,7 +98,9 @@ pub const Tab = struct {
self.currentItems.mutex.lock();
defer self.currentItems.mutex.unlock();
try self.currentItems.data.?.append(resolvedItem);
// const arc = try Arc(*Item).init(self.allocator, resolvedItem);
// try self.currentItems.data.?.append(arc);
try self.currentItems.data.?.append(try Arc(*Item).init(self.allocator, resolvedItem));
}
{
@@ -124,7 +114,18 @@ pub const Tab = struct {
if (self.currentLocation) |c| {
c.item.deinit();
}
self._private.currentItemsAllocator.deinit();
const data = self.currentItems.data;
self.currentItems.data = null;
if (data) |currentItems| {
for (currentItems.items) |arc_item| {
if (arc_item.releaseUnwrap()) |item| {
item.deinit();
}
}
currentItems.deinit();
}
self.allocator.destroy(self);
}
};
@@ -140,3 +141,4 @@ const Item = models.Item;
const locked = @import("../sync.zig").locked;
const Observable = @import("../observable.zig").Observable;
const RootProvider = @import("../provider/root.zig").RootProvider;
const Arc = @import("zigrc").Arc;