diff --git a/src/sema/sema.zig b/src/sema/sema.zig index 2a04bcc..e09ab50 100644 --- a/src/sema/sema.zig +++ b/src/sema/sema.zig @@ -940,13 +940,26 @@ pub const Sema = struct { } fn lookup(s: *Sema, name: []const u8) ?Symbol { + // (The name hashed once, for every scope out to the global one.) + const key: Prehashed = .{ .h = std.hash_map.hashString(name) }; var sc: ?*Scope = s.scope; while (sc) |c| : (sc = c.parent) { - if (c.syms.get(name)) |sym| return sym; + if (c.syms.getAdapted(name, key)) |sym| return sym; } return null; } + /// A string hash map key context with the hash already worked out. + const Prehashed = struct { + h: u64, + pub fn hash(k: Prehashed, _: []const u8) u64 { + return k.h; + } + pub fn eql(_: Prehashed, a: []const u8, b: []const u8) bool { + return std.mem.eql(u8, a, b); + } + }; + /// Resolve a possibly qualified name. fn lookupName(s: *Sema, n: ast.Name) ?Symbol { return s.lookupNameInst(n) catch null; diff --git a/src/sema/types.zig b/src/sema/types.zig index 2f9afc3..77c3201 100644 --- a/src/sema/types.zig +++ b/src/sema/types.zig @@ -357,10 +357,10 @@ pub const ResourceKind = enum(u8) { /// vk::SampledTexture* by name. pub const vk_names = [_]struct { []const u8, ResourceKind }{ - .{ "SampledTexture1D", .sampled_texture1d }, .{ "SampledTexture1DArray", .sampled_texture1d_array }, - .{ "SampledTexture2D", .sampled_texture2d }, .{ "SampledTexture2DArray", .sampled_texture2d_array }, - .{ "SampledTexture2DMS", .sampled_texture2dms }, .{ "SampledTexture2DMSArray", .sampled_texture2dms_array }, - .{ "SampledTexture3D", .sampled_texture3d }, .{ "SampledTextureCUBE", .sampled_texture_cube }, + .{ "SampledTexture1D", .sampled_texture1d }, .{ "SampledTexture1DArray", .sampled_texture1d_array }, + .{ "SampledTexture2D", .sampled_texture2d }, .{ "SampledTexture2DArray", .sampled_texture2d_array }, + .{ "SampledTexture2DMS", .sampled_texture2dms }, .{ "SampledTexture2DMSArray", .sampled_texture2dms_array }, + .{ "SampledTexture3D", .sampled_texture3d }, .{ "SampledTextureCUBE", .sampled_texture_cube }, .{ "SampledTextureCUBEArray", .sampled_texture_cube_array }, }; @@ -536,6 +536,8 @@ pub const TypeTable = struct { gpa: Allocator, types: std.ArrayList(Type) = .empty, map: std.AutoHashMapUnmanaged(Key, TypeId) = .empty, + /// scalar's results. + scalars: [@typeInfo(Scalar).@"enum".field_names.len]TypeId = @splat(std.math.maxInt(TypeId)), pub const error_type: TypeId = 0; pub const void_type: TypeId = 1; @@ -591,7 +593,9 @@ pub const TypeTable = struct { } pub fn scalar(t: *TypeTable, s: Scalar) !TypeId { - return t.intern(.{ .scalar = s }); + const k = @backingInt(s); + if (t.scalars[k] == std.math.maxInt(TypeId)) t.scalars[k] = try t.intern(.{ .scalar = s }); + return t.scalars[k]; } /// `len == 1` vectors are kept distinct from scalars (float1 != float).