Allocate less per compile
Every compile ran in an arena, where a growing list leaves each old buffer behind, and a few sites dominated: the Glide corpus's 8,772 compiles allocated 38 GB. Now 24 GB: - intrinsics: build the Db once per process and share it read-only (it was rebuilt from the static table on every compile, 3.5 GB); - parser: reserve the token list from the source size (5.2 GB of regrowth), and collect name parts on the stack (25M allocations); - dxil emit: reserve the function's instruction list in finish (5.0 GB); - sema: reserve resolutions from the token count (1.0 GB). zdxc --job on the corpus: 15.4 -> 14.3 s on 1 thread, 3.0 -> 2.65 s on 24. Output is byte-identical on a 1,462-compile subset.
This commit is contained in:
parent
22e1bcbc2a
commit
b5eba0625f
|
|
@ -453,6 +453,9 @@ fn run(ctx: *Ctx, arena: std.mem.Allocator) !void {
|
||||||
.root = opts.entry,
|
.root = opts.entry,
|
||||||
.row_major = opts.row_major,
|
.row_major = opts.row_major,
|
||||||
});
|
});
|
||||||
|
// About one resolution per expression: reserve rather
|
||||||
|
// than grow, which leaves every old buffer in the arena.
|
||||||
|
try sema.resolutions.ensureTotalCapacity(arena, parser.toks.len);
|
||||||
try sema.checkUnit(tu);
|
try sema.checkUnit(tu);
|
||||||
if (!opts.sema and !diags.hasErrors() and profile != null) {
|
if (!opts.sema and !diags.hasErrors() and profile != null) {
|
||||||
const stage: zdxc.ir.ir.Stage = switch (profile.?.kind) {
|
const stage: zdxc.ir.ir.Stage = switch (profile.?.kind) {
|
||||||
|
|
|
||||||
|
|
@ -1267,6 +1267,7 @@ const Emitter = struct {
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
try f.insts.ensureUnusedCapacity(e.dm.allocator(), n);
|
||||||
const all = [_][]const PInst{e.prologue.items};
|
const all = [_][]const PInst{e.prologue.items};
|
||||||
for (all) |items| for (items) |pi| try f.insts.append(e.dm.allocator(), try remapInst(e.arena, map, pi.inst, remap));
|
for (all) |items| for (items) |pi| try f.insts.append(e.dm.allocator(), try remapInst(e.arena, map, pi.inst, remap));
|
||||||
for (e.blocks.items) |blk| for (blk.items) |pi| try f.insts.append(e.dm.allocator(), try remapInst(e.arena, map, pi.inst, remap));
|
for (e.blocks.items) |blk| for (blk.items) |pi| try f.insts.append(e.dm.allocator(), try remapInst(e.arena, map, pi.inst, remap));
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,11 @@ pub const Parser = struct {
|
||||||
/// Collect every token from `pp` and set up a parser over them.
|
/// Collect every token from `pp` and set up a parser over them.
|
||||||
pub fn fromPreprocessor(arena: Allocator, diags: *Diagnostics, pp: *Preprocessor) !Parser {
|
pub fn fromPreprocessor(arena: Allocator, diags: *Diagnostics, pp: *Preprocessor) !Parser {
|
||||||
var toks: std.ArrayList(Token) = .empty;
|
var toks: std.ArrayList(Token) = .empty;
|
||||||
|
// Growing a large list in an arena leaves every old buffer behind:
|
||||||
|
// reserve about one token per 5 bytes of the files loaded so far.
|
||||||
|
var loaded: usize = 0;
|
||||||
|
for (pp.sm.files.items) |f| loaded += f.text.len;
|
||||||
|
try toks.ensureTotalCapacity(arena, loaded / 5 + 64);
|
||||||
var packs: std.ArrayList(PackPragma) = .empty;
|
var packs: std.ArrayList(PackPragma) = .empty;
|
||||||
while (true) {
|
while (true) {
|
||||||
// Preprocessor diagnostics are ordered by the token being produced.
|
// Preprocessor diagnostics are ordered by the token being produced.
|
||||||
|
|
@ -600,17 +605,22 @@ pub const Parser = struct {
|
||||||
|
|
||||||
fn parseName(p: *Parser) Error!ast.Name {
|
fn parseName(p: *Parser) Error!ast.Name {
|
||||||
const loc = p.tok().loc;
|
const loc = p.tok().loc;
|
||||||
var parts: std.ArrayList([]const u8) = .empty;
|
// Names are parsed very often and nearly all have one part: collect
|
||||||
var args: std.ArrayList([]const ast.TemplateArg) = .empty;
|
// them on the stack and copy them to the arena once.
|
||||||
|
var parts_buf: [32][]const u8 = undefined;
|
||||||
|
var args_buf: [32][]const ast.TemplateArg = undefined;
|
||||||
|
var parts: std.ArrayList([]const u8) = .initBuffer(&parts_buf);
|
||||||
|
var args: std.ArrayList([]const ast.TemplateArg) = .initBuffer(&args_buf);
|
||||||
var any_args = false;
|
var any_args = false;
|
||||||
p.forced_template = false;
|
p.forced_template = false;
|
||||||
if (p.eat(.colon_colon)) {
|
if (p.eat(.colon_colon)) {
|
||||||
try parts.append(p.arena, "");
|
parts.appendAssumeCapacity("");
|
||||||
try args.append(p.arena, &.{});
|
args.appendAssumeCapacity(&.{});
|
||||||
}
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
const id = try p.expectIdent("identifier");
|
const id = try p.expectIdent("identifier");
|
||||||
try parts.append(p.arena, id.text);
|
if (parts.items.len == parts_buf.len) return p.fail("name has too many parts", .{});
|
||||||
|
parts.appendAssumeCapacity(id.text);
|
||||||
var part_args: []const ast.TemplateArg = &.{};
|
var part_args: []const ast.TemplateArg = &.{};
|
||||||
// `S<T>::member`: arguments followed by `::` belong to this part.
|
// `S<T>::member`: arguments followed by `::` belong to this part.
|
||||||
if (p.at(.less) and p.isTemplateName(id.text)) {
|
if (p.at(.less) and p.isTemplateName(id.text)) {
|
||||||
|
|
@ -621,7 +631,7 @@ pub const Parser = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try args.append(p.arena, part_args);
|
args.appendAssumeCapacity(part_args);
|
||||||
if (!(p.at(.colon_colon) and p.peek(1).kind == .identifier and !p.peek(1).isIdent("operator"))) break;
|
if (!(p.at(.colon_colon) and p.peek(1).kind == .identifier and !p.peek(1).isIdent("operator"))) break;
|
||||||
_ = p.advance();
|
_ = p.advance();
|
||||||
// `X<T>::template Y<U>`: the disambiguator.
|
// `X<T>::template Y<U>`: the disambiguator.
|
||||||
|
|
@ -630,7 +640,11 @@ pub const Parser = struct {
|
||||||
p.forced_template = true;
|
p.forced_template = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return .{ .loc = loc, .parts = parts.items, .args = if (any_args) args.items else &.{} };
|
return .{
|
||||||
|
.loc = loc,
|
||||||
|
.parts = try p.arena.dupe([]const u8, parts.items),
|
||||||
|
.args = if (any_args) try p.arena.dupe([]const ast.TemplateArg, args.items) else &.{},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseTemplateArgs(p: *Parser) Error![]const ast.TemplateArg {
|
fn parseTemplateArgs(p: *Parser) Error![]const ast.TemplateArg {
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,28 @@ pub const Db = struct {
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var shared_db: Db = .{};
|
||||||
|
/// 0: not built, 1: being built, 2: built.
|
||||||
|
var shared_state: std.atomic.Value(u8) = .init(0);
|
||||||
|
|
||||||
|
/// The Db, built once per process and then shared read-only: it
|
||||||
|
/// depends only on the generated table, and building it costs thousands
|
||||||
|
/// of allocations per compile.
|
||||||
|
pub fn shared() Allocator.Error!*const Db {
|
||||||
|
while (true) switch (shared_state.load(.acquire)) {
|
||||||
|
2 => return &shared_db,
|
||||||
|
0 => if (shared_state.cmpxchgStrong(0, 1, .acquire, .monotonic) == null) {
|
||||||
|
shared_db = init(std.heap.smp_allocator) catch |e| {
|
||||||
|
shared_state.store(0, .release);
|
||||||
|
return e;
|
||||||
|
};
|
||||||
|
shared_state.store(2, .release);
|
||||||
|
return &shared_db;
|
||||||
|
},
|
||||||
|
else => std.atomic.spinLoopHint(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// `ns` (e.g. `Texture2DMethods`) declares a method `name`.
|
/// `ns` (e.g. `Texture2DMethods`) declares a method `name`.
|
||||||
pub fn hasMethod(db: *const Db, ns: []const u8, name: []const u8) bool {
|
pub fn hasMethod(db: *const Db, ns: []const u8, name: []const u8) bool {
|
||||||
var buf: [256]u8 = undefined;
|
var buf: [256]u8 = undefined;
|
||||||
|
|
|
||||||
|
|
@ -208,7 +208,7 @@ pub const Sema = struct {
|
||||||
current_block: ?*const ast.Decl = null,
|
current_block: ?*const ast.Decl = null,
|
||||||
loop_depth: u32 = 0,
|
loop_depth: u32 = 0,
|
||||||
switch_depth: u32 = 0,
|
switch_depth: u32 = 0,
|
||||||
intrinsic_db: intrinsics.Db,
|
intrinsic_db: *const intrinsics.Db,
|
||||||
enum_scopes: std.AutoHashMapUnmanaged(*types.EnumInfo, *Scope) = .empty,
|
enum_scopes: std.AutoHashMapUnmanaged(*types.EnumInfo, *Scope) = .empty,
|
||||||
/// Where each template was declared (its names resolve from there).
|
/// Where each template was declared (its names resolve from there).
|
||||||
template_scopes: std.AutoHashMapUnmanaged(*const ast.Decl, *Scope) = .empty,
|
template_scopes: std.AutoHashMapUnmanaged(*const ast.Decl, *Scope) = .empty,
|
||||||
|
|
@ -232,7 +232,7 @@ pub const Sema = struct {
|
||||||
.opts = opts,
|
.opts = opts,
|
||||||
.global = global,
|
.global = global,
|
||||||
.scope = global,
|
.scope = global,
|
||||||
.intrinsic_db = try intrinsics.Db.init(arena),
|
.intrinsic_db = try intrinsics.Db.shared(),
|
||||||
};
|
};
|
||||||
try s.declareBuiltins();
|
try s.declareBuiltins();
|
||||||
return s;
|
return s;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue