authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-05 21:32:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
log8a77c1c637b07a2ff4f8f8981f9f732756cc38f6
treee591d37266273f7f2b566bbe221726595000b947
parent3158dc424e48d3da52d9a8f99cba65a4ef850f2e

Add `cache` method; add support for caching integers


1 files changed, 18 insertions(+), 45 deletions(-)

lib/std/cache_hash.zig+18-45
...@@ -64,13 +64,26 @@ pub const CacheHash = struct {...@@ -64,13 +64,26 @@ pub const CacheHash = struct {
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 var temp_buffer = try self.alloc.alloc(u8, val.len + 1);67 self.blake3.update(val);
68 defer self.alloc.free(temp_buffer);68 }
69
70 pub fn cache(self: *@This(), val: var) !void {
71 debug.assert(self.manifest_file_path == null);
6972
70 mem.copy(u8, temp_buffer, val);73 const val_type = @TypeOf(val);
71 temp_buffer[val.len] = 0;74 switch (@typeInfo(val_type)) {
75 .Int => |int_info| if (int_info.bits != 0 and int_info.bits % 8 == 0) {
76 const buf_len = @divExact(int_info.bits, 8);
77 var buf: [buf_len]u8 = undefined;
78 mem.writeIntNative(val_type, &buf, val);
79 self.blake3.update(&buf);
80 } else {
81 @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice.");
82 },
83 else => @compileError("Unsupported type"),
84 }
7285
73 self.blake3.update(temp_buffer);86 self.blake3.update(&[_]u8{0});
74 }87 }
7588
76 pub fn cache_file(self: *@This(), file_path: []const u8) !void {89 pub fn cache_file(self: *@This(), file_path: []const u8) !void {
...@@ -287,43 +300,3 @@ fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const File) !void {...@@ -287,43 +300,3 @@ fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const File) !void {
287300
288 blake3.final(bin_digest);301 blake3.final(bin_digest);
289}302}
290
291test "see if imported" {
292 const cwd = fs.cwd();
293
294 const temp_manifest_dir = "temp_manifest_dir";
295
296 try cwd.writeFile("test.txt", "Hello, world!\n");
297
298 var digest1 = try ArrayList(u8).initCapacity(testing.allocator, 32);
299 defer digest1.deinit();
300 var digest2 = try ArrayList(u8).initCapacity(testing.allocator, 32);
301 defer digest2.deinit();
302
303 {
304 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
305 defer ch.release();
306
307 try ch.cache_buf("1234");
308 try ch.cache_file("test.txt");
309
310 // There should be nothing in the cache
311 debug.assert((try ch.hit(&digest1)) == false);
312
313 try ch.final(&digest1);
314 }
315 {
316 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
317 defer ch.release();
318
319 try ch.cache_buf("1234");
320 try ch.cache_file("test.txt");
321
322 // Cache hit! We just "built" the same file
323 debug.assert((try ch.hit(&digest2)) == true);
324 }
325
326 debug.assert(mem.eql(u8, digest1.toSlice(), digest2.toSlice()));
327
328 try cwd.deleteTree(temp_manifest_dir);
329}