*** ./src/backend/utils/mmgr/aset.c.orig 2009-06-01 23:12:10.000000000 +0900 --- ./src/backend/utils/mmgr/aset.c 2009-06-02 08:47:30.000000000 +0900 *************** *** 263,268 **** --- 263,287 ---- * that size <= ALLOC_CHUNK_LIMIT. * ---------- */ + #if defined(__i386__) && defined(__GNUC__) + /* + * faster version of AllocSetFreeIndex for x86 architecure. + * this function runs in O(1). + */ + static inline int + AllocSetFreeIndex(Size size) + { + int idx; + + if (__builtin_expect(size < (1 << ALLOC_MINBITS), 0)) + size = (1 << ALLOC_MINBITS); + + /* bsr(Bit Scan Reverse): Search the most significant set bit */ + __asm__ ("bsr %1, %0" :"=r"(idx) :"g"(size - 1)); + + return idx - (ALLOC_MINBITS - 1); + } + #else static inline int AllocSetFreeIndex(Size size) { *************** *** 281,286 **** --- 300,306 ---- return idx; } + #endif /* defined(__i386__) && defined(__GNUC__) */ #ifdef RANDOMIZE_ALLOCATED_MEMORY