Skip to content

Commit

Permalink
fix: close memory leak with testing allocator (#16)
Browse files Browse the repository at this point in the history
* pass allocator by value, deinit after run

* initialize start time with nanoseconds

* use std.testing.allocator
  • Loading branch information
FObersteiner authored Nov 17, 2023
1 parent 204f746 commit 0bfd057
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
19 changes: 4 additions & 15 deletions examples/basic.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
const std = @import("std");
const zbench = @import("zbench");

pub const CustomAllocator = struct {
allocator: std.mem.Allocator,

pub fn create() CustomAllocator {
return CustomAllocator{ .allocator = std.heap.page_allocator };
}

pub fn as_mut(self: *CustomAllocator) *std.mem.Allocator {
return &self.allocator;
}
};
const test_allocator = std.testing.allocator;

fn helloWorld() []const u8 {
var result: usize = 0;
Expand All @@ -29,13 +18,13 @@ fn myBenchmark(_: *zbench.Benchmark) void {
}

test "bench test basic" {
var customAllocator = CustomAllocator.create();
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(customAllocator.allocator);
var bench = try zbench.Benchmark.init("My Benchmark", customAllocator.as_mut());
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var bench = try zbench.Benchmark.init("My Benchmark", test_allocator);

var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
defer benchmarkResults.results.deinit();

try zbench.run(myBenchmark, &bench, &benchmarkResults);
}
19 changes: 4 additions & 15 deletions examples/bubble_sort.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
const std = @import("std");
const inc = @import("include");
const zbench = @import("zbench");

pub const CustomAllocator = struct {
allocator: std.mem.Allocator,

pub fn create() CustomAllocator {
return CustomAllocator{ .allocator = std.heap.page_allocator };
}

pub fn as_mut(self: *CustomAllocator) *std.mem.Allocator {
return &self.allocator;
}
};
const test_allocator = std.testing.allocator;

fn bubbleSort(nums: []i32) void {
var i: usize = nums.len - 1;
Expand All @@ -34,11 +23,11 @@ fn myBenchmark(_: *zbench.Benchmark) void {
}

test "bench test bubbleSort" {
var customAllocator = CustomAllocator.create();
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(customAllocator.allocator);
var bench = try zbench.Benchmark.init("Bubble Sort Benchmark", customAllocator.as_mut());
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(test_allocator);
var bench = try zbench.Benchmark.init("Bubble Sort Benchmark", test_allocator);
var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
defer benchmarkResults.results.deinit();
try zbench.run(myBenchmark, &bench, &benchmarkResults);
}
13 changes: 7 additions & 6 deletions zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ pub const Benchmark = struct {
maxDuration: u64 = 0,
totalDuration: u64 = 0,
durations: std.ArrayList(u64),
allocator: *std.mem.Allocator,
allocator: std.mem.Allocator,
startTime: u64,

pub fn init(name: []const u8, allocator: *std.mem.Allocator) !Benchmark {
var startTime: u64 = @intCast(std.time.microTimestamp());
pub fn init(name: []const u8, allocator: std.mem.Allocator) !Benchmark {
var startTime: u64 = @intCast(std.time.nanoTimestamp());
if (startTime < 0) {
std.debug.warn("Failed to get start time. Defaulting to 0.\n", .{});
startTime = 0;
}

var bench = Benchmark{
.name = name,
.timer = t.Timer{ .startTime = startTime },
.durations = std.ArrayList(u64).init(allocator.*),
.allocator = allocator,
.timer = t.Timer{ .startTime = startTime },
.durations = std.ArrayList(u64).init(allocator),
.startTime = startTime,
};
bench.timer.start();
Expand Down Expand Up @@ -58,7 +58,7 @@ pub const Benchmark = struct {
self.maxDuration = 0;
self.totalDuration = 0;
self.durations.deinit();
self.durations = std.ArrayList(u64).init(self.allocator.*);
self.durations = std.ArrayList(u64).init(self.allocator);
}

// Function to get elapsed time since benchmark start
Expand Down Expand Up @@ -217,6 +217,7 @@ pub const BenchmarkResults = struct {
};

pub fn run(comptime func: BenchFunc, bench: *Benchmark, benchResult: *BenchmarkResults) !void {
defer bench.durations.deinit();
const MIN_DURATION = 1_000_000; // minimum benchmark time in nanoseconds (1 millisecond)
const MAX_N = 10000; // maximum number of executions for the final benchmark run
const MAX_ITERATIONS = 10; // Define a maximum number of iterations
Expand Down

0 comments on commit 0bfd057

Please sign in to comment.