From 7069917e6acd45f4192feaa974d99188e7547982 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 21 Jun 2022 10:32:39 -0700 Subject: [PATCH] use c allocator in release modes if available --- src/main.zig | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main.zig b/src/main.zig index 5d715b0b4..cdc3c3fb9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const options = @import("build_options"); const std = @import("std"); const glfw = @import("glfw"); @@ -8,9 +9,25 @@ const tracy = @import("tracy/tracy.zig"); const Config = @import("config.zig").Config; pub fn main() !void { - var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; - const gpa = general_purpose_allocator.allocator(); - defer _ = general_purpose_allocator.deinit(); + const gpa = gpa: { + // Use the libc allocator if it is available beacuse it is WAY + // faster than GPA. We only do this in release modes so that we + // can get easy memory leak detection in debug modes. + if (builtin.link_libc) { + switch (builtin.mode) { + .ReleaseSafe, .ReleaseFast => break :gpa std.heap.c_allocator, + else => {}, + } + } + + // We don't ever deinit our GPA because the process cleanup will + // clean it up. This defer isn't in the right location anyways because + // it'll deinit on return from blk. + // defer _ = general_purpose_allocator.deinit(); + + var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; + break :gpa general_purpose_allocator.allocator(); + }; // If we're tracing, then wrap memory so we can trace allocations const alloc = if (!tracy.enabled) gpa else tracy.allocator(gpa, null).allocator();