Skip to content

Commit

Permalink
Re-land: "[Support] Replace HashString with djbHash."
Browse files Browse the repository at this point in the history
This patch removes the HashString function from StringExtraces and
replaces its uses with calls to djbHash from DJB.h.

This change is *almost* NFC. While the algorithm is identical, the
djbHash implementation in StringExtras used 0 as its default seed while
the implementation in DJB uses 5381. The latter has been shown to result
in less collisions and improved avalanching and is used by the DWARF
accelerator tables.

Because some test were implicitly relying on the hash order, I've
reverted to using zero as a seed for the following two files:

  lld/include/lld/Core/SymbolTable.h
  llvm/lib/Support/StringMap.cpp

Differential revision: https://reviews.llvm.org/D43615

llvm-svn: 326091
  • Loading branch information
JDevlieghere committed Feb 26, 2018
1 parent 0c97f4e commit 560ce2c
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 221 deletions.
202 changes: 101 additions & 101 deletions clang/lib/Frontend/ASTUnit.cpp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions clang/lib/Frontend/CacheTokens.cpp
Expand Up @@ -21,8 +21,8 @@
#include "clang/Lex/Lexer.h"
#include "clang/Lex/PTHManager.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/DJB.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down Expand Up @@ -128,7 +128,7 @@ class FileEntryPTHEntryInfo {
typedef unsigned offset_type;

static hash_value_type ComputeHash(PTHEntryKeyVariant V) {
return llvm::HashString(V.getString());
return llvm::djbHash(V.getString());
}

static std::pair<unsigned,unsigned>
Expand Down Expand Up @@ -625,7 +625,7 @@ class PTHIdentifierTableTrait {
typedef unsigned offset_type;

static hash_value_type ComputeHash(PTHIdKey* key) {
return llvm::HashString(key->II->getName());
return llvm::djbHash(key->II->getName());
}

static std::pair<unsigned,unsigned>
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Lex/PTHLexer.cpp
Expand Up @@ -23,8 +23,8 @@
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Token.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DJB.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
Expand Down Expand Up @@ -145,7 +145,7 @@ bool PTHLexer::LexEndOfFile(Token &Result) {
ParsingPreprocessorDirective = false; // Done parsing the "line".
return true; // Have a token.
}

assert(!LexingRawMode);

// If we are in a #if directive, emit an error.
Expand Down Expand Up @@ -336,7 +336,7 @@ class PTHFileLookupCommonTrait {
using offset_type = unsigned;

static hash_value_type ComputeHash(internal_key_type x) {
return llvm::HashString(x.second);
return llvm::djbHash(x.second);
}

static std::pair<unsigned, unsigned>
Expand Down Expand Up @@ -396,7 +396,7 @@ class PTHManager::PTHStringLookupTrait {
}

static hash_value_type ComputeHash(const internal_key_type& a) {
return llvm::HashString(StringRef(a.first, a.second));
return llvm::djbHash(StringRef(a.first, a.second));
}

// This hopefully will just get inlined and removed by the optimizer.
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Serialization/ASTCommon.cpp
Expand Up @@ -16,7 +16,7 @@
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Serialization/ASTDeserializationListener.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/DJB.h"

using namespace clang;

Expand Down Expand Up @@ -171,7 +171,7 @@ unsigned serialization::ComputeHash(Selector Sel) {
unsigned R = 5381;
for (unsigned I = 0; I != N; ++I)
if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
R = llvm::HashString(II->getName(), R);
R = llvm::djbHash(II->getName(), R);
return R;
}

Expand Down Expand Up @@ -231,7 +231,7 @@ serialization::getDefinitiveDeclContext(const DeclContext *DC) {
default:
llvm_unreachable("Unhandled DeclContext in AST reader");
}

llvm_unreachable("Unhandled decl kind");
}

Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Serialization/ASTReader.cpp
Expand Up @@ -106,6 +106,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DJB.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -870,7 +871,7 @@ ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
}

unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
return llvm::HashString(a);
return llvm::djbHash(a);
}

std::pair<unsigned, unsigned>
Expand Down Expand Up @@ -3226,7 +3227,7 @@ ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
PP.getPreprocessingRecord()->SetExternalSource(*this);
F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);

if (F.NumPreprocessedSkippedRanges > 0)
GlobalSkippedRangeMap.insert(
std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
Expand Down

0 comments on commit 560ce2c

Please sign in to comment.