authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-17 08:30:56+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-08-17 18:50:10-04:00
log90116d92b08ff882715ca94ecc79934bc0a73762
tree9802071e8f83134558d15f967b67f760112fc469
parent89f02d1c107a159dc6433863c7d9167872b4c0c8

Compilation: don't call `resolveReferences` unnecessarily

This function is slow and should only be called in compile error cases.

1 files changed, 26 insertions(+), 9 deletions(-)

src/Compilation.zig+26-9
......@@ -3065,8 +3065,8 @@ pub fn totalErrorCount(comp: *Compilation) Allocator.Error!u32 {
30653065 if (comp.module) |zcu| {
30663066 const ip = &zcu.intern_pool;
30673067
3068 var all_references = try zcu.resolveReferences();
3069 defer all_references.deinit(zcu.gpa);
3068 var all_references: ?std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference) = null;
3069 defer if (all_references) |*a| a.deinit(zcu.gpa);
30703070
30713071 total += zcu.failed_exports.count();
30723072 total += zcu.failed_embed_files.count();
......@@ -3088,7 +3088,12 @@ pub fn totalErrorCount(comp: *Compilation) Allocator.Error!u32 {
30883088 // the previous parse success, including compile errors, but we cannot
30893089 // emit them until the file succeeds parsing.
30903090 for (zcu.failed_analysis.keys()) |anal_unit| {
3091 if (comp.incremental and !all_references.contains(anal_unit)) continue;
3091 if (comp.incremental) {
3092 if (all_references == null) {
3093 all_references = try zcu.resolveReferences();
3094 }
3095 if (!all_references.?.contains(anal_unit)) continue;
3096 }
30923097 const file_index = switch (anal_unit.unwrap()) {
30933098 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
30943099 .func => |ip_index| (zcu.funcInfo(ip_index).zir_body_inst.resolveFull(ip) orelse continue).file,
......@@ -3172,8 +3177,8 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
31723177 });
31733178 }
31743179
3175 var all_references = if (comp.module) |zcu| try zcu.resolveReferences() else undefined;
3176 defer if (comp.module != null) all_references.deinit(gpa);
3180 var all_references: ?std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference) = null;
3181 defer if (all_references) |*a| a.deinit(gpa);
31773182
31783183 if (comp.module) |zcu| {
31793184 const ip = &zcu.intern_pool;
......@@ -3231,7 +3236,12 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
32313236 if (err) |e| return e;
32323237 }
32333238 for (zcu.failed_analysis.keys(), zcu.failed_analysis.values()) |anal_unit, error_msg| {
3234 if (comp.incremental and !all_references.contains(anal_unit)) continue;
3239 if (comp.incremental) {
3240 if (all_references == null) {
3241 all_references = try zcu.resolveReferences();
3242 }
3243 if (!all_references.?.contains(anal_unit)) continue;
3244 }
32353245
32363246 const file_index = switch (anal_unit.unwrap()) {
32373247 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
......@@ -3352,7 +3362,10 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33523362 if (comp.module) |zcu| {
33533363 if (comp.incremental and bundle.root_list.items.len == 0) {
33543364 const should_have_error = for (zcu.transitive_failed_analysis.keys()) |failed_unit| {
3355 if (all_references.contains(failed_unit)) break true;
3365 if (all_references == null) {
3366 all_references = try zcu.resolveReferences();
3367 }
3368 if (all_references.?.contains(failed_unit)) break true;
33563369 } else false;
33573370 if (should_have_error) {
33583371 @panic("referenced transitive analysis errors, but none actually emitted");
......@@ -3414,7 +3427,7 @@ pub fn addModuleErrorMsg(
34143427 mod: *Zcu,
34153428 eb: *ErrorBundle.Wip,
34163429 module_err_msg: Zcu.ErrorMsg,
3417 all_references: *const std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference),
3430 all_references: *?std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference),
34183431) !void {
34193432 const gpa = eb.gpa;
34203433 const ip = &mod.intern_pool;
......@@ -3438,13 +3451,17 @@ pub fn addModuleErrorMsg(
34383451 defer ref_traces.deinit(gpa);
34393452
34403453 if (module_err_msg.reference_trace_root.unwrap()) |rt_root| {
3454 if (all_references.* == null) {
3455 all_references.* = try mod.resolveReferences();
3456 }
3457
34413458 var seen: std.AutoHashMapUnmanaged(InternPool.AnalUnit, void) = .{};
34423459 defer seen.deinit(gpa);
34433460
34443461 const max_references = mod.comp.reference_trace orelse Sema.default_reference_trace_len;
34453462
34463463 var referenced_by = rt_root;
3447 while (all_references.get(referenced_by)) |maybe_ref| {
3464 while (all_references.*.?.get(referenced_by)) |maybe_ref| {
34483465 const ref = maybe_ref orelse break;
34493466 const gop = try seen.getOrPut(gpa, ref.referencer);
34503467 if (gop.found_existing) break;