From a7088fd9a3edb037f0f51bb402a3c557334634f3 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 23 Sep 2023 23:06:08 -0700 Subject: compiler: packed structs cache bit offsets Instead of linear search every time a packed struct field's bit or byte offset is wanted, they are computed once during resolution of the packed struct's backing int type, and stored in InternPool for O(1) lookup. Closes #17178 --- src/Module.zig | 23 ----------------------- 1 file changed, 23 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 17a97e6c6d..8ac9d794de 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -6649,26 +6649,3 @@ pub fn structFieldAlignmentExtern(mod: *Module, field_ty: Type) Alignment { return ty_abi_align; } - -/// TODO: avoid linear search by storing these in trailing data of packed struct types -/// then packedStructFieldByteOffset can be expressed in terms of bits / 8, fixing -/// that one too. -/// https://github.com/ziglang/zig/issues/17178 -pub fn structPackedFieldBitOffset( - mod: *Module, - struct_type: InternPool.Key.StructType, - field_index: u32, -) u16 { - const ip = &mod.intern_pool; - assert(struct_type.layout == .Packed); - assert(struct_type.haveLayout(ip)); - var bit_sum: u64 = 0; - for (0..struct_type.field_types.len) |i| { - if (i == field_index) { - return @intCast(bit_sum); - } - const field_ty = struct_type.field_types.get(ip)[i].toType(); - bit_sum += field_ty.bitSize(mod); - } - unreachable; // index out of bounds -} -- cgit v1.2.3