[clang-tools-extra] 60b4b39 - [clangd] Avoid lexicographic compare when sorting SymbolIDs. NFC

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 5 14:02:39 PDT 2022


Author: Sam McCall
Date: 2022-10-05T23:02:28+02:00
New Revision: 60b4b39f5a78661e2b9d222cf6ba7d9d5d7511ed

URL: https://github.com/llvm/llvm-project/commit/60b4b39f5a78661e2b9d222cf6ba7d9d5d7511ed
DIFF: https://github.com/llvm/llvm-project/commit/60b4b39f5a78661e2b9d222cf6ba7d9d5d7511ed.diff

LOG: [clangd] Avoid lexicographic compare when sorting SymbolIDs. NFC

These are 8 bytes and we don't care about the actual ordering, so use
integer compare.

The array generated code has some extra byte swaps (clang), calls memcmp (gcc)
or inlines a big chain of comparisons (MSVC): https://godbolt.org/z/e79r6jM6K

Added: 
    

Modified: 
    clang-tools-extra/clangd/index/SymbolID.h

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/index/SymbolID.h b/clang-tools-extra/clangd/index/SymbolID.h
index d1512080edbfb..e8aa462e96c17 100644
--- a/clang-tools-extra/clangd/index/SymbolID.h
+++ b/clang-tools-extra/clangd/index/SymbolID.h
@@ -39,7 +39,9 @@ class SymbolID {
   }
   bool operator!=(const SymbolID &Sym) const { return !(*this == Sym); }
   bool operator<(const SymbolID &Sym) const {
-    return HashValue < Sym.HashValue;
+    // Avoid lexicographic compare which requires swapping bytes or even memcmp!
+    return llvm::bit_cast<IntTy>(HashValue) <
+           llvm::bit_cast<IntTy>(Sym.HashValue);
   }
 
   // The stored hash is truncated to RawSize bytes.
@@ -56,6 +58,8 @@ class SymbolID {
   explicit operator bool() const { return !isNull(); }
 
 private:
+  using IntTy = uint64_t;
+  static_assert(sizeof(IntTy) == RawSize);
   std::array<uint8_t, RawSize> HashValue{};
 };
 


        


More information about the cfe-commits mailing list