authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-09 23:52:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-15 19:06:39-07:00
loga4bb7c8bb17a4ac692401946df6b9f4cc3e5b1b2
tree8ee1dbaf12c60c57df253bafa18d624937365f49
parentf458192e56b13500ff6eb7c3e94dcf48240f4170

stage2: remove redundant source hash


2 files changed, 9 insertions(+), 29 deletions(-)

src/Compilation.zig+5-13
...@@ -1484,22 +1484,14 @@ pub fn update(self: *Compilation) !void {...@@ -1484,22 +1484,14 @@ pub fn update(self: *Compilation) !void {
1484 continue;1484 continue;
1485 }1485 }
14861486
1487 const prev_hash = file.source_hash;1487 log.debug("metadata changed: {s}", .{file.sub_file_path});
1488 file.unloadSource(module.gpa);
1489 // TODO handle error here by populating a retryable compile error
1490 try file.finishGettingSource(module.gpa, f, stat);
1491 assert(file.source_loaded);
1492 if (mem.eql(u8, &prev_hash, &file.source_hash)) {
1493 file.updateTreeToNewSource();
1494 log.debug("unmodified source hash of file: {s}", .{file.sub_file_path});
1495 continue;
1496 }
1497
1498 log.debug("source contents changed: {s}", .{file.sub_file_path});
1499 if (file.status == .unloaded_parse_failure) {1488 if (file.status == .unloaded_parse_failure) {
1500 module.failed_files.swapRemove(file).?.value.destroy(module.gpa);1489 module.failed_files.swapRemove(file).?.value.destroy(module.gpa);
1501 }1490 }
1502 file.unloadTree(module.gpa);1491
1492 file.unload(module.gpa);
1493 // TODO handle error here by populating a retryable compile error
1494 try file.finishGettingSource(module.gpa, f, stat);
15031495
1504 module.analyzeFile(file) catch |err| switch (err) {1496 module.analyzeFile(file) catch |err| switch (err) {
1505 error.OutOfMemory => return error.OutOfMemory,1497 error.OutOfMemory => return error.OutOfMemory,
src/Module.zig+4-16
...@@ -26,7 +26,6 @@ const trace = @import("tracy.zig").trace;...@@ -26,7 +26,6 @@ const trace = @import("tracy.zig").trace;
26const AstGen = @import("AstGen.zig");26const AstGen = @import("AstGen.zig");
27const Sema = @import("Sema.zig");27const Sema = @import("Sema.zig");
28const target_util = @import("target.zig");28const target_util = @import("target.zig");
29const Cache = @import("Cache.zig");
3029
31/// General-purpose allocator. Used for both temporary and long-term storage.30/// General-purpose allocator. Used for both temporary and long-term storage.
32gpa: *Allocator,31gpa: *Allocator,
...@@ -706,8 +705,6 @@ pub const Scope = struct {...@@ -706,8 +705,6 @@ pub const Scope = struct {
706 stat_inode: std.fs.File.INode,705 stat_inode: std.fs.File.INode,
707 /// Whether this is populated depends on `status`.706 /// Whether this is populated depends on `status`.
708 stat_mtime: i128,707 stat_mtime: i128,
709 /// Whether this is populated depends on `status`.
710 source_hash: Cache.BinDigest,
711 /// Whether this is populated or not depends on `status`.708 /// Whether this is populated or not depends on `status`.
712 tree: ast.Tree,709 tree: ast.Tree,
713 /// Package that this file is a part of, managed externally.710 /// Package that this file is a part of, managed externally.
...@@ -734,13 +731,6 @@ pub const Scope = struct {...@@ -734,13 +731,6 @@ pub const Scope = struct {
734 }731 }
735 }732 }
736733
737 pub fn updateTreeToNewSource(file: *File) void {
738 assert(file.source_loaded);
739 if (file.status == .loaded_success) {
740 file.tree.source = file.source;
741 }
742 }
743
744 pub fn deinit(file: *File, gpa: *Allocator) void {734 pub fn deinit(file: *File, gpa: *Allocator) void {
745 file.unload(gpa);735 file.unload(gpa);
746 file.* = undefined;736 file.* = undefined;
...@@ -781,14 +771,11 @@ pub const Scope = struct {...@@ -781,14 +771,11 @@ pub const Scope = struct {
781 return error.FileTooBig;771 return error.FileTooBig;
782772
783 const source = try gpa.allocSentinel(u8, stat.size, 0);773 const source = try gpa.allocSentinel(u8, stat.size, 0);
774 errdefer gpa.free(source);
784 const amt = try f.readAll(source);775 const amt = try f.readAll(source);
785 if (amt != stat.size)776 if (amt != stat.size)
786 return error.UnexpectedEndOfFile;777 return error.UnexpectedEndOfFile;
787778
788 var hasher = Cache.hasher_init;
789 hasher.update(source);
790 hasher.final(&file.source_hash);
791
792 file.stat_size = stat.size;779 file.stat_size = stat.size;
793 file.stat_inode = stat.inode;780 file.stat_inode = stat.inode;
794 file.stat_mtime = stat.mtime;781 file.stat_mtime = stat.mtime;
...@@ -3316,7 +3303,6 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*...@@ -3316,7 +3303,6 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*
3316 new_file.* = .{3303 new_file.* = .{
3317 .sub_file_path = resolved_path,3304 .sub_file_path = resolved_path,
3318 .source = undefined,3305 .source = undefined,
3319 .source_hash = undefined,
3320 .source_loaded = false,3306 .source_loaded = false,
3321 .stat_size = undefined,3307 .stat_size = undefined,
3322 .stat_inode = undefined,3308 .stat_inode = undefined,
...@@ -3343,12 +3329,13 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*...@@ -3343,12 +3329,13 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*
3343 .parent_name_hash = parent_name_hash,3329 .parent_name_hash = parent_name_hash,
3344 .ty = Type.initTag(.type),3330 .ty = Type.initTag(.type),
3345 };3331 };
3332
3346 const top_decl = try mod.createNewDecl(3333 const top_decl = try mod.createNewDecl(
3347 &tmp_namespace,3334 &tmp_namespace,
3348 resolved_path,3335 resolved_path,
3349 0,3336 0,
3350 parent_name_hash,3337 parent_name_hash,
3351 new_file.source_hash,3338 std.zig.hashSrc(tree.source),
3352 );3339 );
3353 defer {3340 defer {
3354 mod.decl_table.removeAssertDiscard(parent_name_hash);3341 mod.decl_table.removeAssertDiscard(parent_name_hash);
...@@ -3421,6 +3408,7 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*...@@ -3421,6 +3408,7 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*
3421 const struct_ty = try val.toType(&gen_scope_arena.allocator);3408 const struct_ty = try val.toType(&gen_scope_arena.allocator);
3422 const struct_decl = struct_ty.getOwnerDecl();3409 const struct_decl = struct_ty.getOwnerDecl();
34233410
3411 struct_decl.contents_hash = top_decl.contents_hash;
3424 new_file.namespace = struct_ty.getNamespace().?;3412 new_file.namespace = struct_ty.getNamespace().?;
3425 new_file.namespace.parent = null;3413 new_file.namespace.parent = null;
3426 new_file.namespace.parent_name_hash = tmp_namespace.parent_name_hash;3414 new_file.namespace.parent_name_hash = tmp_namespace.parent_name_hash;