Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created February 6, 2025 11:30
Show Gist options
  • Save andrewrk/57b55e8023bce4f3311e1b0a5bb78cd1 to your computer and use it in GitHub Desktop.
Save andrewrk/57b55e8023bce4f3311e1b0a5bb78cd1 to your computer and use it in GitHub Desktop.
an allocator test I whipped up real quick
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}));
}
}
@andrewrk
Copy link
Author

andrewrk commented Feb 6, 2025

Benchmark 1 (737 runs): ./test-CAllocator
  measurement          mean ± σ            min … max           outliers         delta
  wall_time          6.75ms ±  533us    5.29ms … 9.14ms          9 ( 1%)        0%
  peak_rss           15.3MB ±  836KB    12.7MB … 17.4MB          4 ( 1%)        0%
  cpu_cycles         77.9M  ± 5.44M     59.8M  … 96.7M           9 ( 1%)        0%
  instructions        274M  ± 35.1K      274M  …  274M          56 ( 8%)        0%
  cache_references    770K  ± 59.6K      653K  … 1.21M          64 ( 9%)        0%
  cache_misses       39.3K  ± 5.14K     28.1K  … 69.0K          27 ( 4%)        0%
  branch_misses      57.9K  ± 2.86K     50.8K  … 68.5K          19 ( 3%)        0%
Benchmark 2 (1033 runs): ./test-SmpAllocator
  measurement          mean ± σ            min … max           outliers         delta
  wall_time          4.82ms ±  313us    4.16ms … 6.81ms         45 ( 4%)        ⚡- 28.7% ±  0.6%
  peak_rss           6.04MB ±  751KB    3.93MB … 8.39MB          1 ( 0%)        ⚡- 60.5% ±  0.5%
  cpu_cycles         57.5M  ± 3.50M     45.6M  … 67.5M           2 ( 0%)        ⚡- 26.2% ±  0.5%
  instructions        182M  ± 5.10K      182M  …  182M          30 ( 3%)        ⚡- 33.7% ±  0.0%
  cache_references    548K  ± 29.9K      469K  …  688K          32 ( 3%)        ⚡- 28.9% ±  0.5%
  cache_misses       28.0K  ± 3.50K     18.4K  … 45.3K           8 ( 1%)        ⚡- 28.9% ±  1.0%
  branch_misses      25.2K  ±  817      22.8K  … 29.3K          18 ( 2%)        ⚡- 56.5% ±  0.3%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment