From 5052c856b84ee9045cb2b2fcc33f7c3668c79f28 Mon Sep 17 00:00:00 2001 From: xtaci Date: Wed, 20 May 2020 22:33:26 +0800 Subject: [PATCH] optimize msb() function to de bruijin sequence --- alloc.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/alloc.go b/alloc.go index 3ce8b37..52887f6 100644 --- a/alloc.go +++ b/alloc.go @@ -5,7 +5,10 @@ import ( "sync" ) -var defaultAllocator *Allocator +var ( + defaultAllocator *Allocator + debruijinPos = [...]byte{0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9} +) func init() { defaultAllocator = NewAllocator() @@ -57,12 +60,14 @@ func (alloc *Allocator) Put(buf []byte) error { } // msb return the pos of most significiant bit -func msb(size int) uint16 { - var pos uint16 - size >>= 1 - for size > 0 { - size >>= 1 - pos++ - } - return pos +// http://supertech.csail.mit.edu/papers/debruijn.pdf +func msb(size int) byte { + v := uint32(size) + v |= v >> 1 + v |= v >> 2 + v |= v >> 4 + v |= v >> 8 + v |= v >> 16 + v = (v >> 1) + 1 + return debruijinPos[(v*0x077CB531)>>27] }