authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-29 22:33:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:57-07:00
log66f83f27a2904180bae7797a7c87c6eddc7eebff
treebd3ef82451b2f04d720d84a9ed44c32d54c78455
parent27f1ad8afde86c8f734deeb05f5c8cad910275e4

InternPool: avoid indexToKey recursion for type_enum_auto

Recursion makes this hot function more difficult to profile and optimize. This commit adds the integer tag type to the type_enum_auto encoding even though the integer tag type can be inferred based on the number of fields of the enum. This avoids a call to getAssumeExists of the integer tag type inside indexToKey.

1 files changed, 15 insertions(+), 18 deletions(-)

src/InternPool.zig+15-18
......@@ -2169,6 +2169,9 @@ pub const EnumAuto = struct {
21692169 decl: Module.Decl.Index,
21702170 /// This may be `none` if there are no declarations.
21712171 namespace: Module.Namespace.OptionalIndex,
2172 /// An integer type which is used for the numerical value of the enum, which
2173 /// was inferred by Zig based on the number of tags.
2174 int_tag_type: Index,
21722175 fields_len: u32,
21732176 /// Maps field names to declaration index.
21742177 names_map: MapIndex,
......@@ -2553,7 +2556,7 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
25532556 return .{ .enum_type = .{
25542557 .decl = enum_auto.data.decl,
25552558 .namespace = enum_auto.data.namespace,
2556 .tag_ty = ip.getEnumIntTagType(enum_auto.data.fields_len),
2559 .tag_ty = enum_auto.data.int_tag_type,
25572560 .names = names,
25582561 .values = &.{},
25592562 .tag_mode = .auto,
......@@ -2928,14 +2931,6 @@ fn indexToKeyFuncType(ip: InternPool, data: u32) Key.FuncType {
29282931 };
29292932}
29302933
2931/// Asserts the integer tag type is already present in the InternPool.
2932fn getEnumIntTagType(ip: InternPool, fields_len: u32) Index {
2933 return ip.getAssumeExists(.{ .int_type = .{
2934 .bits = if (fields_len == 0) 0 else std.math.log2_int_ceil(u32, fields_len),
2935 .signedness = .unsigned,
2936 } });
2937}
2938
29392934fn indexToKeyEnum(ip: InternPool, data: u32, tag_mode: Key.EnumType.TagMode) Key {
29402935 const enum_explicit = ip.extraDataTrail(EnumExplicit, data);
29412936 const names = @ptrCast(
......@@ -3222,6 +3217,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
32223217 .data = ip.addExtraAssumeCapacity(EnumAuto{
32233218 .decl = enum_type.decl,
32243219 .namespace = enum_type.namespace,
3220 .int_tag_type = enum_type.tag_ty,
32253221 .names_map = names_map,
32263222 .fields_len = fields_len,
32273223 }),
......@@ -4000,18 +3996,18 @@ pub fn getIncompleteEnum(
40003996 }
40013997}
40023998
4003pub fn getIncompleteEnumAuto(
3999fn getIncompleteEnumAuto(
40044000 ip: *InternPool,
40054001 gpa: Allocator,
40064002 enum_type: Key.IncompleteEnumType,
40074003) Allocator.Error!IncompleteEnumType {
4008 // Although the integer tag type will not be stored in the `EnumAuto` struct,
4009 // `InternPool` logic depends on it being present so that `typeOf` can be infallible.
4010 // Ensure it is present here:
4011 _ = try ip.get(gpa, .{ .int_type = .{
4012 .bits = if (enum_type.fields_len == 0) 0 else std.math.log2_int_ceil(u32, enum_type.fields_len),
4013 .signedness = .unsigned,
4014 } });
4004 const int_tag_type = if (enum_type.tag_ty != .none)
4005 enum_type.tag_ty
4006 else
4007 try ip.get(gpa, .{ .int_type = .{
4008 .bits = if (enum_type.fields_len == 0) 0 else std.math.log2_int_ceil(u32, enum_type.fields_len),
4009 .signedness = .unsigned,
4010 } });
40154011
40164012 // We must keep the map in sync with `items`. The hash and equality functions
40174013 // for enum types only look at the decl field, which is present even in
......@@ -4029,6 +4025,7 @@ pub fn getIncompleteEnumAuto(
40294025 const extra_index = ip.addExtraAssumeCapacity(EnumAuto{
40304026 .decl = enum_type.decl,
40314027 .namespace = enum_type.namespace,
4028 .int_tag_type = int_tag_type,
40324029 .names_map = names_map,
40334030 .fields_len = enum_type.fields_len,
40344031 });
......@@ -4040,7 +4037,7 @@ pub fn getIncompleteEnumAuto(
40404037 ip.extra.appendNTimesAssumeCapacity(@enumToInt(Index.none), enum_type.fields_len);
40414038 return .{
40424039 .index = @intToEnum(Index, ip.items.len - 1),
4043 .tag_ty_index = undefined,
4040 .tag_ty_index = extra_index + std.meta.fieldIndex(EnumAuto, "int_tag_type").?,
40444041 .names_map = names_map,
40454042 .names_start = extra_index + extra_fields_len,
40464043 .values_map = .none,