From f5fa48311f457276a6d156f1e40cd262f08dd073 Mon Sep 17 00:00:00 2001 From: xtaci Date: Wed, 15 May 2019 16:14:14 +0800 Subject: [PATCH] save 1-loc --- alloc.go | 7 +++---- alloc_test.go | 11 ++++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/alloc.go b/alloc.go index 831f66d..3b02cb0 100644 --- a/alloc.go +++ b/alloc.go @@ -55,11 +55,10 @@ func (alloc *Allocator) Put(buf []byte) error { // msb return the pos of most significiant bit func msb(size int) uint16 { var pos uint16 - for { + size >>= 1 + for size > 0 { size >>= 1 - if size == 0 { - return pos - } pos++ } + return pos } diff --git a/alloc_test.go b/alloc_test.go index 3e1a2b3..e8b27e5 100644 --- a/alloc_test.go +++ b/alloc_test.go @@ -1,6 +1,9 @@ package smux -import "testing" +import ( + "math/rand" + "testing" +) func TestAllocGet(t *testing.T) { alloc := NewAllocator() @@ -70,3 +73,9 @@ func TestAllocPutThenGet(t *testing.T) { } } } + +func BenchmarkMSB(b *testing.B) { + for i := 0; i < b.N; i++ { + msb(rand.Int()) + } +}