authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-21 15:08:15+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-22 15:10:33-04:00
logf540dc1b7ebc1663ef5d3823da4630ff51c697b6
tree9ed45cca43ebad7f0328ff88b12e2fe5ab54d837
parent0fa3cfdb4aa04bf92c5d9344cd4d265ccb40e0dc

cache_hash: hash function change

This makes the `cache_hash` hash function easier to replace. BLAKE3 would be a natural fit for hashing large files, but: - second preimage resistance is not necessary for the cache_hash use cases - our BLAKE3 implementation is currently very slow Switch to SipHash128, which gives us an immediate speed boost.

1 files changed, 38 insertions(+), 32 deletions(-)

lib/std/cache_hash.zig+38-32
......@@ -4,7 +4,8 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66const std = @import("std.zig");
7const Blake3 = std.crypto.hash.Blake3;
7const crypto = std.crypto;
8const Hasher = crypto.auth.siphash.SipHash128(1, 3); // provides enough collision resistance for the CacheHash use cases, while being one of our fastest options right now
89const fs = std.fs;
910const base64 = std.base64;
1011const ArrayList = std.ArrayList;
......@@ -16,9 +17,8 @@ const Allocator = std.mem.Allocator;
1617
1718const base64_encoder = fs.base64_encoder;
1819const base64_decoder = fs.base64_decoder;
19/// This is 70 more bits than UUIDs. For an analysis of probability of collisions, see:
20/// https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions
21const BIN_DIGEST_LEN = 24;
20/// This is 128 bits - Even with 2^54 cache entries, the probably of a collision would be under 10^-6
21const BIN_DIGEST_LEN = 16;
2222const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN);
2323
2424const MANIFEST_FILE_SIZE_MAX = 50 * 1024 * 1024;
......@@ -43,9 +43,13 @@ pub const File = struct {
4343 }
4444};
4545
46/// CacheHash manages project-local `zig-cache` directories.
47/// This is not a general-purpose cache.
48/// It was designed to be fast and simple, not to withstand attacks using specially-crafted input.
4649pub const CacheHash = struct {
4750 allocator: *Allocator,
48 blake3: Blake3,
51 hasher_init: Hasher, // initial state, that can be copied
52 hasher: Hasher, // current state for incremental hashing
4953 manifest_dir: fs.Dir,
5054 manifest_file: ?fs.File,
5155 manifest_dirty: bool,
......@@ -54,9 +58,11 @@ pub const CacheHash = struct {
5458
5559 /// Be sure to call release after successful initialization.
5660 pub fn init(allocator: *Allocator, dir: fs.Dir, manifest_dir_path: []const u8) !CacheHash {
61 const hasher_init = Hasher.init(&[_]u8{0} ** Hasher.minimum_key_length);
5762 return CacheHash{
5863 .allocator = allocator,
59 .blake3 = Blake3.init(.{}),
64 .hasher_init = hasher_init,
65 .hasher = hasher_init,
6066 .manifest_dir = try dir.makeOpenPath(manifest_dir_path, .{}),
6167 .manifest_file = null,
6268 .manifest_dirty = false,
......@@ -69,8 +75,8 @@ pub const CacheHash = struct {
6975 pub fn addSlice(self: *CacheHash, val: []const u8) void {
7076 assert(self.manifest_file == null);
7177
72 self.blake3.update(val);
73 self.blake3.update(&[_]u8{0});
78 self.hasher.update(val);
79 self.hasher.update(&[_]u8{0});
7480 }
7581
7682 /// Convert the input value into bytes and record it as a dependency of the
......@@ -133,12 +139,12 @@ pub const CacheHash = struct {
133139 assert(self.manifest_file == null);
134140
135141 var bin_digest: [BIN_DIGEST_LEN]u8 = undefined;
136 self.blake3.final(&bin_digest);
142 self.hasher.final(&bin_digest);
137143
138144 base64_encoder.encode(self.b64_digest[0..], &bin_digest);
139145
140 self.blake3 = Blake3.init(.{});
141 self.blake3.update(&bin_digest);
146 self.hasher = self.hasher_init;
147 self.hasher.update(&bin_digest);
142148
143149 const manifest_file_path = try fmt.allocPrint(self.allocator, "{}.txt", .{self.b64_digest});
144150 defer self.allocator.free(manifest_file_path);
......@@ -238,7 +244,7 @@ pub const CacheHash = struct {
238244 }
239245
240246 var actual_digest: [BIN_DIGEST_LEN]u8 = undefined;
241 try hashFile(this_file, &actual_digest);
247 try hashFile(this_file, &actual_digest, self.hasher_init);
242248
243249 if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) {
244250 cache_hash_file.bin_digest = actual_digest;
......@@ -248,7 +254,7 @@ pub const CacheHash = struct {
248254 }
249255
250256 if (!any_file_changed) {
251 self.blake3.update(&cache_hash_file.bin_digest);
257 self.hasher.update(&cache_hash_file.bin_digest);
252258 }
253259 }
254260
......@@ -256,8 +262,8 @@ pub const CacheHash = struct {
256262 // cache miss
257263 // keep the manifest file open
258264 // reset the hash
259 self.blake3 = Blake3.init(.{});
260 self.blake3.update(&bin_digest);
265 self.hasher = self.hasher_init;
266 self.hasher.update(&bin_digest);
261267
262268 // Remove files not in the initial hash
263269 for (self.files.items[input_file_count..]) |*file| {
......@@ -266,7 +272,7 @@ pub const CacheHash = struct {
266272 self.files.shrink(input_file_count);
267273
268274 for (self.files.items) |file| {
269 self.blake3.update(&file.bin_digest);
275 self.hasher.update(&file.bin_digest);
270276 }
271277 return null;
272278 }
......@@ -304,23 +310,23 @@ pub const CacheHash = struct {
304310
305311 // Hash while reading from disk, to keep the contents in the cpu cache while
306312 // doing hashing.
307 var blake3 = Blake3.init(.{});
313 var hasher = self.hasher_init;
308314 var off: usize = 0;
309315 while (true) {
310316 // give me everything you've got, captain
311317 const bytes_read = try file.read(contents[off..]);
312318 if (bytes_read == 0) break;
313 blake3.update(contents[off..][0..bytes_read]);
319 hasher.update(contents[off..][0..bytes_read]);
314320 off += bytes_read;
315321 }
316 blake3.final(&ch_file.bin_digest);
322 hasher.final(&ch_file.bin_digest);
317323
318324 ch_file.contents = contents;
319325 } else {
320 try hashFile(file, &ch_file.bin_digest);
326 try hashFile(file, &ch_file.bin_digest, self.hasher_init);
321327 }
322328
323 self.blake3.update(&ch_file.bin_digest);
329 self.hasher.update(&ch_file.bin_digest);
324330 }
325331
326332 /// Add a file as a dependency of process being cached, after the initial hash has been
......@@ -382,7 +388,7 @@ pub const CacheHash = struct {
382388 // the artifacts to cache.
383389
384390 var bin_digest: [BIN_DIGEST_LEN]u8 = undefined;
385 self.blake3.final(&bin_digest);
391 self.hasher.final(&bin_digest);
386392
387393 var out_digest: [BASE64_DIGEST_LEN]u8 = undefined;
388394 base64_encoder.encode(&out_digest, &bin_digest);
......@@ -433,17 +439,17 @@ pub const CacheHash = struct {
433439 }
434440};
435441
436fn hashFile(file: fs.File, bin_digest: []u8) !void {
437 var blake3 = Blake3.init(.{});
442fn hashFile(file: fs.File, bin_digest: []u8, hasher_init: anytype) !void {
438443 var buf: [1024]u8 = undefined;
439444
445 var hasher = hasher_init;
440446 while (true) {
441447 const bytes_read = try file.read(&buf);
442448 if (bytes_read == 0) break;
443 blake3.update(buf[0..bytes_read]);
449 hasher.update(buf[0..bytes_read]);
444450 }
445451
446 blake3.final(bin_digest);
452 hasher.final(bin_digest);
447453}
448454
449455/// If the wall clock time, rounded to the same precision as the
......@@ -507,7 +513,7 @@ test "cache file and then recall it" {
507513 _ = try ch.addFile(temp_file, null);
508514
509515 // There should be nothing in the cache
510 testing.expectEqual(@as(?[32]u8, null), try ch.hit());
516 testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit());
511517
512518 digest1 = ch.final();
513519 }
......@@ -575,7 +581,7 @@ test "check that changing a file makes cache fail" {
575581 const temp_file_idx = try ch.addFile(temp_file, 100);
576582
577583 // There should be nothing in the cache
578 testing.expectEqual(@as(?[32]u8, null), try ch.hit());
584 testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit());
579585
580586 testing.expect(mem.eql(u8, original_temp_file_contents, ch.files.items[temp_file_idx].contents.?));
581587
......@@ -592,7 +598,7 @@ test "check that changing a file makes cache fail" {
592598 const temp_file_idx = try ch.addFile(temp_file, 100);
593599
594600 // A file that we depend on has been updated, so the cache should not contain an entry for it
595 testing.expectEqual(@as(?[32]u8, null), try ch.hit());
601 testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit());
596602
597603 // The cache system does not keep the contents of re-hashed input files.
598604 testing.expect(ch.files.items[temp_file_idx].contents == null);
......@@ -625,7 +631,7 @@ test "no file inputs" {
625631 ch.add("1234");
626632
627633 // There should be nothing in the cache
628 testing.expectEqual(@as(?[32]u8, null), try ch.hit());
634 testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit());
629635
630636 digest1 = ch.final();
631637 }
......@@ -672,7 +678,7 @@ test "CacheHashes with files added after initial hash work" {
672678 _ = try ch.addFile(temp_file1, null);
673679
674680 // There should be nothing in the cache
675 testing.expectEqual(@as(?[32]u8, null), try ch.hit());
681 testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit());
676682
677683 _ = try ch.addFilePost(temp_file2);
678684
......@@ -705,7 +711,7 @@ test "CacheHashes with files added after initial hash work" {
705711 _ = try ch.addFile(temp_file1, null);
706712
707713 // A file that we depend on has been updated, so the cache should not contain an entry for it
708 testing.expectEqual(@as(?[32]u8, null), try ch.hit());
714 testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit());
709715
710716 _ = try ch.addFilePost(temp_file2);
711717