[clangd] Avoid lexicographic compare when sorting SymbolIDs. NFC
authorSam McCall <sam.mccall@gmail.com>
Wed, 5 Oct 2022 21:00:35 +0000 (23:00 +0200)
committerSam McCall <sam.mccall@gmail.com>
Wed, 5 Oct 2022 21:02:28 +0000 (23:02 +0200)
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

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

index d151208..e8aa462 100644 (file)
@@ -39,7 +39,9 @@ public:
   }
   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 @@ public:
   explicit operator bool() const { return !isNull(); }
 
 private:
+  using IntTy = uint64_t;
+  static_assert(sizeof(IntTy) == RawSize);
   std::array<uint8_t, RawSize> HashValue{};
 };