DebugAllocator: only determine tty config if needed

Obtaining it is expensive and usually redundent in the case of no leaks.

Notably, between each unit test run the test runner resets the debug
allocator's state, causing this to slow it down by a notable margin.
This commit is contained in:
Kendall Condon 2025-11-03 20:59:52 -05:00
parent 35d55222cf
commit 491c862e6d

View file

@ -425,7 +425,7 @@ pub fn DebugAllocator(comptime config: Config) type {
bucket: *BucketHeader, bucket: *BucketHeader,
size_class_index: usize, size_class_index: usize,
used_bits_count: usize, used_bits_count: usize,
tty_config: std.Io.tty.Config, tty_config: *?std.Io.tty.Config,
) usize { ) usize {
const size_class = @as(usize, 1) << @as(Log2USize, @intCast(size_class_index)); const size_class = @as(usize, 1) << @as(Log2USize, @intCast(size_class_index));
const slot_count = slot_counts[size_class_index]; const slot_count = slot_counts[size_class_index];
@ -445,7 +445,10 @@ pub fn DebugAllocator(comptime config: Config) type {
addr, addr,
std.debug.FormatStackTrace{ std.debug.FormatStackTrace{
.stack_trace = stack_trace, .stack_trace = stack_trace,
.tty_config = tty_config, .tty_config = tty_config.* orelse config: {
tty_config.* = std.Io.tty.detectConfig(.stderr());
break :config tty_config.*.?;
},
}, },
}); });
leaks += 1; leaks += 1;
@ -460,14 +463,14 @@ pub fn DebugAllocator(comptime config: Config) type {
pub fn detectLeaks(self: *Self) usize { pub fn detectLeaks(self: *Self) usize {
var leaks: usize = 0; var leaks: usize = 0;
const tty_config = std.Io.tty.detectConfig(.stderr()); var tty_config: ?std.Io.tty.Config = null;
for (self.buckets, 0..) |init_optional_bucket, size_class_index| { for (self.buckets, 0..) |init_optional_bucket, size_class_index| {
var optional_bucket = init_optional_bucket; var optional_bucket = init_optional_bucket;
const slot_count = slot_counts[size_class_index]; const slot_count = slot_counts[size_class_index];
const used_bits_count = usedBitsCount(slot_count); const used_bits_count = usedBitsCount(slot_count);
while (optional_bucket) |bucket| { while (optional_bucket) |bucket| {
leaks += detectLeaksInBucket(bucket, size_class_index, used_bits_count, tty_config); leaks += detectLeaksInBucket(bucket, size_class_index, used_bits_count, &tty_config);
optional_bucket = bucket.prev; optional_bucket = bucket.prev;
} }
} }
@ -480,7 +483,10 @@ pub fn DebugAllocator(comptime config: Config) type {
@intFromPtr(large_alloc.bytes.ptr), @intFromPtr(large_alloc.bytes.ptr),
std.debug.FormatStackTrace{ std.debug.FormatStackTrace{
.stack_trace = stack_trace, .stack_trace = stack_trace,
.tty_config = tty_config, .tty_config = tty_config orelse config: {
tty_config = std.Io.tty.detectConfig(.stderr());
break :config tty_config.?;
},
}, },
}); });
leaks += 1; leaks += 1;