From 2ac1356d4daa989706150ade3446fe3e74546130 Mon Sep 17 00:00:00 2001 From: Connal de Souza Date: Fri, 19 Apr 2024 19:33:53 +0000 Subject: [PATCH] Optimize ExtractOffset() by avoiding storing the mask. Currently, the constant mask is stored to memory and then immediately read back. Since the read address is often not aligned to the store address, this can cause an expensive Read-after-Write hazard. Although the load is contained entirely within the store, meaning that store forwarding can occur and there are no throughput penalties, there is still a latency penalty. The increased latency matters because of the pointer-chasing pattern in decompression here. https://godbolt.org/z/e71jhE3bj PiperOrigin-RevId: 626440765 --- snappy.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snappy.cc b/snappy.cc index 708c57d..d689aac 100644 --- a/snappy.cc +++ b/snappy.cc @@ -1344,11 +1344,11 @@ inline size_t AdvanceToNextTagX86Optimized(const uint8_t** ip_p, size_t* tag) { // Extract the offset for copy-1 and copy-2 returns 0 for literals or copy-4. inline uint32_t ExtractOffset(uint32_t val, size_t tag_type) { - // For x86 non-static storage works better. For ARM static storage is better. + // For Arm non-static storage works better. For x86 static storage is better. // TODO: Once the array is recognized as a register, improve the // readability for x86. #if defined(__x86_64__) - constexpr uint64_t kExtractMasksCombined = 0x0000FFFF00FF0000ull; + static constexpr uint64_t kExtractMasksCombined = 0x0000FFFF00FF0000ull; uint16_t result; memcpy(&result, reinterpret_cast(&kExtractMasksCombined) + 2 * tag_type,