From 3a4a7d2ca378862dd6b31678a143315a9e306f8c Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Sun, 11 Jan 2026 13:31:22 +0000 Subject: [PATCH] Sema: minor cleanup (the second) --- src/Air.zig | 44 -------------------------------------------- src/Sema.zig | 30 ++++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/src/Air.zig b/src/Air.zig index d5a2fa3e10c57f17de6bb1333e845a6146293986..b5cb950d493278244cb5cc9e859d6a69c5aaf338 100644 --- a/src/Air.zig +++ b/src/Air.zig @@ -1339,50 +1339,6 @@ pub const SwitchBr = struct { ranges_len: u32, body_len: u32, }; - - pub const BranchHints = struct { - bags: std.ArrayList(u32), - count: u32, - - const hints_per_bag = 10; - const hint_bits = @bitSizeOf(std.builtin.BranchHint); - - pub const empty: BranchHints = .{ - .bags = .empty, - .count = 0, - }; - - pub fn initCapacity(gpa: std.mem.Allocator, num: u32) std.mem.Allocator.Error!BranchHints { - const bags_required = std.math.divCeil(u32, num, hints_per_bag) catch unreachable; - const bags: std.ArrayList(u32) = try .initCapacity(gpa, bags_required); - return .{ .bags = bags, .count = 0 }; - } - - pub fn ensureUnusedCapacity(hints: *BranchHints, gpa: std.mem.Allocator, additional_count: u32) std.mem.Allocator.Error!void { - const unused_hints = hints.bags.capacity * hints_per_bag - hints.count; - if (unused_hints >= additional_count) return; - const bags_required = std.math.divCeil(u32, hints.count + additional_count, hints_per_bag) catch unreachable; - return hints.bags.ensureUnusedCapacity(gpa, bags_required); - } - - pub fn appendAssumeCapacity(hints: *BranchHints, hint: std.builtin.BranchHint) void { - const idx_in_bag = hints.count % hints_per_bag; - var bag: u32 = if (idx_in_bag > 0) hints.bags.pop().? else 0; - bag |= @as(u32, @intFromEnum(hint)) << @intCast(hint_bits * idx_in_bag); - hints.count += 1; - return hints.bags.appendAssumeCapacity(bag); - } - - pub fn append(hints: *BranchHints, gpa: std.mem.Allocator, hint: std.builtin.BranchHint) std.mem.Allocator.Error!void { - try hints.ensureUnusedCapacity(gpa, 1); - return hints.appendAssumeCapacity(hint); - } - - pub fn deinit(hints: *BranchHints, gpa: std.mem.Allocator) void { - hints.bags.deinit(gpa); - hints.* = undefined; - } - }; }; /// This data is stored inside extra. Trailing: diff --git a/src/Sema.zig b/src/Sema.zig index 642d7768a694159337deaf043475f698d59e775a..ceb407732551b4b27275515afe7d0af0d84812af 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -11108,11 +11108,37 @@ fn finishSwitchBr( const estimated_cases_len: u32 = scalar_cases_len + multi_cases_len + @intFromBool(has_else or has_under); + const BranchHints = struct { + bags: std.ArrayList(u32), + count: u32, + const hints_per_bag = 10; + fn ensureUnusedCapacity(hints: *@This(), gpa_inner: Allocator, additional_count: u32) Allocator.Error!void { + const unused_hints = hints.bags.capacity * hints_per_bag - hints.count; + if (unused_hints >= additional_count) return; + const bags_required = std.math.divCeil(u32, hints.count + additional_count, hints_per_bag) catch unreachable; + return hints.bags.ensureUnusedCapacity(gpa_inner, bags_required); + } + fn appendAssumeCapacity(hints: *@This(), hint: std.builtin.BranchHint) void { + const idx_in_bag = hints.count % hints_per_bag; + var bag: u32 = if (idx_in_bag > 0) hints.bags.pop().? else 0; + bag |= @as(u32, @intFromEnum(hint)) << @intCast(@bitSizeOf(std.builtin.BranchHint) * idx_in_bag); + hints.count += 1; + return hints.bags.appendAssumeCapacity(bag); + } + fn append(hints: *@This(), gpa_inner: Allocator, hint: std.builtin.BranchHint) Allocator.Error!void { + try hints.ensureUnusedCapacity(gpa_inner, 1); + return hints.appendAssumeCapacity(hint); + } + }; + var branch_hints: BranchHints = hints: { + const num_bags = std.math.divCeil(u32, estimated_cases_len, BranchHints.hints_per_bag) catch unreachable; + break :hints .{ .bags = try .initCapacity(gpa, num_bags), .count = 0 }; + }; + defer branch_hints.bags.deinit(gpa); + var cases_extra: std.ArrayList(u32) = try .initCapacity(gpa, estimated_cases_len * @typeInfo(Air.SwitchBr.Case).@"struct".fields.len); defer cases_extra.deinit(gpa); - var branch_hints: Air.SwitchBr.BranchHints = try .initCapacity(gpa, estimated_cases_len); - defer branch_hints.deinit(gpa); // We will reuse this block for each case. var case_block = child_block.makeSubBlock(); -- 2.54.0