Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate duplicate hash calculations for getSymbolInSymbolMaps #1801

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/include/khash.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ static const double __ac_HASH_UPPER = 0.77;
h->size = h->n_occupied = 0; \
} \
} \
SCOPE khint_t kh_hash_##name(khkey_t key) { \
return __hash_func(key); \
} \
SCOPE khint_t kh_get_##name##_with_hash(const kh_##name##_t *h, khkey_t key, khint_t hash) { \
if (h->n_buckets) { \
khint_t k = hash, i, last, mask, step = 0; \
mask = h->n_buckets - 1; \
i = k & mask; \
last = i; \
while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
i = (i + (++step)) & mask; \
if (i == last) return h->n_buckets; \
} \
return __ac_iseither(h->flags, i)? h->n_buckets : i; \
} else return 0; \
} \
SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
{ \
if (h->n_buckets) { \
Expand Down Expand Up @@ -462,6 +478,24 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key)
*/
#define kh_put(name, h, k, r) kh_put_##name(h, k, r)

/*! @function
@abstract Hash a key.
@param name Name of the hash table [symbol]
@param key Key [type of keys]
@return Hash value [khint_t]
*/
#define kh_hash(name, key) kh_hash_##name(key)

/*! @function
@abstract Retrieve a key from the hash table with a given hash value.
@param name Name of the hash table [symbol]
@param h Pointer to the hash table [khash_t(name)*]
@param k Key [type of keys]
@param hash Hash value [khint_t]
@return Iterator to the found element, or kh_end(h) if the element is absent [khint_t]
*/
#define kh_get_with_hash(name, h, k, hash) kh_get_##name##_with_hash(h, k, hash)

/*! @function
@abstract Retrieve a key from the hash table.
@param name Name of the hash table [symbol]
Expand Down
15 changes: 9 additions & 6 deletions src/librarian/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,9 +836,10 @@ static int getSymbolInDataMaps(library_t*lib, const char* name, int noweak, uint
}
static int getSymbolInSymbolMaps(library_t*lib, const char* name, int noweak, uintptr_t *addr, uintptr_t *size, int* weak)
{
const khint_t hash = kh_hash(symbolmap, name);
void* symbol;
// check in mysymbolmap
khint_t k = kh_get(symbolmap, lib->w.mysymbolmap, name);
khint_t k = kh_get_with_hash(symbolmap, lib->w.mysymbolmap, name, hash);
if (k!=kh_end(lib->w.mysymbolmap)) {
symbol1_t *s = &kh_value(lib->w.mysymbolmap, k);
if(!s->resolved) {
Expand Down Expand Up @@ -866,7 +867,7 @@ static int getSymbolInSymbolMaps(library_t*lib, const char* name, int noweak, ui
return 1;
}
// check in stsymbolmap (return struct...)
k = kh_get(symbolmap, lib->w.stsymbolmap, name);
k = kh_get_with_hash(symbolmap, lib->w.stsymbolmap, name, hash);
if (k!=kh_end(lib->w.stsymbolmap)) {
symbol1_t *s = &kh_value(lib->w.stsymbolmap, k);
if(!s->resolved) {
Expand Down Expand Up @@ -894,7 +895,7 @@ static int getSymbolInSymbolMaps(library_t*lib, const char* name, int noweak, ui
return 1;
}
// check in symbolmap
k = kh_get(symbolmap, lib->w.symbolmap, name);
k = kh_get_with_hash(symbolmap, lib->w.symbolmap, name, hash);
if (k!=kh_end(lib->w.symbolmap)) {
symbol1_t *s = &kh_value(lib->w.symbolmap, k);
if(!s->resolved) {
Expand Down Expand Up @@ -931,7 +932,7 @@ static int getSymbolInSymbolMaps(library_t*lib, const char* name, int noweak, ui
}
if(!noweak) {
// check in wmysymbolmap
khint_t k = kh_get(symbolmap, lib->w.wmysymbolmap, name);
khint_t k = kh_get_with_hash(symbolmap, lib->w.wmysymbolmap, name, hash);
if (k!=kh_end(lib->w.wmysymbolmap)) {
symbol1_t *s = &kh_value(lib->w.wmysymbolmap, k);
if(!s->resolved) {
Expand All @@ -958,7 +959,7 @@ static int getSymbolInSymbolMaps(library_t*lib, const char* name, int noweak, ui
*weak = 1;
return 1;
}
k = kh_get(symbolmap, lib->w.wsymbolmap, name);
k = kh_get_with_hash(symbolmap, lib->w.wsymbolmap, name, hash);
if (k!=kh_end(lib->w.wsymbolmap)) {
symbol1_t *s = &kh_value(lib->w.wsymbolmap, k);
if(!s->resolved) {
Expand Down Expand Up @@ -995,7 +996,9 @@ static int getSymbolInSymbolMaps(library_t*lib, const char* name, int noweak, ui
}
}
// check in symbol2map
k = kh_get(symbol2map, lib->w.symbol2map, name);
//
// NOTE: symbol2map & symbolmap share the same hash function, so we can use the same hash
k = kh_get_with_hash(symbol2map, lib->w.symbol2map, name, hash);
if (k!=kh_end(lib->w.symbol2map)) {
symbol2_t *s = &kh_value(lib->w.symbol2map, k);
if(!noweak || !s->weak)
Expand Down