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 {...@@ -1138,6 +1138,23 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1138 try builtin_pkg.add(gpa, "builtin", builtin_pkg);1138 try builtin_pkg.add(gpa, "builtin", builtin_pkg);
1139 }1139 }
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
1141 // TODO when we implement serialization and deserialization of incremental1158 // TODO when we implement serialization and deserialization of incremental
1142 // compilation metadata, this is where we would load it. We have open a handle1159 // compilation metadata, this is where we would load it. We have open a handle
1143 // to the directory where the output either already is, or will be.1160 // to the directory where the output either already is, or will be.
...@@ -1151,6 +1168,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1151,6 +1168,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1151 .comp = comp,1168 .comp = comp,
1152 .root_pkg = root_pkg,1169 .root_pkg = root_pkg,
1153 .zig_cache_artifact_directory = zig_cache_artifact_directory,1170 .zig_cache_artifact_directory = zig_cache_artifact_directory,
1171 .global_zir_cache = global_zir_cache,
1172 .local_zir_cache = local_zir_cache,
1154 .emit_h = options.emit_h,1173 .emit_h = options.emit_h,
1155 .error_name_list = try std.ArrayListUnmanaged([]const u8).initCapacity(gpa, 1),1174 .error_name_list = try std.ArrayListUnmanaged([]const u8).initCapacity(gpa, 1),
1156 };1175 };
src/Module.zig+11-10
...@@ -36,6 +36,11 @@ comp: *Compilation,...@@ -36,6 +36,11 @@ comp: *Compilation,
36zig_cache_artifact_directory: Compilation.Directory,36zig_cache_artifact_directory: Compilation.Directory,
37/// Pointer to externally managed resource. `null` if there is no zig file being compiled.37/// Pointer to externally managed resource. `null` if there is no zig file being compiled.
38root_pkg: *Package,38root_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,
39/// It's rare for a decl to be exported, so we save memory by having a sparse map of44/// It's rare for a decl to be exported, so we save memory by having a sparse map of
40/// Decl pointers to details about them being exported.45/// Decl pointers to details about them being exported.
41/// The Export memory is owned by the `export_owners` table; the slice itself is owned by this table.46/// 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 {...@@ -2620,6 +2625,8 @@ pub fn deinit(mod: *Module) void {
2620 mod.compile_log_text.deinit(gpa);2625 mod.compile_log_text.deinit(gpa);
26212626
2622 mod.zig_cache_artifact_directory.handle.close();2627 mod.zig_cache_artifact_directory.handle.close();
2628 mod.local_zir_cache.handle.close();
2629 mod.global_zir_cache.handle.close();
26232630
2624 mod.deletion_set.deinit(gpa);2631 mod.deletion_set.deinit(gpa);
26252632
...@@ -2722,18 +2729,12 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node...@@ -2722,18 +2729,12 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
2722 path_hash.addBytes(file.sub_file_path);2729 path_hash.addBytes(file.sub_file_path);
2723 break :hash path_hash.final();2730 break :hash path_hash.final();
2724 };2731 };
2725 const cache_directory = if (want_local_cache)2732 const cache_directory = if (want_local_cache) mod.local_zir_cache else mod.global_zir_cache;
2726 comp.local_cache_directory2733 const zir_dir = cache_directory.handle;
2727 else
2728 comp.global_cache_directory;
27292734
2730 var cache_file: ?std.fs.File = null;2735 var cache_file: ?std.fs.File = null;
2731 defer if (cache_file) |f| f.close();2736 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
2737 // Determine whether we need to reload the file from disk and redo parsing and AstGen.2738 // Determine whether we need to reload the file from disk and redo parsing and AstGen.
2738 switch (file.status) {2739 switch (file.status) {
2739 .never_loaded, .retryable_failure => cached: {2740 .never_loaded, .retryable_failure => cached: {
...@@ -2899,7 +2900,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node...@@ -2899,7 +2900,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
2899 else => |e| {2900 else => |e| {
2900 const pkg_path = file.pkg.root_src_directory.path orelse ".";2901 const pkg_path = file.pkg.root_src_directory.path orelse ".";
2901 const cache_path = cache_directory.path orelse ".";2902 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}", .{
2903 pkg_path, file.sub_file_path, cache_path, &digest, @errorName(e),2904 pkg_path, file.sub_file_path, cache_path, &digest, @errorName(e),
2904 });2905 });
2905 return;2906 return;
...@@ -3023,7 +3024,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node...@@ -3023,7 +3024,7 @@ pub fn astGenFile(mod: *Module, file: *Scope.File, prog_node: *std.Progress.Node
3023 cache_file.?.writevAll(&iovecs) catch |err| {3024 cache_file.?.writevAll(&iovecs) catch |err| {
3024 const pkg_path = file.pkg.root_src_directory.path orelse ".";3025 const pkg_path = file.pkg.root_src_directory.path orelse ".";
3025 const cache_path = cache_directory.path orelse ".";3026 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}", .{
3027 pkg_path, file.sub_file_path, cache_path, &digest, @errorName(err),3028 pkg_path, file.sub_file_path, cache_path, &digest, @errorName(err),
3028 });3029 });
3029 };3030 };