authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-05 23:22:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
logce5b2286f1db3d5a01ce139b450140f21c9d9909
tree8b4e485ad67194355f9853fb42429ee754f4cde6
parentde341b8fb86374ac62a36c987e11d1dd0b8c3358

Support caching bools; make caching values infallible


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

lib/std/cache_hash.zig+16-10
...@@ -61,13 +61,14 @@ pub const CacheHash = struct {...@@ -61,13 +61,14 @@ pub const CacheHash = struct {
61 };61 };
62 }62 }
6363
64 pub fn cache_buf(self: *@This(), val: []const u8) !void {64 pub fn cache_buf(self: *@This(), val: []const u8) void {
65 debug.assert(self.manifest_file_path == null);65 debug.assert(self.manifest_file_path == null);
6666
67 self.blake3.update(val);67 self.blake3.update(val);
68 self.blake3.update(&[_]u8{0});
68 }69 }
6970
70 pub fn cache(self: *@This(), val: var) !void {71 pub fn cache(self: *@This(), val: var) void {
71 debug.assert(self.manifest_file_path == null);72 debug.assert(self.manifest_file_path == null);
7273
73 const val_type = @TypeOf(val);74 const val_type = @TypeOf(val);
...@@ -76,14 +77,17 @@ pub const CacheHash = struct {...@@ -76,14 +77,17 @@ pub const CacheHash = struct {
76 const buf_len = @divExact(int_info.bits, 8);77 const buf_len = @divExact(int_info.bits, 8);
77 var buf: [buf_len]u8 = undefined;78 var buf: [buf_len]u8 = undefined;
78 mem.writeIntNative(val_type, &buf, val);79 mem.writeIntNative(val_type, &buf, val);
79 self.blake3.update(&buf);80 self.cache_buf(&buf);
80 } else {81 } else {
81 @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice.");82 @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice.");
82 },83 },
84 .Bool => {
85 var buf: [1]u8 = undefined;
86 buf[0] = if (val) 1 else 0;
87 self.blake3.update(&buf);
88 },
83 else => @compileError("Unsupported type"),89 else => @compileError("Unsupported type"),
84 }90 }
85
86 self.blake3.update(&[_]u8{0});
87 }91 }
8892
89 pub fn cache_file(self: *@This(), file_path: []const u8) !void {93 pub fn cache_file(self: *@This(), file_path: []const u8) !void {
...@@ -92,7 +96,7 @@ pub const CacheHash = struct {...@@ -92,7 +96,7 @@ pub const CacheHash = struct {
92 var cache_hash_file = try self.files.addOne();96 var cache_hash_file = try self.files.addOne();
93 cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path});97 cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path});
9498
95 try self.cache_buf(cache_hash_file.path.?);99 self.cache_buf(cache_hash_file.path.?);
96 }100 }
97101
98 pub fn hit(self: *@This(), out_digest: *ArrayList(u8)) !bool {102 pub fn hit(self: *@This(), out_digest: *ArrayList(u8)) !bool {
...@@ -321,8 +325,9 @@ test "cache file and the recall it" {...@@ -321,8 +325,9 @@ test "cache file and the recall it" {
321 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);325 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
322 defer ch.release();326 defer ch.release();
323327
324 try ch.cache(@as(u16, 1234));328 ch.cache(true);
325 try ch.cache_buf("1234");329 ch.cache(@as(u16, 1234));
330 ch.cache_buf("1234");
326 try ch.cache_file("test.txt");331 try ch.cache_file("test.txt");
327332
328 // There should be nothing in the cache333 // There should be nothing in the cache
...@@ -334,8 +339,9 @@ test "cache file and the recall it" {...@@ -334,8 +339,9 @@ test "cache file and the recall it" {
334 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);339 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
335 defer ch.release();340 defer ch.release();
336341
337 try ch.cache(@as(u16, 1234));342 ch.cache(true);
338 try ch.cache_buf("1234");343 ch.cache(@as(u16, 1234));
344 ch.cache_buf("1234");
339 try ch.cache_file("test.txt");345 try ch.cache_file("test.txt");
340346
341 // Cache hit! We just "built" the same file347 // Cache hit! We just "built" the same file