Debian Codegraph @

include/plink2_htable.h

raw
1 #ifndef __PLINK2_HTABLE_H__
2 #define __PLINK2_HTABLE_H__
3
4 // This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell,
5 // Christopher Chang.
6 //
7 // This program is free software: you can redistribute it and/or modify it
8 // under the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 // This implements a hash table over a const char* const* array of strings.
22
23 #include "plink2_base.h"
24
25 #ifdef __cplusplus
26 namespace plink2 {
27 #endif
28
29 // copy_subset() doesn't exist since a loop of the form
30 // uintptr_t uidx_base = 0;
31 // uintptr_t cur_bits = subset_mask[0];
32 // for (uint32_t idx = 0; idx != subset_size; ++idx) {
33 // const uintptr_t uidx = BitIter1(subset_mask, &uidx_base, &cur_bits);
34 // *target_iter++ = source_arr[uidx];
35 // }
36 // seems to compile better?
37 //
38 // similarly, copy_when_nonmissing() retired in favor of genoarr_to_nonmissing
39 // followed by a for loop
40
41 // Benchmark results justify replacing this with XXH3 when the latter is ready.
42 // Not considering it ready yet since the development XXH_INLINE_ALL setup
43 // isn't very friendly.
44 //
45 // (Can also try using _mm_aesenc_si128() in a similar manner to the Golang
46 // runtime when SSE4.2+ is available, and otherwise keeping Murmurhash. See
47 // aeshashbody() in https://golang.org/src/runtime/asm_amd64.s .)
48 //
49 // Eventually want this to be a constexpr? Seems painful to make that work,
50 // though.
51 uint32_t Hash32(const void* key, uint32_t len);
52
53 // see http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
54 // Note that this is a bit more vulnerable to adversarial input: modulo
55 // reduction requires lots of hash collisions (or near-collisions) or known
56 // hash table size, while this can be attacked without knowledge of the table
57 // size. But it doesn't really matter; either way, you'd need to add something
58 // like a randomly generated salt if you care about defending.
59 HEADER_INLINE uint32_t Hashceil(const char* idstr, uint32_t idlen, uint32_t htable_size) {
60 return (S_CAST(uint64_t, Hash32(idstr, idlen)) * htable_size) >> 32;
61 }
62
63 // In most cases, plink2 represents an array of strings in one of the following
64 // two ways:
65 // (const char* strbox, uint32_t str_ct, uintptr_t max_str_blen):
66 // null-terminated string #x starts at &(strbox[x * max_str_blen)), where
67 // x is a 0-based index.
68 // (const char* const* item_ids, uint32_t str_ct):
69 // null-terminated string #x starts at item_ids[x].
70 // When we need to perform string -> string-index lookups into the array, we
71 // construct a hashmap as follows:
72 // - Allocate a uint32_t array of htable_size >= 2 * str_ct, and initialize all
73 // entries to UINT32_MAX to mark them empty.
74 // - Iterate through the array, computing hashval := Hashceil(str, strlen(str),
75 // htable_size) for each string, and then setting htable[hashval] :=
76 // string-index whenever that htable entry is empty. When there is a
77 // conflict, use linear probing (increment hashval until an empty table cell
78 // is found, wrapping around from (htable_size - 1) to 0).
79 // - PLINK 1.9 used quadratic probing, but that's been scrapped since
80 // benchmark results suggest that it has no meaningful advantage. We
81 // aren't dealing with adversarial input...
82 // The "StrboxHtable" functions below work with the const char* strbox
83 // string-array representation, while "IdHtable" functions work with the const
84 // char* const* item_ids representation.
85
86 // uintptr_t geqprime(uintptr_t floor);
87
88 // assumes ceil is odd and greater than 4. Returns the first prime <= ceil.
89 // uintptr_t leqprime(uintptr_t ceil);
90
91 HEADER_INLINE uint32_t GetHtableMinSize(uintptr_t item_ct) {
92 // Don't need primes any more.
93 return item_ct * 2;
94 }
95
96 // load factor ~22% seems to yield the best speed/space tradeoff on my test
97 // machines
98 HEADER_INLINE uint32_t GetHtableFastSize(uint32_t item_ct) {
99 if (item_ct < 954437176) {
100 return (item_ct * 9LLU) / 2;
101 }
102 return 4294967290U;
103 }
104
105 // cur_id DOES need to be null-terminated
106 uint32_t IdHtableFind(const char* cur_id, const char* const* item_ids, const uint32_t* id_htable, uint32_t cur_id_slen, uint32_t id_htable_size);
107
108 // item_ids overread must be ok. In exchange, cur_id doesn't need to be
109 // null-terminated any more.
110 uint32_t IdHtableFindNnt(const char* cur_id, const char* const* item_ids, const uint32_t* id_htable, uint32_t cur_id_slen, uint32_t id_htable_size);
111
112 HEADER_INLINE void HtableAddNondup(const char* cur_id, uint32_t cur_id_slen, uint32_t id_htable_size, uint32_t value, uint32_t* id_htable) {
113 for (uint32_t hashval = Hashceil(cur_id, cur_id_slen, id_htable_size); ; ) {
114 const uint32_t cur_htable_entry = id_htable[hashval];
115 if (cur_htable_entry == UINT32_MAX) {
116 id_htable[hashval] = value;
117 return;
118 }
119 if (++hashval == id_htable_size) {
120 hashval = 0;
121 }
122 }
123 }
124
125 // Assumes cur_id is null-terminated.
126 // item_ids overread must be ok.
127 // Returns string-index if cur_id is already in the table, UINT32_MAX if it was
128 // added.
129 uint32_t IdHtableAdd(const char* cur_id, const char* const* item_ids, uint32_t cur_id_slen, uint32_t id_htable_size, uint32_t value, uint32_t* id_htable);
130
131 // Does not require cur_id to be null-terminated.
132 uint32_t IdHtableAddNnt(const char* cur_id, const char* const* item_ids, uint32_t cur_id_slen, uint32_t id_htable_size, uint32_t value, uint32_t* id_htable);
133
134 CONSTI32(kDupstoreBlockSize, 65536);
135 CONSTI32(kDupstoreThreadWkspace, kDupstoreBlockSize * 2 * sizeof(int32_t));
136
137 uint32_t PopulateIdHtableMtDupstoreThreadCt(uint32_t max_thread_ct, uint32_t item_ct);
138
139 // store_all_dups currently must be true for dup_ct to be set, but this is easy
140 // to change
141 PglErr PopulateIdHtableMt(unsigned char* arena_top, const uintptr_t* subset_mask, const char* const* item_ids, uintptr_t item_ct, uint32_t store_all_dups, uint32_t id_htable_size, uint32_t thread_ct, unsigned char** arena_bottom_ptr, uint32_t* id_htable, uint32_t* dup_ct_ptr);
142
143 PglErr MakeNondupHtable(const uintptr_t* subset_mask, const char* const* item_ids, uintptr_t item_ct, uint32_t id_htable_size, uint32_t max_thread_ct, uint32_t* id_htable, uint32_t* dup_found_ptr);
144
145 PglErr AllocAndPopulateNondupHtableMt(unsigned char* arena_top, const uintptr_t* subset_mask, const char* const* item_ids, uintptr_t item_ct, uint32_t max_thread_ct, unsigned char** arena_bottom_ptr, uint32_t** id_htable_ptr, uint32_t* id_htable_size_ptr, uint32_t* dup_found_ptr);
146
147 HEADER_INLINE PglErr CheckIdUniqueness(unsigned char* arena_bottom, unsigned char* arena_top, const uintptr_t* subset_mask, const char* const* item_ids, uintptr_t item_ct, uint32_t max_thread_ct, uint32_t* dup_found_ptr) {
148 uint32_t* id_htable;
149 uint32_t id_htable_size;
150 return AllocAndPopulateNondupHtableMt(arena_top, subset_mask, item_ids, item_ct, max_thread_ct, &arena_bottom, &id_htable, &id_htable_size, dup_found_ptr);
151 }
152
153 // item_set is in/out: it should be set to item_include on input, but it's
154 // overwritten with the loaded item set on successful return.
155 // errbuf assumed to have length at least 2 * kMaxMediumLine.
156 PglErr NondupIdLoad(unsigned char* arena_bottom, unsigned char* arena_top, const char* const* item_ids, const char* fname, uint32_t raw_item_ct, uint32_t item_ct, uint32_t max_thread_ct, uintptr_t* item_set, uint32_t* dup_found_ptr, char* errbuf);
157
158 #ifdef __cplusplus
159 } // namespace plink2
160 #endif
161
162 #endif // __PLINK2_HTABLE_H__