Change default type to err and use ensureTotalCapacity to reduce allocations

This commit is contained in:
Fabian Schmalzried 2025-10-15 09:41:04 +00:00 committed by GitHub
parent 29dc0fefb1
commit abbcff84a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 4 deletions

View file

@ -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);
}
}

View file

@ -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 };

View file

@ -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);