From 9d090613012bf2bc09181ae24bc5856f06e8e8d7 Mon Sep 17 00:00:00 2001 From: ezbr <69321728+ezbr@users.noreply.github.com> Date: Mon, 30 Dec 2024 23:32:13 -0500 Subject: [PATCH] Avoid misaligned loads from StaticRandomData in the size [4, 8] hashing case. (#4743) Avoid misaligned loads from StaticRandomData in the size [4, 8] hashing case. We can use aligned loads in this case for lower latency. We introduce the SampleAlignedRandomData function for this purpose. --- common/hashing.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/common/hashing.h b/common/hashing.h index aea263b87f00..1c87a820a318 100644 --- a/common/hashing.h +++ b/common/hashing.h @@ -387,6 +387,14 @@ class Hasher { return data; } + // As above, but for small offsets, we can use aligned loads, which are + // faster. The offset must be in the range [0, 8). + static auto SampleAlignedRandomData(ssize_t offset) -> uint64_t { + CARBON_DCHECK(static_cast(offset) < + sizeof(StaticRandomData) / sizeof(uint64_t)); + return StaticRandomData[offset]; + } + // Random data taken from the hexadecimal digits of Pi's fractional component, // written in lexical order for convenience of reading. The resulting // byte-stream will be different due to little-endian integers. These can be @@ -950,7 +958,7 @@ inline auto Hasher::HashSizedBytes(llvm::ArrayRef bytes) -> void { // Note that we don't drop to the `WeakMix` routine here because we want // to use sampled random data to encode the size, which may not be as // effective without the full 128-bit folded result. - buffer = Mix(data ^ buffer, SampleRandomData(size)); + buffer = Mix(data ^ buffer, SampleAlignedRandomData(size - 1)); CARBON_MCA_END("dynamic-8b"); return; }