From 60b4b39f5a78661e2b9d222cf6ba7d9d5d7511ed Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Wed, 5 Oct 2022 23:00:35 +0200 Subject: [PATCH] [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 --- clang-tools-extra/clangd/index/SymbolID.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clangd/index/SymbolID.h b/clang-tools-extra/clangd/index/SymbolID.h index d151208..e8aa462 100644 --- a/clang-tools-extra/clangd/index/SymbolID.h +++ b/clang-tools-extra/clangd/index/SymbolID.h @@ -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(HashValue) < + llvm::bit_cast(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 HashValue{}; }; -- 2.7.4