authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-25 10:43:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-25 10:43:07-07:00
log56226449d2106448ac8cd1888a7b302020e22456
tree16f650fc09b858958b088c7d15b9793f8fb32dde
parent015cd79f89aefd26fb1df91f3d07f7dcade21c4a

stage2: pre-open ZIR cache dir handles

So that we do not needlessly open and close the ZIR cache dir handles in each AstGen operation.

2 files changed, 30 insertions(+), 10 deletions(-)

src/Compilation.zig+19
......@@ -1138,6 +1138,23 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11381138 try builtin_pkg.add(gpa, "builtin", builtin_pkg);
11391139 }
11401140
1141 // Pre-open the directory handles for cached ZIR code so that it does not need
1142 // to redundantly happen for each AstGen operation.
1143 const zir_sub_dir = "z";
1144
1145 var local_zir_dir = try options.local_cache_directory.handle.makeOpenPath(zir_sub_dir, .{});
1146 errdefer local_zir_dir.close();
1147 const local_zir_cache: Directory = .{
1148 .handle = local_zir_dir,
1149 .path = try options.local_cache_directory.join(arena, &[_][]const u8{zir_sub_dir}),
1150 };
1151 var global_zir_dir = try options.global_cache_directory.handle.makeOpenPath(zir_sub_dir, .{});
1152 errdefer global_zir_dir.close();
1153 const global_zir_cache: Directory = .{
1154 .handle = global_zir_dir,
1155 .path = try options.global_cache_directory.join(arena, &[_][]const u8{zir_sub_dir}),
1156 };
1157
11411158 // TODO when we implement serialization and deserialization of incremental
11421159 // compilation metadata, this is where we would load it. We have open a handle
11431160 // to the directory where the output either already is, or will be.
......@@ -1151,6 +1168,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11511168 .comp = comp,
11521169 .root_pkg = root_pkg,
11531170 .zig_cache_artifact_directory = zig_cache_artifact_directory,
1171 .global_zir_cache = global_zir_cache,
1172 .local_zir_cache = local_zir_cache,
11541173 .emit_h = options.emit_h,
11551174 .error_name_list = try std.ArrayListUnmanaged([]const u8).initCapacity(gpa, 1),
11561175 };
src/Module.zig+11-10
......@@ -36,6 +36,11 @@ comp: *Compilation,
3636zig_cache_artifact_directory: Compilation.Directory,
3737/// Pointer to externally managed resource. `null` if there is no zig file being compiled.
3838root_pkg: *Package,
39
40/// Used by AstGen worker to load and store ZIR cache.
41global_zir_cache: Compilation.Directory,
42/// Used by AstGen worker to load and store ZIR cache.
43local_zir_cache: Compilation.Directory,
3944/// It's rare for a decl to be exported, so we save memory by having a sparse map of
4045/// Decl pointers to details about them being exported.
4146/// The Export memory is owned by the `export_owners` table; the slice itself is owned by this table.
......@@ -2620,6 +2625,8 @@ pub fn deinit(mod: *Module) void {
26202625 mod.compile_log_text.deinit(gpa);
26212626
26222627 mod.zig_cache_artifact_directory.handle.close();
2628 mod.local_zir_cache.handle.close();
2629 mod.global_zir_cache.handle.close();
26232630
26242631 mod.deletion_set.deinit(gpa);
26252632
......@@ -2722,18 +2729,12 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
27222729 path_hash.addBytes(file.sub_file_path);
27232730 break :hash path_hash.final();
27242731 };
2725 const cache_directory = if (want_local_cache)
2726 comp.local_cache_directory
2727 else
2728 comp.global_cache_directory;
2732 const cache_directory = if (want_local_cache) mod.local_zir_cache else mod.global_zir_cache;
2733 const zir_dir = cache_directory.handle;
27292734
27302735 var cache_file: ?std.fs.File = null;
27312736 defer if (cache_file) |f| f.close();
27322737
2733 // TODO do this before spawning astgen workers
2734 var zir_dir = try cache_directory.handle.makeOpenPath("z", .{});
2735 defer zir_dir.close();
2736
27372738 // Determine whether we need to reload the file from disk and redo parsing and AstGen.
27382739 switch (file.status) {
27392740 .never_loaded, .retryable_failure => cached: {
......@@ -2899,7 +2900,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
28992900 else => |e| {
29002901 const pkg_path = file.pkg.root_src_directory.path orelse ".";
29012902 const cache_path = cache_directory.path orelse ".";
2902 log.warn("unable to save cached ZIR code for {s}/{s} to {s}/z/{s}: {s}", .{
2903 log.warn("unable to save cached ZIR code for {s}/{s} to {s}/{s}: {s}", .{
29032904 pkg_path, file.sub_file_path, cache_path, &digest, @errorName(e),
29042905 });
29052906 return;
......@@ -3023,7 +3024,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
30233024 cache_file.?.writevAll(&iovecs) catch |err| {
30243025 const pkg_path = file.pkg.root_src_directory.path orelse ".";
30253026 const cache_path = cache_directory.path orelse ".";
3026 log.warn("unable to write cached ZIR code for {s}/{s} to {s}/z/{s}: {s}", .{
3027 log.warn("unable to write cached ZIR code for {s}/{s} to {s}/{s}: {s}", .{
30273028 pkg_path, file.sub_file_path, cache_path, &digest, @errorName(err),
30283029 });
30293030 };