Commit 52ec121469

Jacob Young <jacobly0@users.noreply.github.com>
2023-06-20 19:48:19
Sema: optimize callers of `indexToKey`
1 parent d7bd4f3
src/Air.zig
@@ -1406,7 +1406,7 @@ pub fn typeOfIndex(air: *const Air, inst: Air.Inst.Index, ip: *const InternPool)
 
         .call, .call_always_tail, .call_never_tail, .call_never_inline => {
             const callee_ty = air.typeOf(datas[inst].pl_op.operand, ip);
-            return callee_ty.fnReturnTypeIp(ip);
+            return ip.funcReturnType(callee_ty.toIntern()).toType();
         },
 
         .slice_elem_val, .ptr_elem_val, .array_elem_val => {
src/InternPool.zig
@@ -5694,11 +5694,26 @@ pub fn aggregateTypeLenIncludingSentinel(ip: *const InternPool, ty: Index) u64 {
     };
 }
 
+pub fn funcReturnType(ip: *const InternPool, ty: Index) Index {
+    const item = ip.items.get(@intFromEnum(ty));
+    const child_item = switch (item.tag) {
+        .type_pointer => ip.items.get(ip.extra.items[
+            item.data + std.meta.fieldIndex(Tag.TypePointer, "child").?
+        ]),
+        .type_function => item,
+        else => unreachable,
+    };
+    assert(child_item.tag == .type_function);
+    return @enumFromInt(Index, ip.extra.items[
+        child_item.data + std.meta.fieldIndex(TypeFunction, "return_type").?
+    ]);
+}
+
 pub fn isNoReturn(ip: *const InternPool, ty: Index) bool {
     return switch (ty) {
         .noreturn_type => true,
-        else => switch (ip.indexToKey(ty)) {
-            .error_set_type => |error_set_type| error_set_type.names.len == 0,
+        else => switch (ip.items.items(.tag)[@intFromEnum(ty)]) {
+            .type_error_set => ip.extra.items[ip.items.items(.data)[@intFromEnum(ty)] + std.meta.fieldIndex(ErrorSet, "names_len").?] == 0,
             else => false,
         },
     };
src/Sema.zig
@@ -33817,18 +33817,25 @@ pub fn resolveTypeFields(sema: *Sema, ty: Type) CompileError!Type {
         .call_modifier_type => return sema.getBuiltinType("CallModifier"),
         .prefetch_options_type => return sema.getBuiltinType("PrefetchOptions"),
 
-        _ => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
-            .struct_type => |struct_type| {
-                const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return ty;
-                try sema.resolveTypeFieldsStruct(ty, struct_obj);
-                return ty;
-            },
-            .union_type => |union_type| {
-                const union_obj = mod.unionPtr(union_type.index);
-                try sema.resolveTypeFieldsUnion(ty, union_obj);
-                return ty;
+        _ => switch (mod.intern_pool.items.items(.tag)[@intFromEnum(ty.toIntern())]) {
+            .type_struct,
+            .type_struct_ns,
+            .type_union_tagged,
+            .type_union_untagged,
+            .type_union_safety,
+            => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
+                .struct_type => |struct_type| {
+                    const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return ty;
+                    try sema.resolveTypeFieldsStruct(ty, struct_obj);
+                    return ty;
+                },
+                .union_type => |union_type| {
+                    const union_obj = mod.unionPtr(union_type.index);
+                    try sema.resolveTypeFieldsUnion(ty, union_obj);
+                    return ty;
+                },
+                else => unreachable,
             },
-
             else => return ty,
         },
     }
src/type.zig
@@ -2385,15 +2385,7 @@ pub const Type = struct {
 
     /// Asserts the type is a function or a function pointer.
     pub fn fnReturnType(ty: Type, mod: *Module) Type {
-        return fnReturnTypeIp(ty, &mod.intern_pool);
-    }
-
-    pub fn fnReturnTypeIp(ty: Type, ip: *const InternPool) Type {
-        return switch (ip.indexToKey(ty.toIntern())) {
-            .ptr_type => |ptr_type| ip.indexToKey(ptr_type.child).func_type.return_type,
-            .func_type => |func_type| func_type.return_type,
-            else => unreachable,
-        }.toType();
+        return mod.intern_pool.funcReturnType(ty.toIntern()).toType();
     }
 
     /// Asserts the type is a function.