Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3c4f6e

Browse files
committed
Revise the way the element allocator for a simplehash is specified.
This method is more elegant and more efficient.Per a suggestion from Andres Freund, who also briefly reviewedthe patch.
1 parent242066c commitc3c4f6e

File tree

3 files changed

+24
-46
lines changed

3 files changed

+24
-46
lines changed

‎src/backend/executor/execGrouping.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
330330
else
331331
hashtable->hash_iv=0;
332332

333-
hashtable->hashtab=
334-
tuplehash_create(tablecxt,nbuckets,NULL,NULL,NULL);
333+
hashtable->hashtab=tuplehash_create(tablecxt,nbuckets);
335334
hashtable->hashtab->private_data=hashtable;
336335

337336
returnhashtable;

‎src/backend/nodes/tidbitmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ tbm_create_pagetable(TIDBitmap *tbm)
244244
Assert(tbm->status!=TBM_HASH);
245245
Assert(tbm->pagetable==NULL);
246246

247-
tbm->pagetable=pagetable_create(tbm->mcxt,128,NULL,NULL,NULL);
247+
tbm->pagetable=pagetable_create(tbm->mcxt,128);
248248

249249
/* If entry1 is valid, push it into the hashtable */
250250
if (tbm->status==TBM_ONE_PAGE)

‎src/include/lib/simplehash.h

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* - SH_DEFINE - if defined function definitions are generated
2424
* - SH_SCOPE - in which scope (e.g. extern, static inline) do function
2525
*declarations reside
26+
* - SH_USE_NONDEFAULT_ALLOCATOR - if defined no element allocator functions
27+
* are defined, so you can supply your own
2628
* The following parameters are only relevant when SH_DEFINE is defined:
2729
* - SH_KEY - name of the element in SH_ELEMENT_TYPE containing the hash key
2830
* - SH_EQUAL(table, a, b) - compare two table keys
@@ -77,6 +79,8 @@
7779
#defineSH_START_ITERATE SH_MAKE_NAME(start_iterate)
7880
#defineSH_START_ITERATE_AT SH_MAKE_NAME(start_iterate_at)
7981
#defineSH_ITERATE SH_MAKE_NAME(iterate)
82+
#defineSH_ALLOCATE SH_MAKE_NAME(allocate)
83+
#defineSH_FREE SH_MAKE_NAME(free)
8084
#defineSH_STAT SH_MAKE_NAME(stat)
8185

8286
/* internal helper functions (no externally visible prototypes) */
@@ -87,13 +91,6 @@
8791
#defineSH_INITIAL_BUCKET SH_MAKE_NAME(initial_bucket)
8892
#defineSH_ENTRY_HASH SH_MAKE_NAME(entry_hash)
8993

90-
/* Allocation function for hash table elements */
91-
#ifndefSIMPLEHASH_TYPEDEFS
92-
#defineSIMPLEHASH_TYPEDEFS
93-
typedefvoid*(*simplehash_allocate) (Sizesize,void*args);
94-
typedefvoid (*simplehash_free) (void*pointer,void*args);
95-
#endif
96-
9794
/* generate forward declarations necessary to use the hash table */
9895
#ifdefSH_DECLARE
9996

@@ -119,11 +116,6 @@ typedef struct SH_TYPE
119116
/* hash buckets */
120117
SH_ELEMENT_TYPE*data;
121118

122-
/* Allocation and free functions, and the associated context. */
123-
simplehash_allocateelement_alloc;
124-
simplehash_freeelement_free;
125-
void*element_args;
126-
127119
/* memory context to use for allocations */
128120
MemoryContextctx;
129121

@@ -145,8 +137,7 @@ typedef struct SH_ITERATOR
145137
}SH_ITERATOR;
146138

147139
/* externally visible function prototypes */
148-
SH_SCOPESH_TYPE*SH_CREATE(MemoryContextctx,uint32nelements,
149-
simplehash_allocateallocfunc,simplehash_freefreefunc,void*args);
140+
SH_SCOPESH_TYPE*SH_CREATE(MemoryContextctx,uint32nelements);
150141
SH_SCOPEvoidSH_DESTROY(SH_TYPE*tb);
151142
SH_SCOPEvoidSH_GROW(SH_TYPE*tb,uint32newsize);
152143
SH_SCOPESH_ELEMENT_TYPE*SH_INSERT(SH_TYPE*tb,SH_KEY_TYPEkey,bool*found);
@@ -289,23 +280,25 @@ SH_ENTRY_HASH(SH_TYPE *tb, SH_ELEMENT_TYPE * entry)
289280
#endif
290281
}
291282

