simplehash.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /*
  2. * simplehash.h
  3. *
  4. * When included this file generates a "templated" (by way of macros)
  5. * open-addressing hash table implementation specialized to user-defined
  6. * types.
  7. *
  8. * It's probably not worthwhile to generate such a specialized implementation
  9. * for hash tables that aren't performance or space sensitive.
  10. *
  11. * Compared to dynahash, simplehash has the following benefits:
  12. *
  13. * - Due to the "templated" code generation has known structure sizes and no
  14. * indirect function calls (which show up substantially in dynahash
  15. * profiles). These features considerably increase speed for small
  16. * entries.
  17. * - Open addressing has better CPU cache behavior than dynahash's chained
  18. * hashtables.
  19. * - The generated interface is type-safe and easier to use than dynahash,
  20. * though at the cost of more complex setup.
  21. * - Allocates memory in a MemoryContext or another allocator with a
  22. * malloc/free style interface (which isn't easily usable in a shared
  23. * memory context)
  24. * - Does not require the overhead of a separate memory context.
  25. *
  26. * Usage notes:
  27. *
  28. * To generate a hash-table and associated functions for a use case several
  29. * macros have to be #define'ed before this file is included. Including
  30. * the file #undef's all those, so a new hash table can be generated
  31. * afterwards.
  32. * The relevant parameters are:
  33. * - SH_PREFIX - prefix for all symbol names generated. A prefix of 'foo'
  34. * will result in hash table type 'foo_hash' and functions like
  35. * 'foo_insert'/'foo_lookup' and so forth.
  36. * - SH_ELEMENT_TYPE - type of the contained elements
  37. * - SH_KEY_TYPE - type of the hashtable's key
  38. * - SH_DECLARE - if defined function prototypes and type declarations are
  39. * generated
  40. * - SH_DEFINE - if defined function definitions are generated
  41. * - SH_SCOPE - in which scope (e.g. extern, static inline) do function
  42. * declarations reside
  43. * - SH_RAW_ALLOCATOR - if defined, memory contexts are not used; instead,
  44. * use this to allocate bytes. The allocator must zero the returned space.
  45. * - SH_USE_NONDEFAULT_ALLOCATOR - if defined no element allocator functions
  46. * are defined, so you can supply your own
  47. * The following parameters are only relevant when SH_DEFINE is defined:
  48. * - SH_KEY - name of the element in SH_ELEMENT_TYPE containing the hash key
  49. * - SH_EQUAL(table, a, b) - compare two table keys
  50. * - SH_HASH_KEY(table, key) - generate hash for the key
  51. * - SH_STORE_HASH - if defined the hash is stored in the elements
  52. * - SH_GET_HASH(tb, a) - return the field to store the hash in
  53. *
  54. * The element type is required to contain a "status" member that can store
  55. * the range of values defined in the SH_STATUS enum.
  56. *
  57. * While SH_STORE_HASH (and subsequently SH_GET_HASH) are optional, because
  58. * the hash table implementation needs to compare hashes to move elements
  59. * (particularly when growing the hash), it's preferable, if possible, to
  60. * store the element's hash in the element's data type. If the hash is so
  61. * stored, the hash table will also compare hashes before calling SH_EQUAL
  62. * when comparing two keys.
  63. *
  64. * For convenience the hash table create functions accept a void pointer
  65. * that will be stored in the hash table type's member private_data. This
  66. * allows callbacks to reference caller provided data.
  67. *
  68. * For examples of usage look at tidbitmap.c (file local definition) and
  69. * execnodes.h/execGrouping.c (exposed declaration, file local
  70. * implementation).
  71. *
  72. * Hash table design:
  73. *
  74. * The hash table design chosen is a variant of linear open-addressing. The
  75. * reason for doing so is that linear addressing is CPU cache & pipeline
  76. * friendly. The biggest disadvantage of simple linear addressing schemes
  77. * are highly variable lookup times due to clustering, and deletions
  78. * leaving a lot of tombstones around. To address these issues a variant
  79. * of "robin hood" hashing is employed. Robin hood hashing optimizes
  80. * chaining lengths by moving elements close to their optimal bucket
  81. * ("rich" elements), out of the way if a to-be-inserted element is further
  82. * away from its optimal position (i.e. it's "poor"). While that can make
  83. * insertions slower, the average lookup performance is a lot better, and
  84. * higher fill factors can be used in a still performant manner. To avoid
  85. * tombstones - which normally solve the issue that a deleted node's
  86. * presence is relevant to determine whether a lookup needs to continue
  87. * looking or is done - buckets following a deleted element are shifted
  88. * backwards, unless they're empty or already at their optimal position.
  89. *
  90. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  91. * Portions Copyright (c) 1994, Regents of the University of California
  92. *
  93. * src/include/lib/simplehash.h
  94. */
  95. #include "port/pg_bitutils.h"
  96. /* helpers */
  97. #define SH_MAKE_PREFIX(a) CppConcat(a,_)
  98. #define SH_MAKE_NAME(name) SH_MAKE_NAME_(SH_MAKE_PREFIX(SH_PREFIX),name)
  99. #define SH_MAKE_NAME_(a,b) CppConcat(a,b)
  100. /* name macros for: */
  101. /* type declarations */
  102. #define SH_TYPE SH_MAKE_NAME(hash)
  103. #define SH_STATUS SH_MAKE_NAME(status)
  104. #define SH_STATUS_EMPTY SH_MAKE_NAME(SH_EMPTY)
  105. #define SH_STATUS_IN_USE SH_MAKE_NAME(SH_IN_USE)
  106. #define SH_ITERATOR SH_MAKE_NAME(iterator)
  107. /* function declarations */
  108. #define SH_CREATE SH_MAKE_NAME(create)
  109. #define SH_DESTROY SH_MAKE_NAME(destroy)
  110. #define SH_RESET SH_MAKE_NAME(reset)
  111. #define SH_INSERT SH_MAKE_NAME(insert)
  112. #define SH_INSERT_HASH SH_MAKE_NAME(insert_hash)
  113. #define SH_DELETE_ITEM SH_MAKE_NAME(delete_item)
  114. #define SH_DELETE SH_MAKE_NAME(delete)
  115. #define SH_LOOKUP SH_MAKE_NAME(lookup)
  116. #define SH_LOOKUP_HASH SH_MAKE_NAME(lookup_hash)
  117. #define SH_GROW SH_MAKE_NAME(grow)
  118. #define SH_START_ITERATE SH_MAKE_NAME(start_iterate)
  119. #define SH_START_ITERATE_AT SH_MAKE_NAME(start_iterate_at)
  120. #define SH_ITERATE SH_MAKE_NAME(iterate)
  121. #define SH_ALLOCATE SH_MAKE_NAME(allocate)
  122. #define SH_FREE SH_MAKE_NAME(free)
  123. #define SH_STAT SH_MAKE_NAME(stat)
  124. /* internal helper functions (no externally visible prototypes) */
  125. #define SH_COMPUTE_PARAMETERS SH_MAKE_NAME(compute_parameters)
  126. #define SH_NEXT SH_MAKE_NAME(next)
  127. #define SH_PREV SH_MAKE_NAME(prev)
  128. #define SH_DISTANCE_FROM_OPTIMAL SH_MAKE_NAME(distance)
  129. #define SH_INITIAL_BUCKET SH_MAKE_NAME(initial_bucket)
  130. #define SH_ENTRY_HASH SH_MAKE_NAME(entry_hash)
  131. #define SH_INSERT_HASH_INTERNAL SH_MAKE_NAME(insert_hash_internal)
  132. #define SH_LOOKUP_HASH_INTERNAL SH_MAKE_NAME(lookup_hash_internal)
  133. /* generate forward declarations necessary to use the hash table */
  134. #ifdef SH_DECLARE
  135. /* type definitions */
  136. typedef struct SH_TYPE
  137. {
  138. /*
  139. * Size of data / bucket array, 64 bits to handle UINT32_MAX sized hash
  140. * tables. Note that the maximum number of elements is lower
  141. * (SH_MAX_FILLFACTOR)
  142. */
  143. uint64 size;
  144. /* how many elements have valid contents */
  145. uint32 members;
  146. /* mask for bucket and size calculations, based on size */
  147. uint32 sizemask;
  148. /* boundary after which to grow hashtable */
  149. uint32 grow_threshold;
  150. /* hash buckets */
  151. SH_ELEMENT_TYPE *data;
  152. #ifndef SH_RAW_ALLOCATOR
  153. /* memory context to use for allocations */
  154. MemoryContext ctx;
  155. #endif
  156. /* user defined data, useful for callbacks */
  157. void *private_data;
  158. } SH_TYPE;
  159. typedef enum SH_STATUS
  160. {
  161. SH_STATUS_EMPTY = 0x00,
  162. SH_STATUS_IN_USE = 0x01
  163. } SH_STATUS;
  164. typedef struct SH_ITERATOR
  165. {
  166. uint32 cur; /* current element */
  167. uint32 end;
  168. bool done; /* iterator exhausted? */
  169. } SH_ITERATOR;
  170. /* externally visible function prototypes */
  171. #ifdef SH_RAW_ALLOCATOR
  172. /* <prefix>_hash <prefix>_create(uint32 nelements, void *private_data) */
  173. SH_SCOPE SH_TYPE *SH_CREATE(uint32 nelements, void *private_data);
  174. #else
  175. /*
  176. * <prefix>_hash <prefix>_create(MemoryContext ctx, uint32 nelements,
  177. * void *private_data)
  178. */
  179. SH_SCOPE SH_TYPE *SH_CREATE(MemoryContext ctx, uint32 nelements,
  180. void *private_data);
  181. #endif
  182. /* void <prefix>_destroy(<prefix>_hash *tb) */
  183. SH_SCOPE void SH_DESTROY(SH_TYPE * tb);
  184. /* void <prefix>_reset(<prefix>_hash *tb) */
  185. SH_SCOPE void SH_RESET(SH_TYPE * tb);
  186. /* void <prefix>_grow(<prefix>_hash *tb, uint64 newsize) */
  187. SH_SCOPE void SH_GROW(SH_TYPE * tb, uint64 newsize);
  188. /* <element> *<prefix>_insert(<prefix>_hash *tb, <key> key, bool *found) */
  189. SH_SCOPE SH_ELEMENT_TYPE *SH_INSERT(SH_TYPE * tb, SH_KEY_TYPE key, bool *found);
  190. /*
  191. * <element> *<prefix>_insert_hash(<prefix>_hash *tb, <key> key, uint32 hash,
  192. * bool *found)
  193. */
  194. SH_SCOPE SH_ELEMENT_TYPE *SH_INSERT_HASH(SH_TYPE * tb, SH_KEY_TYPE key,
  195. uint32 hash, bool *found);
  196. /* <element> *<prefix>_lookup(<prefix>_hash *tb, <key> key) */
  197. SH_SCOPE SH_ELEMENT_TYPE *SH_LOOKUP(SH_TYPE * tb, SH_KEY_TYPE key);
  198. /* <element> *<prefix>_lookup_hash(<prefix>_hash *tb, <key> key, uint32 hash) */
  199. SH_SCOPE SH_ELEMENT_TYPE *SH_LOOKUP_HASH(SH_TYPE * tb, SH_KEY_TYPE key,
  200. uint32 hash);
  201. /* void <prefix>_delete_item(<prefix>_hash *tb, <element> *entry) */
  202. SH_SCOPE void SH_DELETE_ITEM(SH_TYPE * tb, SH_ELEMENT_TYPE * entry);
  203. /* bool <prefix>_delete(<prefix>_hash *tb, <key> key) */
  204. SH_SCOPE bool SH_DELETE(SH_TYPE * tb, SH_KEY_TYPE key);
  205. /* void <prefix>_start_iterate(<prefix>_hash *tb, <prefix>_iterator *iter) */
  206. SH_SCOPE void SH_START_ITERATE(SH_TYPE * tb, SH_ITERATOR * iter);
  207. /*
  208. * void <prefix>_start_iterate_at(<prefix>_hash *tb, <prefix>_iterator *iter,
  209. * uint32 at)
  210. */
  211. SH_SCOPE void SH_START_ITERATE_AT(SH_TYPE * tb, SH_ITERATOR * iter, uint32 at);
  212. /* <element> *<prefix>_iterate(<prefix>_hash *tb, <prefix>_iterator *iter) */
  213. SH_SCOPE SH_ELEMENT_TYPE *SH_ITERATE(SH_TYPE * tb, SH_ITERATOR * iter);
  214. /* void <prefix>_stat(<prefix>_hash *tb */
  215. SH_SCOPE void SH_STAT(SH_TYPE * tb);
  216. #endif /* SH_DECLARE */
  217. /* generate implementation of the hash table */
  218. #ifdef SH_DEFINE
  219. #ifndef SH_RAW_ALLOCATOR
  220. #include "utils/memutils.h"
  221. #endif
  222. /* max data array size,we allow up to PG_UINT32_MAX buckets, including 0 */
  223. #define SH_MAX_SIZE (((uint64) PG_UINT32_MAX) + 1)
  224. /* normal fillfactor, unless already close to maximum */
  225. #ifndef SH_FILLFACTOR
  226. #define SH_FILLFACTOR (0.9)
  227. #endif
  228. /* increase fillfactor if we otherwise would error out */
  229. #define SH_MAX_FILLFACTOR (0.98)
  230. /* grow if actual and optimal location bigger than */
  231. #ifndef SH_GROW_MAX_DIB
  232. #define SH_GROW_MAX_DIB 25
  233. #endif
  234. /* grow if more than elements to move when inserting */
  235. #ifndef SH_GROW_MAX_MOVE
  236. #define SH_GROW_MAX_MOVE 150
  237. #endif
  238. #ifndef SH_GROW_MIN_FILLFACTOR
  239. /* but do not grow due to SH_GROW_MAX_* if below */
  240. #define SH_GROW_MIN_FILLFACTOR 0.1
  241. #endif
  242. #ifdef SH_STORE_HASH
  243. #define SH_COMPARE_KEYS(tb, ahash, akey, b) (ahash == SH_GET_HASH(tb, b) && SH_EQUAL(tb, b->SH_KEY, akey))
  244. #else
  245. #define SH_COMPARE_KEYS(tb, ahash, akey, b) (SH_EQUAL(tb, b->SH_KEY, akey))
  246. #endif
  247. /*
  248. * Wrap the following definitions in include guards, to avoid multiple
  249. * definition errors if this header is included more than once. The rest of
  250. * the file deliberately has no include guards, because it can be included
  251. * with different parameters to define functions and types with non-colliding
  252. * names.
  253. */
  254. #ifndef SIMPLEHASH_H
  255. #define SIMPLEHASH_H
  256. #ifdef FRONTEND
  257. #define sh_error(...) pg_fatal(__VA_ARGS__)
  258. #define sh_log(...) pg_log_info(__VA_ARGS__)
  259. #else
  260. #define sh_error(...) elog(ERROR, __VA_ARGS__)
  261. #define sh_log(...) elog(LOG, __VA_ARGS__)
  262. #endif
  263. #endif
  264. /*
  265. * Compute sizing parameters for hashtable. Called when creating and growing
  266. * the hashtable.
  267. */
  268. static inline void
  269. SH_COMPUTE_PARAMETERS(SH_TYPE * tb, uint64 newsize)
  270. {
  271. uint64 size;
  272. /* supporting zero sized hashes would complicate matters */
  273. size = Max(newsize, 2);
  274. /* round up size to the next power of 2, that's how bucketing works */
  275. size = pg_nextpower2_64(size);
  276. Assert(size <= SH_MAX_SIZE);
  277. /*
  278. * Verify that allocation of ->data is possible on this platform, without
  279. * overflowing Size.
  280. */
  281. if (unlikely((((uint64) sizeof(SH_ELEMENT_TYPE)) * size) >= SIZE_MAX / 2))
  282. sh_error("hash table too large");
  283. /* now set size */
  284. tb->size = size;
  285. tb->sizemask = (uint32) (size - 1);
  286. /*
  287. * Compute the next threshold at which we need to grow the hash table
  288. * again.
  289. */
  290. if (tb->size == SH_MAX_SIZE)
  291. tb->grow_threshold = ((double) tb->size) * SH_MAX_FILLFACTOR;
  292. else
  293. tb->grow_threshold = ((double) tb->size) * SH_FILLFACTOR;
  294. }
  295. /* return the optimal bucket for the hash */
  296. static inline uint32
  297. SH_INITIAL_BUCKET(SH_TYPE * tb, uint32 hash)
  298. {
  299. return hash & tb->sizemask;
  300. }
  301. /* return next bucket after the current, handling wraparound */
  302. static inline uint32
  303. SH_NEXT(SH_TYPE * tb, uint32 curelem, uint32 startelem)
  304. {
  305. curelem = (curelem + 1) & tb->sizemask;
  306. Assert(curelem != startelem);
  307. return curelem;
  308. }
  309. /* return bucket before the current, handling wraparound */
  310. static inline uint32
  311. SH_PREV(SH_TYPE * tb, uint32 curelem, uint32 startelem)
  312. {
  313. curelem = (curelem - 1) & tb->sizemask;
  314. Assert(curelem != startelem);
  315. return curelem;
  316. }
  317. /* return distance between bucket and its optimal position */
  318. static inline uint32
  319. SH_DISTANCE_FROM_OPTIMAL(SH_TYPE * tb, uint32 optimal, uint32 bucket)
  320. {
  321. if (optimal <= bucket)
  322. return bucket - optimal;
  323. else
  324. return (tb->size + bucket) - optimal;
  325. }
  326. static inline uint32
  327. SH_ENTRY_HASH(SH_TYPE * tb, SH_ELEMENT_TYPE * entry)
  328. {
  329. #ifdef SH_STORE_HASH
  330. return SH_GET_HASH(tb, entry);
  331. #else
  332. return SH_HASH_KEY(tb, entry->SH_KEY);
  333. #endif
  334. }
  335. /* default memory allocator function */
  336. static inline void *SH_ALLOCATE(SH_TYPE * type, Size size);
  337. static inline void SH_FREE(SH_TYPE * type, void *pointer);
  338. #ifndef SH_USE_NONDEFAULT_ALLOCATOR
  339. /* default memory allocator function */
  340. static inline void *
  341. SH_ALLOCATE(SH_TYPE * type, Size size)
  342. {
  343. #ifdef SH_RAW_ALLOCATOR
  344. return SH_RAW_ALLOCATOR(size);
  345. #else
  346. return MemoryContextAllocExtended(type->ctx, size,
  347. MCXT_ALLOC_HUGE | MCXT_ALLOC_ZERO);
  348. #endif
  349. }
  350. /* default memory free function */
  351. static inline void
  352. SH_FREE(SH_TYPE * type, void *pointer)
  353. {
  354. pfree(pointer);
  355. }
  356. #endif
  357. /*
  358. * Create a hash table with enough space for `nelements` distinct members.
  359. * Memory for the hash table is allocated from the passed-in context. If
  360. * desired, the array of elements can be allocated using a passed-in allocator;
  361. * this could be useful in order to place the array of elements in a shared
  362. * memory, or in a context that will outlive the rest of the hash table.
  363. * Memory other than for the array of elements will still be allocated from
  364. * the passed-in context.
  365. */
  366. #ifdef SH_RAW_ALLOCATOR
  367. SH_SCOPE SH_TYPE *
  368. SH_CREATE(uint32 nelements, void *private_data)
  369. #else
  370. SH_SCOPE SH_TYPE *
  371. SH_CREATE(MemoryContext ctx, uint32 nelements, void *private_data)
  372. #endif
  373. {
  374. SH_TYPE *tb;
  375. uint64 size;
  376. #ifdef SH_RAW_ALLOCATOR
  377. tb = (SH_TYPE *) SH_RAW_ALLOCATOR(sizeof(SH_TYPE));
  378. #else
  379. tb = (SH_TYPE *) MemoryContextAllocZero(ctx, sizeof(SH_TYPE));
  380. tb->ctx = ctx;
  381. #endif
  382. tb->private_data = private_data;
  383. /* increase nelements by fillfactor, want to store nelements elements */
  384. size = Min((double) SH_MAX_SIZE, ((double) nelements) / SH_FILLFACTOR);
  385. SH_COMPUTE_PARAMETERS(tb, size);
  386. tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size);
  387. return tb;
  388. }
  389. /* destroy a previously created hash table */
  390. SH_SCOPE void
  391. SH_DESTROY(SH_TYPE * tb)
  392. {
  393. SH_FREE(tb, tb->data);
  394. pfree(tb);
  395. }
  396. /* reset the contents of a previously created hash table */
  397. SH_SCOPE void
  398. SH_RESET(SH_TYPE * tb)
  399. {
  400. memset(tb->data, 0, sizeof(SH_ELEMENT_TYPE) * tb->size);
  401. tb->members = 0;
  402. }
  403. /*
  404. * Grow a hash table to at least `newsize` buckets.
  405. *
  406. * Usually this will automatically be called by insertions/deletions, when
  407. * necessary. But resizing to the exact input size can be advantageous
  408. * performance-wise, when known at some point.
  409. */
  410. SH_SCOPE void
  411. SH_GROW(SH_TYPE * tb, uint64 newsize)
  412. {
  413. uint64 oldsize = tb->size;
  414. SH_ELEMENT_TYPE *olddata = tb->data;
  415. SH_ELEMENT_TYPE *newdata;
  416. uint32 i;
  417. uint32 startelem = 0;
  418. uint32 copyelem;
  419. Assert(oldsize == pg_nextpower2_64(oldsize));
  420. Assert(oldsize != SH_MAX_SIZE);
  421. Assert(oldsize < newsize);
  422. /* compute parameters for new table */
  423. SH_COMPUTE_PARAMETERS(tb, newsize);
  424. tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size);
  425. newdata = tb->data;
  426. /*
  427. * Copy entries from the old data to newdata. We theoretically could use
  428. * SH_INSERT here, to avoid code duplication, but that's more general than
  429. * we need. We neither want tb->members increased, nor do we need to do
  430. * deal with deleted elements, nor do we need to compare keys. So a
  431. * special-cased implementation is lot faster. As resizing can be time
  432. * consuming and frequent, that's worthwhile to optimize.
  433. *
  434. * To be able to simply move entries over, we have to start not at the
  435. * first bucket (i.e olddata[0]), but find the first bucket that's either
  436. * empty, or is occupied by an entry at its optimal position. Such a
  437. * bucket has to exist in any table with a load factor under 1, as not all
  438. * buckets are occupied, i.e. there always has to be an empty bucket. By
  439. * starting at such a bucket we can move the entries to the larger table,
  440. * without having to deal with conflicts.
  441. */
  442. /* search for the first element in the hash that's not wrapped around */
  443. for (i = 0; i < oldsize; i++)
  444. {
  445. SH_ELEMENT_TYPE *oldentry = &olddata[i];
  446. uint32 hash;
  447. uint32 optimal;
  448. if (oldentry->status != SH_STATUS_IN_USE)
  449. {
  450. startelem = i;
  451. break;
  452. }
  453. hash = SH_ENTRY_HASH(tb, oldentry);
  454. optimal = SH_INITIAL_BUCKET(tb, hash);
  455. if (optimal == i)
  456. {
  457. startelem = i;
  458. break;
  459. }
  460. }
  461. /* and copy all elements in the old table */
  462. copyelem = startelem;
  463. for (i = 0; i < oldsize; i++)
  464. {
  465. SH_ELEMENT_TYPE *oldentry = &olddata[copyelem];
  466. if (oldentry->status == SH_STATUS_IN_USE)
  467. {
  468. uint32 hash;
  469. uint32 startelem;
  470. uint32 curelem;
  471. SH_ELEMENT_TYPE *newentry;
  472. hash = SH_ENTRY_HASH(tb, oldentry);
  473. startelem = SH_INITIAL_BUCKET(tb, hash);
  474. curelem = startelem;
  475. /* find empty element to put data into */
  476. while (true)
  477. {
  478. newentry = &newdata[curelem];
  479. if (newentry->status == SH_STATUS_EMPTY)
  480. {
  481. break;
  482. }
  483. curelem = SH_NEXT(tb, curelem, startelem);
  484. }
  485. /* copy entry to new slot */
  486. memcpy(newentry, oldentry, sizeof(SH_ELEMENT_TYPE));
  487. }
  488. /* can't use SH_NEXT here, would use new size */
  489. copyelem++;
  490. if (copyelem >= oldsize)
  491. {
  492. copyelem = 0;
  493. }
  494. }
  495. SH_FREE(tb, olddata);
  496. }
  497. /*
  498. * This is a separate static inline function, so it can be reliably be inlined
  499. * into its wrapper functions even if SH_SCOPE is extern.
  500. */
  501. static inline SH_ELEMENT_TYPE *
  502. SH_INSERT_HASH_INTERNAL(SH_TYPE * tb, SH_KEY_TYPE key, uint32 hash, bool *found)
  503. {
  504. uint32 startelem;
  505. uint32 curelem;
  506. SH_ELEMENT_TYPE *data;
  507. uint32 insertdist;
  508. restart:
  509. insertdist = 0;
  510. /*
  511. * We do the grow check even if the key is actually present, to avoid
  512. * doing the check inside the loop. This also lets us avoid having to
  513. * re-find our position in the hashtable after resizing.
  514. *
  515. * Note that this also reached when resizing the table due to
  516. * SH_GROW_MAX_DIB / SH_GROW_MAX_MOVE.
  517. */
  518. if (unlikely(tb->members >= tb->grow_threshold))
  519. {
  520. if (unlikely(tb->size == SH_MAX_SIZE))
  521. sh_error("hash table size exceeded");
  522. /*
  523. * When optimizing, it can be very useful to print these out.
  524. */
  525. /* SH_STAT(tb); */
  526. SH_GROW(tb, tb->size * 2);
  527. /* SH_STAT(tb); */
  528. }
  529. /* perform insert, start bucket search at optimal location */
  530. data = tb->data;
  531. startelem = SH_INITIAL_BUCKET(tb, hash);
  532. curelem = startelem;
  533. while (true)
  534. {
  535. uint32 curdist;
  536. uint32 curhash;
  537. uint32 curoptimal;
  538. SH_ELEMENT_TYPE *entry = &data[curelem];
  539. /* any empty bucket can directly be used */
  540. if (entry->status == SH_STATUS_EMPTY)
  541. {
  542. tb->members++;
  543. entry->SH_KEY = key;
  544. #ifdef SH_STORE_HASH
  545. SH_GET_HASH(tb, entry) = hash;
  546. #endif
  547. entry->status = SH_STATUS_IN_USE;
  548. *found = false;
  549. return entry;
  550. }
  551. /*
  552. * If the bucket is not empty, we either found a match (in which case
  553. * we're done), or we have to decide whether to skip over or move the
  554. * colliding entry. When the colliding element's distance to its
  555. * optimal position is smaller than the to-be-inserted entry's, we
  556. * shift the colliding entry (and its followers) forward by one.
  557. */
  558. if (SH_COMPARE_KEYS(tb, hash, key, entry))
  559. {
  560. Assert(entry->status == SH_STATUS_IN_USE);
  561. *found = true;
  562. return entry;
  563. }
  564. curhash = SH_ENTRY_HASH(tb, entry);
  565. curoptimal = SH_INITIAL_BUCKET(tb, curhash);
  566. curdist = SH_DISTANCE_FROM_OPTIMAL(tb, curoptimal, curelem);
  567. if (insertdist > curdist)
  568. {
  569. SH_ELEMENT_TYPE *lastentry = entry;
  570. uint32 emptyelem = curelem;
  571. uint32 moveelem;
  572. int32 emptydist = 0;
  573. /* find next empty bucket */
  574. while (true)
  575. {
  576. SH_ELEMENT_TYPE *emptyentry;
  577. emptyelem = SH_NEXT(tb, emptyelem, startelem);
  578. emptyentry = &data[emptyelem];
  579. if (emptyentry->status == SH_STATUS_EMPTY)
  580. {
  581. lastentry = emptyentry;
  582. break;
  583. }
  584. /*
  585. * To avoid negative consequences from overly imbalanced
  586. * hashtables, grow the hashtable if collisions would require
  587. * us to move a lot of entries. The most likely cause of such
  588. * imbalance is filling a (currently) small table, from a
  589. * currently big one, in hash-table order. Don't grow if the
  590. * hashtable would be too empty, to prevent quick space
  591. * explosion for some weird edge cases.
  592. */
  593. if (unlikely(++emptydist > SH_GROW_MAX_MOVE) &&
  594. ((double) tb->members / tb->size) >= SH_GROW_MIN_FILLFACTOR)
  595. {
  596. tb->grow_threshold = 0;
  597. goto restart;
  598. }
  599. }
  600. /* shift forward, starting at last occupied element */
  601. /*
  602. * TODO: This could be optimized to be one memcpy in many cases,
  603. * excepting wrapping around at the end of ->data. Hasn't shown up
  604. * in profiles so far though.
  605. */
  606. moveelem = emptyelem;
  607. while (moveelem != curelem)
  608. {
  609. SH_ELEMENT_TYPE *moveentry;
  610. moveelem = SH_PREV(tb, moveelem, startelem);
  611. moveentry = &data[moveelem];
  612. memcpy(lastentry, moveentry, sizeof(SH_ELEMENT_TYPE));
  613. lastentry = moveentry;
  614. }
  615. /* and fill the now empty spot */
  616. tb->members++;
  617. entry->SH_KEY = key;
  618. #ifdef SH_STORE_HASH
  619. SH_GET_HASH(tb, entry) = hash;
  620. #endif
  621. entry->status = SH_STATUS_IN_USE;
  622. *found = false;
  623. return entry;
  624. }
  625. curelem = SH_NEXT(tb, curelem, startelem);
  626. insertdist++;
  627. /*
  628. * To avoid negative consequences from overly imbalanced hashtables,
  629. * grow the hashtable if collisions lead to large runs. The most
  630. * likely cause of such imbalance is filling a (currently) small
  631. * table, from a currently big one, in hash-table order. Don't grow
  632. * if the hashtable would be too empty, to prevent quick space
  633. * explosion for some weird edge cases.
  634. */
  635. if (unlikely(insertdist > SH_GROW_MAX_DIB) &&
  636. ((double) tb->members / tb->size) >= SH_GROW_MIN_FILLFACTOR)
  637. {
  638. tb->grow_threshold = 0;
  639. goto restart;
  640. }
  641. }
  642. }
  643. /*
  644. * Insert the key key into the hash-table, set *found to true if the key
  645. * already exists, false otherwise. Returns the hash-table entry in either
  646. * case.
  647. */
  648. SH_SCOPE SH_ELEMENT_TYPE *
  649. SH_INSERT(SH_TYPE * tb, SH_KEY_TYPE key, bool *found)
  650. {
  651. uint32 hash = SH_HASH_KEY(tb, key);
  652. return SH_INSERT_HASH_INTERNAL(tb, key, hash, found);
  653. }
  654. /*
  655. * Insert the key key into the hash-table using an already-calculated
  656. * hash. Set *found to true if the key already exists, false
  657. * otherwise. Returns the hash-table entry in either case.
  658. */
  659. SH_SCOPE SH_ELEMENT_TYPE *
  660. SH_INSERT_HASH(SH_TYPE * tb, SH_KEY_TYPE key, uint32 hash, bool *found)
  661. {
  662. return SH_INSERT_HASH_INTERNAL(tb, key, hash, found);
  663. }
  664. /*
  665. * This is a separate static inline function, so it can be reliably be inlined
  666. * into its wrapper functions even if SH_SCOPE is extern.
  667. */
  668. static inline SH_ELEMENT_TYPE *
  669. SH_LOOKUP_HASH_INTERNAL(SH_TYPE * tb, SH_KEY_TYPE key, uint32 hash)
  670. {
  671. const uint32 startelem = SH_INITIAL_BUCKET(tb, hash);
  672. uint32 curelem = startelem;
  673. while (true)
  674. {
  675. SH_ELEMENT_TYPE *entry = &tb->data[curelem];
  676. if (entry->status == SH_STATUS_EMPTY)
  677. {
  678. return NULL;
  679. }
  680. Assert(entry->status == SH_STATUS_IN_USE);
  681. if (SH_COMPARE_KEYS(tb, hash, key, entry))
  682. return entry;
  683. /*
  684. * TODO: we could stop search based on distance. If the current
  685. * buckets's distance-from-optimal is smaller than what we've skipped
  686. * already, the entry doesn't exist. Probably only do so if
  687. * SH_STORE_HASH is defined, to avoid re-computing hashes?
  688. */
  689. curelem = SH_NEXT(tb, curelem, startelem);
  690. }
  691. }
  692. /*
  693. * Lookup up entry in hash table. Returns NULL if key not present.
  694. */
  695. SH_SCOPE SH_ELEMENT_TYPE *
  696. SH_LOOKUP(SH_TYPE * tb, SH_KEY_TYPE key)
  697. {
  698. uint32 hash = SH_HASH_KEY(tb, key);
  699. return SH_LOOKUP_HASH_INTERNAL(tb, key, hash);
  700. }
  701. /*
  702. * Lookup up entry in hash table using an already-calculated hash.
  703. *
  704. * Returns NULL if key not present.
  705. */
  706. SH_SCOPE SH_ELEMENT_TYPE *
  707. SH_LOOKUP_HASH(SH_TYPE * tb, SH_KEY_TYPE key, uint32 hash)
  708. {
  709. return SH_LOOKUP_HASH_INTERNAL(tb, key, hash);
  710. }
  711. /*
  712. * Delete entry from hash table by key. Returns whether to-be-deleted key was
  713. * present.
  714. */
  715. SH_SCOPE bool
  716. SH_DELETE(SH_TYPE * tb, SH_KEY_TYPE key)
  717. {
  718. uint32 hash = SH_HASH_KEY(tb, key);
  719. uint32 startelem = SH_INITIAL_BUCKET(tb, hash);
  720. uint32 curelem = startelem;
  721. while (true)
  722. {
  723. SH_ELEMENT_TYPE *entry = &tb->data[curelem];
  724. if (entry->status == SH_STATUS_EMPTY)
  725. return false;
  726. if (entry->status == SH_STATUS_IN_USE &&
  727. SH_COMPARE_KEYS(tb, hash, key, entry))
  728. {
  729. SH_ELEMENT_TYPE *lastentry = entry;
  730. tb->members--;
  731. /*
  732. * Backward shift following elements till either an empty element
  733. * or an element at its optimal position is encountered.
  734. *
  735. * While that sounds expensive, the average chain length is short,
  736. * and deletions would otherwise require tombstones.
  737. */
  738. while (true)
  739. {
  740. SH_ELEMENT_TYPE *curentry;
  741. uint32 curhash;
  742. uint32 curoptimal;
  743. curelem = SH_NEXT(tb, curelem, startelem);
  744. curentry = &tb->data[curelem];
  745. if (curentry->status != SH_STATUS_IN_USE)
  746. {
  747. lastentry->status = SH_STATUS_EMPTY;
  748. break;
  749. }
  750. curhash = SH_ENTRY_HASH(tb, curentry);
  751. curoptimal = SH_INITIAL_BUCKET(tb, curhash);
  752. /* current is at optimal position, done */
  753. if (curoptimal == curelem)
  754. {
  755. lastentry->status = SH_STATUS_EMPTY;
  756. break;
  757. }
  758. /* shift */
  759. memcpy(lastentry, curentry, sizeof(SH_ELEMENT_TYPE));
  760. lastentry = curentry;
  761. }
  762. return true;
  763. }
  764. /* TODO: return false; if distance too big */
  765. curelem = SH_NEXT(tb, curelem, startelem);
  766. }
  767. }
  768. /*
  769. * Delete entry from hash table by entry pointer
  770. */
  771. SH_SCOPE void
  772. SH_DELETE_ITEM(SH_TYPE * tb, SH_ELEMENT_TYPE * entry)
  773. {
  774. SH_ELEMENT_TYPE *lastentry = entry;
  775. uint32 hash = SH_ENTRY_HASH(tb, entry);
  776. uint32 startelem = SH_INITIAL_BUCKET(tb, hash);
  777. uint32 curelem;
  778. /* Calculate the index of 'entry' */
  779. curelem = entry - &tb->data[0];
  780. tb->members--;
  781. /*
  782. * Backward shift following elements till either an empty element or an
  783. * element at its optimal position is encountered.
  784. *
  785. * While that sounds expensive, the average chain length is short, and
  786. * deletions would otherwise require tombstones.
  787. */
  788. while (true)
  789. {
  790. SH_ELEMENT_TYPE *curentry;
  791. uint32 curhash;
  792. uint32 curoptimal;
  793. curelem = SH_NEXT(tb, curelem, startelem);
  794. curentry = &tb->data[curelem];
  795. if (curentry->status != SH_STATUS_IN_USE)
  796. {
  797. lastentry->status = SH_STATUS_EMPTY;
  798. break;
  799. }
  800. curhash = SH_ENTRY_HASH(tb, curentry);
  801. curoptimal = SH_INITIAL_BUCKET(tb, curhash);
  802. /* current is at optimal position, done */
  803. if (curoptimal == curelem)
  804. {
  805. lastentry->status = SH_STATUS_EMPTY;
  806. break;
  807. }
  808. /* shift */
  809. memcpy(lastentry, curentry, sizeof(SH_ELEMENT_TYPE));
  810. lastentry = curentry;
  811. }
  812. }
  813. /*
  814. * Initialize iterator.
  815. */
  816. SH_SCOPE void
  817. SH_START_ITERATE(SH_TYPE * tb, SH_ITERATOR * iter)
  818. {
  819. uint64 startelem = PG_UINT64_MAX;
  820. /*
  821. * Search for the first empty element. As deletions during iterations are
  822. * supported, we want to start/end at an element that cannot be affected
  823. * by elements being shifted.
  824. */
  825. for (uint32 i = 0; i < tb->size; i++)
  826. {
  827. SH_ELEMENT_TYPE *entry = &tb->data[i];
  828. if (entry->status != SH_STATUS_IN_USE)
  829. {
  830. startelem = i;
  831. break;
  832. }
  833. }
  834. /* we should have found an empty element */
  835. Assert(startelem < SH_MAX_SIZE);
  836. /*
  837. * Iterate backwards, that allows the current element to be deleted, even
  838. * if there are backward shifts
  839. */
  840. iter->cur = startelem;
  841. iter->end = iter->cur;
  842. iter->done = false;
  843. }
  844. /*
  845. * Initialize iterator to a specific bucket. That's really only useful for
  846. * cases where callers are partially iterating over the hashspace, and that
  847. * iteration deletes and inserts elements based on visited entries. Doing that
  848. * repeatedly could lead to an unbalanced keyspace when always starting at the
  849. * same position.
  850. */
  851. SH_SCOPE void
  852. SH_START_ITERATE_AT(SH_TYPE * tb, SH_ITERATOR * iter, uint32 at)
  853. {
  854. /*
  855. * Iterate backwards, that allows the current element to be deleted, even
  856. * if there are backward shifts.
  857. */
  858. iter->cur = at & tb->sizemask; /* ensure at is within a valid range */
  859. iter->end = iter->cur;
  860. iter->done = false;
  861. }
  862. /*
  863. * Iterate over all entries in the hash-table. Return the next occupied entry,
  864. * or NULL if done.
  865. *
  866. * During iteration the current entry in the hash table may be deleted,
  867. * without leading to elements being skipped or returned twice. Additionally
  868. * the rest of the table may be modified (i.e. there can be insertions or
  869. * deletions), but if so, there's neither a guarantee that all nodes are
  870. * visited at least once, nor a guarantee that a node is visited at most once.
  871. */
  872. SH_SCOPE SH_ELEMENT_TYPE *
  873. SH_ITERATE(SH_TYPE * tb, SH_ITERATOR * iter)
  874. {
  875. while (!iter->done)
  876. {
  877. SH_ELEMENT_TYPE *elem;
  878. elem = &tb->data[iter->cur];
  879. /* next element in backward direction */
  880. iter->cur = (iter->cur - 1) & tb->sizemask;
  881. if ((iter->cur & tb->sizemask) == (iter->end & tb->sizemask))
  882. iter->done = true;
  883. if (elem->status == SH_STATUS_IN_USE)
  884. {
  885. return elem;
  886. }
  887. }
  888. return NULL;
  889. }
  890. /*
  891. * Report some statistics about the state of the hashtable. For
  892. * debugging/profiling purposes only.
  893. */
  894. SH_SCOPE void
  895. SH_STAT(SH_TYPE * tb)
  896. {
  897. uint32 max_chain_length = 0;
  898. uint32 total_chain_length = 0;
  899. double avg_chain_length;
  900. double fillfactor;
  901. uint32 i;
  902. uint32 *collisions = (uint32 *) palloc0(tb->size * sizeof(uint32));
  903. uint32 total_collisions = 0;
  904. uint32 max_collisions = 0;
  905. double avg_collisions;
  906. for (i = 0; i < tb->size; i++)
  907. {
  908. uint32 hash;
  909. uint32 optimal;
  910. uint32 dist;
  911. SH_ELEMENT_TYPE *elem;
  912. elem = &tb->data[i];
  913. if (elem->status != SH_STATUS_IN_USE)
  914. continue;
  915. hash = SH_ENTRY_HASH(tb, elem);
  916. optimal = SH_INITIAL_BUCKET(tb, hash);
  917. dist = SH_DISTANCE_FROM_OPTIMAL(tb, optimal, i);
  918. if (dist > max_chain_length)
  919. max_chain_length = dist;
  920. total_chain_length += dist;
  921. collisions[optimal]++;
  922. }
  923. for (i = 0; i < tb->size; i++)
  924. {
  925. uint32 curcoll = collisions[i];
  926. if (curcoll == 0)
  927. continue;
  928. /* single contained element is not a collision */
  929. curcoll--;
  930. total_collisions += curcoll;
  931. if (curcoll > max_collisions)
  932. max_collisions = curcoll;
  933. }
  934. if (tb->members > 0)
  935. {
  936. fillfactor = tb->members / ((double) tb->size);
  937. avg_chain_length = ((double) total_chain_length) / tb->members;
  938. avg_collisions = ((double) total_collisions) / tb->members;
  939. }
  940. else
  941. {
  942. fillfactor = 0;
  943. avg_chain_length = 0;
  944. avg_collisions = 0;
  945. }
  946. sh_log("size: " UINT64_FORMAT ", members: %u, filled: %f, total chain: %u, max chain: %u, avg chain: %f, total_collisions: %u, max_collisions: %u, avg_collisions: %f",
  947. tb->size, tb->members, fillfactor, total_chain_length, max_chain_length, avg_chain_length,
  948. total_collisions, max_collisions, avg_collisions);
  949. }
  950. #endif /* SH_DEFINE */
  951. /* undefine external parameters, so next hash table can be defined */
  952. #undef SH_PREFIX
  953. #undef SH_KEY_TYPE
  954. #undef SH_KEY
  955. #undef SH_ELEMENT_TYPE
  956. #undef SH_HASH_KEY
  957. #undef SH_SCOPE
  958. #undef SH_DECLARE
  959. #undef SH_DEFINE
  960. #undef SH_GET_HASH
  961. #undef SH_STORE_HASH
  962. #undef SH_USE_NONDEFAULT_ALLOCATOR
  963. #undef SH_EQUAL
  964. /* undefine locally declared macros */
  965. #undef SH_MAKE_PREFIX
  966. #undef SH_MAKE_NAME
  967. #undef SH_MAKE_NAME_
  968. #undef SH_FILLFACTOR
  969. #undef SH_MAX_FILLFACTOR
  970. #undef SH_GROW_MAX_DIB
  971. #undef SH_GROW_MAX_MOVE
  972. #undef SH_GROW_MIN_FILLFACTOR
  973. #undef SH_MAX_SIZE
  974. /* types */
  975. #undef SH_TYPE
  976. #undef SH_STATUS
  977. #undef SH_STATUS_EMPTY
  978. #undef SH_STATUS_IN_USE
  979. #undef SH_ITERATOR
  980. /* external function names */
  981. #undef SH_CREATE
  982. #undef SH_DESTROY
  983. #undef SH_RESET
  984. #undef SH_INSERT
  985. #undef SH_INSERT_HASH
  986. #undef SH_DELETE_ITEM
  987. #undef SH_DELETE
  988. #undef SH_LOOKUP
  989. #undef SH_LOOKUP_HASH
  990. #undef SH_GROW
  991. #undef SH_START_ITERATE
  992. #undef SH_START_ITERATE_AT
  993. #undef SH_ITERATE
  994. #undef SH_ALLOCATE
  995. #undef SH_FREE
  996. #undef SH_STAT
  997. /* internal function names */
  998. #undef SH_COMPUTE_PARAMETERS
  999. #undef SH_COMPARE_KEYS
  1000. #undef SH_INITIAL_BUCKET
  1001. #undef SH_NEXT
  1002. #undef SH_PREV
  1003. #undef SH_DISTANCE_FROM_OPTIMAL
  1004. #undef SH_ENTRY_HASH
  1005. #undef SH_INSERT_HASH_INTERNAL
  1006. #undef SH_LOOKUP_HASH_INTERNAL