authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-10-03 17:37:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-04 02:55:54-07:00
logec0f76c5996e88f61d376640bf36ed7feb2b0ea6
tree1f8ca77b728ca1381fb40615c2eff9817945ff6f
parent11489bb04f82cf91e23bd1980fb4cf49b5a6fc54

GeneralPurposeAllocator.searchBucket: check current bucket before searching the list

Follow up to #17383. This is a minor optimization that only matters when a small allocation is resized/free'd soon after it is allocated. The only real difference I was able to observe with this was via a synthetic benchmark that allocates a full bucket and then frees all but one of the slots, over and over in a loop: Debug build: Benchmark 1 (9 runs): gpa-degen-master.exe measurement mean ± σ min … max outliers delta wall_time 575ms ± 5.19ms 569ms … 583ms 0 ( 0%) 0% peak_rss 43.8MB ± 1.37KB 43.8MB … 43.8MB 1 (11%) 0% Benchmark 2 (10 runs): gpa-degen-search-cur.exe measurement mean ± σ min … max outliers delta wall_time 532ms ± 5.55ms 520ms … 539ms 0 ( 0%) ⚡- 7.5% ± 0.9% peak_rss 43.8MB ± 65.2KB 43.8MB … 44.0MB 1 (10%) + 0.0% ± 0.1% ReleaseFast build: Benchmark 1 (129 runs): gpa-degen-master-release.exe measurement mean ± σ min … max outliers delta wall_time 38.9ms ± 1.12ms 36.7ms … 42.4ms 8 ( 6%) 0% peak_rss 23.2MB ± 2.39KB 23.2MB … 23.2MB 0 ( 0%) 0% Benchmark 2 (151 runs): gpa-degen-search-cur-release.exe measurement mean ± σ min … max outliers delta wall_time 33.2ms ± 999us 31.9ms … 36.3ms 20 (13%) ⚡- 14.7% ± 0.6% peak_rss 23.2MB ± 2.26KB 23.2MB … 23.2MB 0 ( 0%) + 0.0% ± 0.0%

1 files changed, 14 insertions(+), 10 deletions(-)

lib/std/heap/general_purpose_allocator.zig+14-10
......@@ -541,10 +541,14 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
541541 fn searchBucket(
542542 buckets: *Buckets,
543543 addr: usize,
544 current_bucket: ?*BucketHeader,
544545 ) ?*BucketHeader {
545 const search_page = mem.alignBackward(usize, addr, page_size);
546 const search_page: [*]align(page_size) u8 = @ptrFromInt(mem.alignBackward(usize, addr, page_size));
547 if (current_bucket != null and current_bucket.?.page == search_page) {
548 return current_bucket;
549 }
546550 var search_header: BucketHeader = undefined;
547 search_header.page = @ptrFromInt(search_page);
551 search_header.page = search_page;
548552 const entry = buckets.getEntryFor(&search_header);
549553 return if (entry.node) |node| node.key else null;
550554 }
......@@ -712,7 +716,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
712716 var bucket_index = math.log2(size_class_hint);
713717 var size_class: usize = size_class_hint;
714718 const bucket = while (bucket_index < small_bucket_count) : (bucket_index += 1) {
715 if (searchBucket(&self.buckets[bucket_index], @intFromPtr(old_mem.ptr))) |bucket| {
719 if (searchBucket(&self.buckets[bucket_index], @intFromPtr(old_mem.ptr), self.cur_buckets[bucket_index])) |bucket| {
716720 break bucket;
717721 }
718722 size_class *= 2;
......@@ -720,7 +724,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
720724 if (config.retain_metadata) {
721725 if (!self.large_allocations.contains(@intFromPtr(old_mem.ptr))) {
722726 // object not in active buckets or a large allocation, so search empty buckets
723 if (searchBucket(&self.empty_buckets, @intFromPtr(old_mem.ptr))) |bucket| {
727 if (searchBucket(&self.empty_buckets, @intFromPtr(old_mem.ptr), null)) |bucket| {
724728 // bucket is empty so is_used below will always be false and we exit there
725729 break :blk bucket;
726730 } else {
......@@ -830,7 +834,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
830834 var bucket_index = math.log2(size_class_hint);
831835 var size_class: usize = size_class_hint;
832836 const bucket = while (bucket_index < small_bucket_count) : (bucket_index += 1) {
833 if (searchBucket(&self.buckets[bucket_index], @intFromPtr(old_mem.ptr))) |bucket| {
837 if (searchBucket(&self.buckets[bucket_index], @intFromPtr(old_mem.ptr), self.cur_buckets[bucket_index])) |bucket| {
834838 break bucket;
835839 }
836840 size_class *= 2;
......@@ -838,7 +842,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
838842 if (config.retain_metadata) {
839843 if (!self.large_allocations.contains(@intFromPtr(old_mem.ptr))) {
840844 // object not in active buckets or a large allocation, so search empty buckets
841 if (searchBucket(&self.empty_buckets, @intFromPtr(old_mem.ptr))) |bucket| {
845 if (searchBucket(&self.empty_buckets, @intFromPtr(old_mem.ptr), null)) |bucket| {
842846 // bucket is empty so is_used below will always be false and we exit there
843847 break :blk bucket;
844848 } else {
......@@ -1387,10 +1391,10 @@ test "double frees" {
13871391 const index: usize = 6;
13881392 const size_class: usize = @as(usize, 1) << 6;
13891393 const small = try allocator.alloc(u8, size_class);
1390 try std.testing.expect(GPA.searchBucket(&gpa.buckets[index], @intFromPtr(small.ptr)) != null);
1394 try std.testing.expect(GPA.searchBucket(&gpa.buckets[index], @intFromPtr(small.ptr), gpa.cur_buckets[index]) != null);
13911395 allocator.free(small);
1392 try std.testing.expect(GPA.searchBucket(&gpa.buckets[index], @intFromPtr(small.ptr)) == null);
1393 try std.testing.expect(GPA.searchBucket(&gpa.empty_buckets, @intFromPtr(small.ptr)) != null);
1396 try std.testing.expect(GPA.searchBucket(&gpa.buckets[index], @intFromPtr(small.ptr), gpa.cur_buckets[index]) == null);
1397 try std.testing.expect(GPA.searchBucket(&gpa.empty_buckets, @intFromPtr(small.ptr), null) != null);
13941398
13951399 // detect a large allocation double free
13961400 const large = try allocator.alloc(u8, 2 * page_size);
......@@ -1408,7 +1412,7 @@ test "double frees" {
14081412 // check that flushing retained metadata doesn't disturb live allocations
14091413 gpa.flushRetainedMetadata();
14101414 try std.testing.expect(gpa.empty_buckets.root == null);
1411 try std.testing.expect(GPA.searchBucket(&gpa.buckets[index], @intFromPtr(normal_small.ptr)) != null);
1415 try std.testing.expect(GPA.searchBucket(&gpa.buckets[index], @intFromPtr(normal_small.ptr), gpa.cur_buckets[index]) != null);
14121416 try std.testing.expect(gpa.large_allocations.contains(@intFromPtr(normal_large.ptr)));
14131417 try std.testing.expect(!gpa.large_allocations.contains(@intFromPtr(large.ptr)));
14141418}