283+
#ifndefSH_USE_NONDEFAULT_ALLOCATOR
284+
292285
/* default memory allocator function */
293-
staticvoid*
294-
SH_DEFAULT_ALLOC(Sizesize,void*args)
286+
staticinlinevoid*
287+
SH_ALLOCATE(SH_TYPE*type,Sizesize)
295288
{
296-
MemoryContextcontext= (MemoryContext)args;
297-
298-
returnMemoryContextAllocExtended(context,size,
289+
returnMemoryContextAllocExtended(type->ctx,size,
299290
MCXT_ALLOC_HUGE |MCXT_ALLOC_ZERO);
300291
}
301292

302293
/* default memory free function */
303-
staticvoid
304-
SH_DEFAULT_FREE(void*pointer,void*args)
294+
staticinlinevoid
295+
SH_FREE(SH_TYPE*type,void*pointer)
305296
{
306297
pfree(pointer);
307298
}
308299

300+
#endif
301+
309302
/*
310303
* Create a hash table with enough space for `nelements` distinct members.
311304
* Memory for the hash table is allocated from the passed-in context. If
@@ -316,8 +309,7 @@ SH_DEFAULT_FREE(void *pointer, void *args)
316309
* the passed-in context.
317310
*/
318311
SH_SCOPESH_TYPE*
319-
SH_CREATE(MemoryContextctx,uint32nelements,simplehash_allocateallocfunc,
320-
simplehash_freefreefunc,void*args)
312+
SH_CREATE(MemoryContextctx,uint32nelements)
321313
{
322314
SH_TYPE*tb;
323315
uint64size;
@@ -330,22 +322,7 @@ SH_CREATE(MemoryContext ctx, uint32 nelements, simplehash_allocate allocfunc,
330322

331323
SH_COMPUTE_PARAMETERS(tb,size);
332324

333-
if (allocfunc==NULL)
334-
{
335-
tb->element_alloc=SH_DEFAULT_ALLOC;
336-
tb->element_free=SH_DEFAULT_FREE;
337-
tb->element_args=ctx;
338-
}
339-
else
340-
{
341-
tb->element_alloc=allocfunc;
342-
tb->element_free=freefunc;
343-
344-
tb->element_args=args;
345-
}
346-
347-
tb->data=tb->element_alloc(sizeof(SH_ELEMENT_TYPE)*tb->size,
348-
tb->element_args);
325+
tb->data=SH_ALLOCATE(tb,sizeof(SH_ELEMENT_TYPE)*tb->size);
349326

350327
returntb;
351328
}
@@ -354,7 +331,7 @@ SH_CREATE(MemoryContext ctx, uint32 nelements, simplehash_allocate allocfunc,
354331
SH_SCOPEvoid
355332
SH_DESTROY(SH_TYPE*tb)
356333
{
357-
tb->element_free(tb->data,tb->element_args);
334+
SH_FREE(tb,tb->data);
358335
pfree(tb);
359336
}
360337

@@ -382,8 +359,7 @@ SH_GROW(SH_TYPE *tb, uint32 newsize)
382359
/* compute parameters for new table */
383360
SH_COMPUTE_PARAMETERS(tb,newsize);
384361

385-
tb->data=tb->element_alloc(sizeof(SH_ELEMENT_TYPE)*tb->size,
386-
tb->element_args);
362+
tb->data=SH_ALLOCATE(tb,sizeof(SH_ELEMENT_TYPE)*tb->size);
387363

388364
newdata=tb->data;
389365

@@ -469,7 +445,7 @@ SH_GROW(SH_TYPE *tb, uint32 newsize)
469445
}
470446
}
471447

472-
tb->element_free(olddata,tb->element_args);
448+
SH_FREE(tb,olddata);
473449
}
474450

475451
/*
@@ -888,6 +864,7 @@ SH_STAT(SH_TYPE *tb)
888864
#undef SH_DEFINE
889865
#undef SH_GET_HASH
890866
#undef SH_STORE_HASH
867+
#undef SH_USE_NONDEFAULT_ALLOCATOR
891868

892869
/* undefine locally declared macros */
893870
#undef SH_MAKE_PREFIX
@@ -914,6 +891,8 @@ SH_STAT(SH_TYPE *tb)
914891
#undef SH_START_ITERATE
915892
#undef SH_START_ITERATE_AT
916893
#undef SH_ITERATE
894+
#undef SH_ALLOCATE
895+
#undef SH_FREE
917896
#undef SH_STAT
918897

919898
/* internal function names */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp