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:
parent
b659b5a4af
commit
e8982e3378
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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).
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue