Created
February 6, 2025 11:30
-
-
Save andrewrk/57b55e8023bce4f3311e1b0a5bb78cd1 to your computer and use it in GitHub Desktop.
an allocator test I whipped up real quick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const Allocator = std.mem.Allocator; | |
var debug_allocator: std.heap.DebugAllocator(.{ | |
.safety = false, | |
.stack_trace_frames = 0, | |
}) = .init; | |
pub fn main() !void { | |
//const gpa = debug_allocator.allocator(); | |
const gpa = std.heap.smp_allocator; | |
var thread_pool: std.Thread.Pool = undefined; | |
try thread_pool.init(.{ | |
.allocator = gpa, | |
.n_jobs = std.Thread.getCpuCount() catch @panic("cpu count"), | |
}); | |
defer thread_pool.deinit(); | |
var wg: std.Thread.WaitGroup = .{}; | |
defer wg.wait(); | |
for (0..1000) |i| { | |
thread_pool.spawnWg(&wg, run, .{ gpa, i }); | |
} | |
} | |
fn run(gpa: Allocator, n: usize) void { | |
runFallible(gpa, n) catch @panic("OOM"); | |
} | |
fn runFallible(gpa: Allocator, n: usize) !void { | |
var numbers: std.ArrayListUnmanaged(usize) = .empty; | |
defer numbers.deinit(gpa); | |
for (0..n) |i| { | |
try numbers.append(gpa, i); | |
} | |
var strings: std.AutoArrayHashMapUnmanaged(usize, []const u8) = .empty; | |
defer { | |
for (strings.values()) |v| gpa.free(v); | |
strings.deinit(gpa); | |
} | |
for (numbers.items) |i| { | |
try strings.put(gpa, n, try std.fmt.allocPrint(gpa, "{d}", .{i})); | |
} | |
} |
Author
andrewrk
commented
Feb 6, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment