api: remove custom allocator creation
Closed this issue ยท 5 comments
Below is the current way of using zBench with a custom allocator:
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;
}
};
...
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());
// ...
}
A more streamlined approach could be having zBench automatically adjust the custom allocator, reducing the manual setup needed by the user. This could potentially be implemented by having a default allocator within zBench, with an option for users to override it if necessary.
Proposed Improved Example:
const std = @import("std");
const zbench = @import("zbench");
...
test "bench test bubbleSort" {
var bench = try zbench.Benchmark.init("Bubble Sort Benchmark");
var resultsAlloc = std.ArrayList(zbench.BenchmarkResult).init(bench.allocator());
var benchmarkResults = zbench.BenchmarkResults{
.results = resultsAlloc,
};
try zbench.run(myBenchmark, &bench, &benchmarkResults);
}
In this revised example, zbench.Benchmark.init
would handle the allocator adjustment internally, removing the need for users to define and manage a custom allocator explicitly. This simplifies the API and makes zBench more accessible while retaining the flexibility for users who wish to use a custom allocator.
Context:
Automating the allocator adjustment could potentially lower the barrier of entry for new users and simplify the API, making it easier to use and understand. This enhancement could contribute to a more user-friendly and efficient benchmarking experience with zBench.
Hey, I've noticed that there is a memory leak if I swap out the CustomAllocator
for the generally recommended std.testing.allocator
for use in tests. Is there a way zbench could support the testing allocator?
@jasperdunn @hendriknielaender I think we're not far off from making that work. IIUC there are two deinit
steps required;
- durations array in the benchmark structure
- results array in the test
Whilst we're at it, we should think about passing the allocator directly as argument, instead of by reference.
see #16 - note that this way would still allow (or force...) the user to control the allocation.
Hey @jasperdunn,
Thank you for bringing this up. The usage of std.testing.allocator
in the context of zbench tests should now be more straightforward, and users have clear control over the allocation process as intended, with the recent merge of the PR.
Fantastic!
closed #16