diff --git a/src/check/Check.zig b/src/check/Check.zig index 057fddf800..28a448bab3 100644 --- a/src/check/Check.zig +++ b/src/check/Check.zig @@ -164,8 +164,9 @@ pub inline fn debugAssertArraysInSync(self: *const Self) void { inline fn ensureTypeStoreIsFilled(self: *Self) Allocator.Error!void { const region_nodes: usize = @intCast(self.regions.len()); const type_nodes: usize = @intCast(self.types.len()); + try self.types.ensureTotalCapacity(region_nodes); for (type_nodes..region_nodes) |_| { - _ = try self.types.fresh(); + _ = self.types.appendFromContentAssumeCapacity(.err); } } diff --git a/src/collections/safe_list.zig b/src/collections/safe_list.zig index 7bab56e26f..7fe88e0b7a 100644 --- a/src/collections/safe_list.zig +++ b/src/collections/safe_list.zig @@ -200,6 +200,14 @@ pub fn SafeList(comptime T: type) type { return @enumFromInt(@as(u32, @intCast(length))); } + /// Add a new item to the end of this list assuming cpacity is sufficient to hold an additional item. + pub fn appendAssumeCapacity(self: *SafeList(T), item: T) Idx { + const length = self.len(); + self.items.appendAssumeCapacity(item); + + return @enumFromInt(@as(u32, @intCast(length))); + } + /// Create a range from the provided idx to the end of the list pub fn rangeToEnd(self: *SafeList(T), start_int: u32) Range { const len_int = self.len(); @@ -432,6 +440,14 @@ pub fn SafeMultiList(comptime T: type) type { return @enumFromInt(@as(u32, @intCast(length))); } + /// Add a new item to the end of this list assuming cpacity is sufficient to hold an additional item. + pub fn appendAssumeCapacity(self: *SafeMultiList(T), item: T) Idx { + const length = self.len(); + self.items.appendAssumeCapacity(item); + + return @enumFromInt(@as(u32, @intCast(length))); + } + pub fn appendSlice(self: *SafeMultiList(T), gpa: Allocator, elems: []const T) std.mem.Allocator.Error!Range { if (elems.len == 0) { return .{ .start = .zero, .count = 0 }; diff --git a/src/types/store.zig b/src/types/store.zig index aff8253c25..2ffa5a151f 100644 --- a/src/types/store.zig +++ b/src/types/store.zig @@ -184,6 +184,13 @@ pub const Store = struct { } } + /// Create a new variable with the provided content assuming there is capacity + pub fn appendFromContentAssumeCapacity(self: *Self, content: Content) Var { + const desc_idx = self.descs.appendAssumeCapacity(.{ .content = content, .rank = Rank.top_level, .mark = Mark.none }); + const slot_idx = self.slots.appendAssumeCapacity(.{ .root = desc_idx }); + return Self.slotIdxToVar(slot_idx); + } + // setting variables // /// Set a type variable to the provided content @@ -964,9 +971,9 @@ const SlotStore = struct { return @enumFromInt(@intFromEnum(safe_idx)); } - /// Insert a value into the store - fn appendAssumeCapacity(self: *Self, gpa: Allocator, typ: Slot) std.mem.Allocator.Error!Idx { - const safe_idx = try self.backing.append(gpa, typ); + /// Insert a value into the store assuming there is capacity + fn appendAssumeCapacity(self: *Self, typ: Slot) Idx { + const safe_idx = self.backing.appendAssumeCapacity(typ); return @enumFromInt(@intFromEnum(safe_idx)); } @@ -1068,6 +1075,12 @@ const DescStore = struct { return @enumFromInt(@intFromEnum(safe_idx)); } + /// Appends a value to the store assuming there is capacity + fn appendAssumeCapacity(self: *Self, typ: Desc) Idx { + const safe_idx = self.backing.appendAssumeCapacity(typ); + return @enumFromInt(@intFromEnum(safe_idx)); + } + /// Set a value in the store fn set(self: *Self, idx: Idx, val: Desc) void { self.backing.set(@enumFromInt(@intFromEnum(idx)), val);