From 52ec121469d7bf10116fb22c122cbce8ceddd028 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Tue, 20 Jun 2023 13:48:19 -0400 Subject: [PATCH] Sema: optimize callers of `indexToKey` --- src/Air.zig | 2 +- src/InternPool.zig | 19 +++++++++++++++++-- src/Sema.zig | 29 ++++++++++++++++++----------- src/type.zig | 10 +--------- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/Air.zig b/src/Air.zig index f2c9ce9634..ec2baf0dab 100644 --- a/src/Air.zig +++ b/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 => { diff --git a/src/InternPool.zig b/src/InternPool.zig index f93539daf8..d77b82932c 100644 --- a/src/InternPool.zig +++ b/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, }, }; diff --git a/src/Sema.zig b/src/Sema.zig index 761dacf547..7d4c4a6240 100644 --- a/src/Sema.zig +++ b/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; + _ => 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, }, - .union_type => |union_type| { - const union_obj = mod.unionPtr(union_type.index); - try sema.resolveTypeFieldsUnion(ty, union_obj); - return ty; - }, - else => return ty, }, } diff --git a/src/type.zig b/src/type.zig index ac5305b3f4..32ea8e2c5d 100644 --- a/src/type.zig +++ b/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.