Skip to content

Commit ee65565

Browse files
committed
Inline ginCompareItemPointers function for speed.
ginCompareItemPointers function is called heavily in gin index scans - inlining it speeds up some kind of queries a lot.
1 parent d51b271 commit ee65565

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/backend/access/gin/gindatapage.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,6 @@
1717
#include "access/gin_private.h"
1818
#include "utils/rel.h"
1919

20-
int
21-
ginCompareItemPointers(ItemPointer a, ItemPointer b)
22-
{
23-
BlockNumber ba = GinItemPointerGetBlockNumber(a);
24-
BlockNumber bb = GinItemPointerGetBlockNumber(b);
25-
26-
if (ba == bb)
27-
{
28-
OffsetNumber oa = GinItemPointerGetOffsetNumber(a);
29-
OffsetNumber ob = GinItemPointerGetOffsetNumber(b);
30-
31-
if (oa == ob)
32-
return 0;
33-
return (oa > ob) ? 1 : -1;
34-
}
35-
36-
return (ba > bb) ? 1 : -1;
37-
}
38-
3920
/*
4021
* Merge two ordered arrays of itempointers, eliminating any duplicates.
4122
* Returns the number of items in the result.

src/include/access/gin_private.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ extern void ginEntryFillRoot(GinBtree btree, Buffer root, Buffer lbuf, Buffer rb
530530
extern IndexTuple ginPageGetLinkItup(Buffer buf);
531531

532532
/* gindatapage.c */
533-
extern int ginCompareItemPointers(ItemPointer a, ItemPointer b);
534533
extern uint32 ginMergeItemPointers(ItemPointerData *dst,
535534
ItemPointerData *a, uint32 na,
536535
ItemPointerData *b, uint32 nb);
@@ -724,4 +723,28 @@ extern void ginHeapTupleFastCollect(GinState *ginstate,
724723
extern void ginInsertCleanup(GinState *ginstate,
725724
bool vac_delay, IndexBulkDeleteResult *stats);
726725

726+
/*
727+
* Merging the results of several gin scans compares item pointers a lot,
728+
* so we want this to be inlined. But if the compiler doesn't support that,
729+
* fall back on the non-inline version from itemptr.c. See STATIC_IF_INLINE in
730+
* c.h.
731+
*/
732+
#ifdef PG_USE_INLINE
733+
static inline int
734+
ginCompareItemPointers(ItemPointer a, ItemPointer b)
735+
{
736+
uint64 ia = (uint64) a->ip_blkid.bi_hi << 32 | (uint64) a->ip_blkid.bi_lo << 16 | a->ip_posid;
737+
uint64 ib = (uint64) b->ip_blkid.bi_hi << 32 | (uint64) b->ip_blkid.bi_lo << 16 | b->ip_posid;
738+
739+
if (ia == ib)
740+
return 0;
741+
else if (ia > ib)
742+
return 1;
743+
else
744+
return -1;
745+
}
746+
#else
747+
#define ginCompareItemPointers(a, b) ItemPointerCompare(a, b)
748+
#endif /* PG_USE_INLINE */
749+
727750
#endif /* GIN_PRIVATE_H */

0 commit comments

Comments
 (0)