sema: hash a name once per lookup; cache scalar types

Sema.lookup hashed the name again in every scope out to the global one;
it now hashes once and probes each scope with that hash. TypeTable.scalar
interned its key through the type map on every call; it now keeps the
result per scalar.

Glide's shaders in the hill-climbing set: -3.1% (A/B); output unchanged
on the verify corpus.
This commit is contained in:
peterino2 2026-11-03 09:03:38 -08:00
parent b659b5a4af
commit e8982e3378
2 changed files with 23 additions and 6 deletions

View File

@ -940,13 +940,26 @@ pub const Sema = struct {
} }
fn lookup(s: *Sema, name: []const u8) ?Symbol { 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; var sc: ?*Scope = s.scope;
while (sc) |c| : (sc = c.parent) { 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; 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. /// Resolve a possibly qualified name.
fn lookupName(s: *Sema, n: ast.Name) ?Symbol { fn lookupName(s: *Sema, n: ast.Name) ?Symbol {
return s.lookupNameInst(n) catch null; return s.lookupNameInst(n) catch null;

View File

@ -357,10 +357,10 @@ pub const ResourceKind = enum(u8) {
/// vk::SampledTexture* by name. /// vk::SampledTexture* by name.
pub const vk_names = [_]struct { []const u8, ResourceKind }{ pub const vk_names = [_]struct { []const u8, ResourceKind }{
.{ "SampledTexture1D", .sampled_texture1d }, .{ "SampledTexture1DArray", .sampled_texture1d_array }, .{ "SampledTexture1D", .sampled_texture1d }, .{ "SampledTexture1DArray", .sampled_texture1d_array },
.{ "SampledTexture2D", .sampled_texture2d }, .{ "SampledTexture2DArray", .sampled_texture2d_array }, .{ "SampledTexture2D", .sampled_texture2d }, .{ "SampledTexture2DArray", .sampled_texture2d_array },
.{ "SampledTexture2DMS", .sampled_texture2dms }, .{ "SampledTexture2DMSArray", .sampled_texture2dms_array }, .{ "SampledTexture2DMS", .sampled_texture2dms }, .{ "SampledTexture2DMSArray", .sampled_texture2dms_array },
.{ "SampledTexture3D", .sampled_texture3d }, .{ "SampledTextureCUBE", .sampled_texture_cube }, .{ "SampledTexture3D", .sampled_texture3d }, .{ "SampledTextureCUBE", .sampled_texture_cube },
.{ "SampledTextureCUBEArray", .sampled_texture_cube_array }, .{ "SampledTextureCUBEArray", .sampled_texture_cube_array },
}; };
@ -536,6 +536,8 @@ pub const TypeTable = struct {
gpa: Allocator, gpa: Allocator,
types: std.ArrayList(Type) = .empty, types: std.ArrayList(Type) = .empty,
map: std.AutoHashMapUnmanaged(Key, TypeId) = .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 error_type: TypeId = 0;
pub const void_type: TypeId = 1; pub const void_type: TypeId = 1;
@ -591,7 +593,9 @@ pub const TypeTable = struct {
} }
pub fn scalar(t: *TypeTable, s: Scalar) !TypeId { 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). /// `len == 1` vectors are kept distinct from scalars (float1 != float).