From dba9497b5b587da873fbb2de89570ec8b36d604b Mon Sep 17 00:00:00 2001 From: John Naylor Date: Sun, 12 Feb 2023 15:17:40 +0700 Subject: [PATCH v28 05/10] Do bitmap conversion in one place rather than forcing callers to do it --- src/backend/access/common/tidstore.c | 31 +++++++++++++++------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/backend/access/common/tidstore.c b/src/backend/access/common/tidstore.c index ff8e66936e..ad8c0866e2 100644 --- a/src/backend/access/common/tidstore.c +++ b/src/backend/access/common/tidstore.c @@ -70,6 +70,7 @@ * and value, respectively. */ #define TIDSTORE_VALUE_NBITS 6 /* log(64, 2) */ +#define TIDSTORE_OFFSET_MASK ((1 << TIDSTORE_VALUE_NBITS) - 1) /* A magic value used to identify our TidStores. */ #define TIDSTORE_MAGIC 0x826f6a10 @@ -158,8 +159,8 @@ typedef struct TidStoreIter static void tidstore_iter_extract_tids(TidStoreIter *iter, uint64 key, uint64 val); static inline BlockNumber key_get_blkno(TidStore *ts, uint64 key); -static inline uint64 encode_key_off(TidStore *ts, BlockNumber block, uint32 offset, uint32 *off); -static inline uint64 tid_to_key_off(TidStore *ts, ItemPointer tid, uint32 *off); +static inline uint64 encode_key_off(TidStore *ts, BlockNumber block, uint32 offset, uint64 *off_bit); +static inline uint64 tid_to_key_off(TidStore *ts, ItemPointer tid, uint64 *off_bit); /* * Create a TidStore. The returned object is allocated in backend-local memory. @@ -376,10 +377,10 @@ tidstore_add_tids(TidStore *ts, BlockNumber blkno, OffsetNumber *offsets, for (int i = 0; i < num_offsets; i++) { - uint32 off; + uint64 off_bit; /* encode the tid to key and val */ - key = encode_key_off(ts, blkno, offsets[i], &off); + key = encode_key_off(ts, blkno, offsets[i], &off_bit); /* make sure we scanned the line pointer array in order */ Assert(key >= prev_key); @@ -401,7 +402,7 @@ tidstore_add_tids(TidStore *ts, BlockNumber blkno, OffsetNumber *offsets, prev_key = key; } - off_bitmap |= UINT64CONST(1) << off; + off_bitmap |= off_bit; } /* save the final index for later */ @@ -441,10 +442,10 @@ tidstore_lookup_tid(TidStore *ts, ItemPointer tid) { uint64 key; uint64 val = 0; - uint32 off; + uint64 off_bit; bool found; - key = tid_to_key_off(ts, tid, &off); + key = tid_to_key_off(ts, tid, &off_bit); if (TidStoreIsShared(ts)) found = shared_rt_search(ts->tree.shared, key, &val); @@ -454,7 +455,7 @@ tidstore_lookup_tid(TidStore *ts, ItemPointer tid) if (!found) return false; - return (val & (UINT64CONST(1) << off)) != 0; + return (val & off_bit) != 0; } /* @@ -660,26 +661,28 @@ key_get_blkno(TidStore *ts, uint64 key) /* Encode a tid to key and offset */ static inline uint64 -tid_to_key_off(TidStore *ts, ItemPointer tid, uint32 *off) +tid_to_key_off(TidStore *ts, ItemPointer tid, uint64 *off_bit) { uint32 offset = ItemPointerGetOffsetNumber(tid); BlockNumber block = ItemPointerGetBlockNumber(tid); - return encode_key_off(ts, block, offset, off); + return encode_key_off(ts, block, offset, off_bit); } /* encode a block and offset to a key and partial offset */ static inline uint64 -encode_key_off(TidStore *ts, BlockNumber block, uint32 offset, uint32 *off) +encode_key_off(TidStore *ts, BlockNumber block, uint32 offset, uint64 *off_bit) { uint64 key; uint64 tid_i; + uint32 off_lower; - tid_i = offset | ((uint64) block << ts->control->offset_nbits); + off_lower = offset & TIDSTORE_OFFSET_MASK; + Assert(off_lower < (sizeof(uint64) * BITS_PER_BYTE)); - *off = tid_i & ((UINT64CONST(1) << TIDSTORE_VALUE_NBITS) - 1); + *off_bit = UINT64CONST(1) << off_lower; + tid_i = offset | ((uint64) block << ts->control->offset_nbits); key = tid_i >> TIDSTORE_VALUE_NBITS; - Assert(*off < (sizeof(uint64) * BITS_PER_BYTE)); return key; } -- 2.39.1