improve memory allocations (#889)

* improve memory allocations

* Update src/main.zig

Co-authored-by: erikarvstedt <36110478+erikarvstedt@users.noreply.github.com>

* add missing 0x21

Co-authored-by: erikarvstedt <36110478+erikarvstedt@users.noreply.github.com>
This commit is contained in:
Techatrix 2023-01-09 16:09:36 +00:00 committed by GitHub
parent 21b103c158
commit 4423a5face
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -1334,7 +1334,7 @@ fn completeFieldAccess(server: *Server, handle: *const DocumentStore.Handle, sou
return try completions.toOwnedSlice(allocator); return try completions.toOwnedSlice(allocator);
} }
fn formatDetailledLabel(item: *types.CompletionItem, alloc: std.mem.Allocator) error{OutOfMemory}!void { fn formatDetailledLabel(item: *types.CompletionItem, arena: std.mem.Allocator) error{OutOfMemory}!void {
// NOTE: this is not ideal, we should build a detailled label like we do for label/detail // NOTE: this is not ideal, we should build a detailled label like we do for label/detail
// because this implementation is very loose, nothing is formated properly so we need to clean // because this implementation is very loose, nothing is formated properly so we need to clean
// things a little bit, wich is quite messy // things a little bit, wich is quite messy
@ -1345,7 +1345,7 @@ fn formatDetailledLabel(item: *types.CompletionItem, alloc: std.mem.Allocator) e
return; return;
var detailLen: usize = item.detail.?.len; var detailLen: usize = item.detail.?.len;
var it: []u8 = try alloc.alloc(u8, detailLen); var it: []u8 = try arena.alloc(u8, detailLen);
detailLen -= std.mem.replace(u8, item.detail.?, " ", " ", it) * 3; detailLen -= std.mem.replace(u8, item.detail.?, " ", " ", it) * 3;
it = it[0..detailLen]; it = it[0..detailLen];
@ -1695,7 +1695,7 @@ fn initializeHandler(server: *Server, request: types.InitializeParams) Error!typ
// so we can now format the prebuilt builtins items for labelDetails // so we can now format the prebuilt builtins items for labelDetails
if (server.client_capabilities.label_details_support) { if (server.client_capabilities.label_details_support) {
for (server.builtin_completions.items) |*item| { for (server.builtin_completions.items) |*item| {
try formatDetailledLabel(item, std.heap.page_allocator); try formatDetailledLabel(item, server.arena.allocator());
} }
} }

View File

@ -48,9 +48,18 @@ fn loop(
var buffered_writer = std.io.bufferedWriter(std_out); var buffered_writer = std.io.bufferedWriter(std_out);
const writer = buffered_writer.writer(); const writer = buffered_writer.writer();
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
while (true) { while (true) {
var arena = std.heap.ArenaAllocator.init(server.allocator); defer {
defer arena.deinit(); // Mom, can we have garbage collection?
// No, we already have garbage collection at home.
// at home:
if (arena.queryCapacity() > 128 * 1024) {
_ = arena.reset(.free_all);
}
}
// write server -> client messages // write server -> client messages
for (server.outgoing_messages.items) |outgoing_message| { for (server.outgoing_messages.items) |outgoing_message| {
@ -340,7 +349,7 @@ const stack_frames = switch (zig_builtin.mode) {
pub fn main() !void { pub fn main() !void {
var gpa_state = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = stack_frames }){}; var gpa_state = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = stack_frames }){};
defer _ = gpa_state.deinit(); defer std.debug.assert(!gpa_state.deinit());
var tracy_state = if (tracy.enable_allocation) tracy.tracyAllocator(gpa_state.allocator()) else void{}; var tracy_state = if (tracy.enable_allocation) tracy.tracyAllocator(gpa_state.allocator()) else void{};
const inner_allocator: std.mem.Allocator = if (tracy.enable_allocation) tracy_state.allocator() else gpa_state.allocator(); const inner_allocator: std.mem.Allocator = if (tracy.enable_allocation) tracy_state.allocator() else gpa_state.allocator();