diff --git a/src/command.zig b/src/command.zig index b3b0bf6..26a4dcc 100644 --- a/src/command.zig +++ b/src/command.zig @@ -13,10 +13,14 @@ pub const Context = struct { args: tp.message = .{}, pub fn fmt(value: anytype) Context { - return .{ .args = tp.message.fmtbuf(&context_buffer, value) catch @panic("command.Context.fmt failed") }; + context_buffer.clearRetainingCapacity(); + cbor.writeValue(context_buffer.writer(), value) catch @panic("command.Context.fmt failed"); + return .{ .args = .{ .buf = context_buffer.items } }; } }; -threadlocal var context_buffer: [tp.max_message_size]u8 = undefined; + +const context_buffer_allocator = std.heap.c_allocator; +threadlocal var context_buffer: std.ArrayList(u8) = std.ArrayList(u8).init(context_buffer_allocator); pub const fmt = Context.fmt; const Vtable = struct { diff --git a/src/syntax/src/QueryCache.zig b/src/syntax/src/QueryCache.zig index 5843e4a..5c8ec26 100644 --- a/src/syntax/src/QueryCache.zig +++ b/src/syntax/src/QueryCache.zig @@ -13,8 +13,8 @@ const Query = treez.Query; allocator: std.mem.Allocator, mutex: ?std.Thread.Mutex, -highlights: std.StringHashMapUnmanaged(CacheEntry) = .{}, -injections: std.StringHashMapUnmanaged(CacheEntry) = .{}, +highlights: std.StringHashMapUnmanaged(*CacheEntry) = .{}, +injections: std.StringHashMapUnmanaged(*CacheEntry) = .{}, ref_count: usize = 1, const CacheEntry = struct { @@ -75,11 +75,13 @@ fn release_ref_unlocked_and_maybe_destroy(self: *Self) void { while (iter_highlights.next()) |p| { self.allocator.free(p.key_ptr.*); if (p.value_ptr.*.query) |q| q.destroy(); + self.allocator.destroy(p.value_ptr.*); } var iter_injections = self.injections.iterator(); while (iter_injections.next()) |p| { self.allocator.free(p.key_ptr.*); if (p.value_ptr.*.query) |q| q.destroy(); + self.allocator.destroy(p.value_ptr.*); } self.highlights.deinit(self.allocator); self.injections.deinit(self.allocator); @@ -95,21 +97,26 @@ fn get_cache_entry(self: *Self, file_type: *const FileType, comptime query_type: .injections => &self.injections, }; - return if (hash.getPtr(file_type.name)) |entry| entry else blk: { + return if (hash.get(file_type.name)) |entry| entry else blk: { const entry_ = try hash.getOrPut(self.allocator, try self.allocator.dupe(u8, file_type.name)); - entry_.value_ptr.* = .{ + + const q = try self.allocator.create(CacheEntry); + q.* = .{ .query = null, .mutex = if (self.mutex) |_| .{} else null, .file_type = file_type, .query_type = query_type, }; - break :blk entry_.value_ptr; + entry_.value_ptr.* = q; + + break :blk q; }; } fn get_cached_query(_: *Self, entry: *CacheEntry) QueryParseError!?*Query { if (entry.mutex) |*mtx| mtx.lock(); defer if (entry.mutex) |*mtx| mtx.unlock(); + return if (entry.query) |query| query else blk: { const lang = entry.file_type.lang_fn() orelse std.debug.panic("tree-sitter parser function failed for language: {s}", .{entry.file_type.name}); entry.query = try Query.create(lang, switch (entry.query_type) {