authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-06-20 08:38:23+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-06-20 08:41:07+01:00
log2b677d1660018434757f9c9efec3c712675e6c47
treeb0c05ac895e1e42abca677a135caf34d15173d24
parent6a8cf25a8aea11ef80b48c9bce59d0aafcff3297
signaturelock-open Commit is signed but in an unrecognized format.

Sema: fix performance regression

LLVM fails to notice that in release builds, `logFn` ignores its arguments, so their computation can be elided. So, LLVM fails to elide this hashmap lookup. Its cost isn't too significant, but doing it in the hottest loop in Sema adds up! Technically, we could do the lookup a single time, before the loop, but it was cleanest (and a little faster) to just disable this log call at comptime when debug logging is disabled.

1 files changed, 10 insertions(+), 5 deletions(-)

src/Sema.zig+10-5
......@@ -992,11 +992,16 @@ fn analyzeBodyInner(
992992 while (true) {
993993 crash_info.setBodyIndex(i);
994994 const inst = body[i];
995 std.log.scoped(.sema_zir).debug("sema ZIR {s} %{d}", .{ sub_file_path: {
996 const path_digest = block.src_base_inst.resolveFull(&mod.intern_pool).path_digest;
997 const index = mod.path_digest_map.getIndex(path_digest).?;
998 break :sub_file_path mod.import_table.values()[index].sub_file_path;
999 }, inst });
995
996 // The hashmap lookup in here is a little expensive, and LLVM fails to optimize it away.
997 if (build_options.enable_logging) {
998 std.log.scoped(.sema_zir).debug("sema ZIR {s} %{d}", .{ sub_file_path: {
999 const path_digest = block.src_base_inst.resolveFull(&mod.intern_pool).path_digest;
1000 const index = mod.path_digest_map.getIndex(path_digest).?;
1001 break :sub_file_path mod.import_table.values()[index].sub_file_path;
1002 }, inst });
1003 }
1004
10001005 const air_inst: Air.Inst.Ref = switch (tags[@intFromEnum(inst)]) {
10011006 // zig fmt: off
10021007 .alloc => try sema.zirAlloc(block, inst),