diff options
-rw-r--r-- | src/install/npm.zig | 46 | ||||
-rw-r--r-- | src/javascript/jsc/webcore/response.zig | 4 | ||||
-rw-r--r-- | src/pool.zig | 37 |
3 files changed, 26 insertions, 61 deletions
diff --git a/src/install/npm.zig b/src/install/npm.zig index 9d12255d4..a6cded5dc 100644 --- a/src/install/npm.zig +++ b/src/install/npm.zig @@ -26,51 +26,7 @@ const SlicedString = Semver.SlicedString; const FileSystem = @import("../fs.zig").FileSystem; const VersionSlice = @import("./install.zig").VersionSlice; -fn ObjectPool(comptime Type: type, comptime Init: (fn (allocator: *std.mem.Allocator) anyerror!Type), comptime threadsafe: bool) type { - return struct { - const LinkedList = std.SinglyLinkedList(Type); - const Data = if (threadsafe) - struct { - pub threadlocal var list: LinkedList = undefined; - pub threadlocal var loaded: bool = false; - } - else - struct { - pub var list: LinkedList = undefined; - pub var loaded: bool = false; - }; - - const data = Data; - - pub fn get(allocator: *std.mem.Allocator) *LinkedList.Node { - if (data.loaded) { - if (data.list.popFirst()) |node| { - node.data.reset(); - return node; - } - } - - var new_node = allocator.create(LinkedList.Node) catch unreachable; - new_node.* = LinkedList.Node{ - .data = Init( - allocator, - ) catch unreachable, - }; - - return new_node; - } - - pub fn release(node: *LinkedList.Node) void { - if (data.loaded) { - data.list.prepend(node); - return; - } - - data.list = LinkedList{ .first = node }; - data.loaded = true; - } - }; -} +const ObjectPool = @import("../pool.zig").ObjectPool; const Npm = @This(); diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig index 248c3a4a7..dfb994492 100644 --- a/src/javascript/jsc/webcore/response.zig +++ b/src/javascript/jsc/webcore/response.zig @@ -430,8 +430,8 @@ pub const Fetch = struct { reject: js.JSObjectRef = null, context: FetchTaskletContext = undefined, - const Pool = ObjectPool(FetchTasklet, init); - const BodyPool = ObjectPool(MutableString, MutableString.init2048); + const Pool = ObjectPool(FetchTasklet, init, true); + const BodyPool = ObjectPool(MutableString, MutableString.init2048, true); pub const FetchTaskletContext = struct { tasklet: *FetchTasklet, }; diff --git a/src/pool.zig b/src/pool.zig index f37b62162..45934b98e 100644 --- a/src/pool.zig +++ b/src/pool.zig @@ -1,23 +1,32 @@ const std = @import("std"); -pub fn ObjectPool(comptime Type: type, comptime Init: (fn (allocator: *std.mem.Allocator) anyerror!Type)) type { +pub fn ObjectPool(comptime Type: type, comptime Init: (fn (allocator: *std.mem.Allocator) anyerror!Type), comptime threadsafe: bool) type { return struct { const LinkedList = std.SinglyLinkedList(Type); - // mimalloc crashes on realloc across threads - threadlocal var list: LinkedList = undefined; - threadlocal var loaded: bool = false; + const Data = if (threadsafe) + struct { + pub threadlocal var list: LinkedList = undefined; + pub threadlocal var loaded: bool = false; + } + else + struct { + pub var list: LinkedList = undefined; + pub var loaded: bool = false; + }; + const data = Data; pub const Node = LinkedList.Node; - pub fn get(allocator: *std.mem.Allocator) *Node { - if (loaded) { - if (list.popFirst()) |node| { + + pub fn get(allocator: *std.mem.Allocator) *LinkedList.Node { + if (data.loaded) { + if (data.list.popFirst()) |node| { node.data.reset(); return node; } } - var new_node = allocator.create(Node) catch unreachable; - new_node.* = Node{ + var new_node = allocator.create(LinkedList.Node) catch unreachable; + new_node.* = LinkedList.Node{ .data = Init( allocator, ) catch unreachable, @@ -26,14 +35,14 @@ pub fn ObjectPool(comptime Type: type, comptime Init: (fn (allocator: *std.mem.A return new_node; } - pub fn release(node: *Node) void { - if (loaded) { - list.prepend(node); + pub fn release(node: *LinkedList.Node) void { + if (data.loaded) { + data.list.prepend(node); return; } - list = LinkedList{ .first = node }; - loaded = true; + data.list = LinkedList{ .first = node }; + data.loaded = true; } }; } |