1
0
mirror of https://github.com/golang/go synced 2024-11-05 14:46:11 -07:00

go.tools/container/intsets: increase block size to 256 bits.

This consistently yields better performance with go/pointer.

Also: return int not word from ntz().

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/97570044
This commit is contained in:
Alan Donovan 2014-05-30 13:21:36 -04:00
parent 8df7a779db
commit fd72015344
2 changed files with 10 additions and 6 deletions

View File

@ -21,6 +21,10 @@ package intsets
// TODO(adonovan): implement Dense, a dense bit vector with a similar API.
// The space usage would be proportional to Max(), not Len(), and the
// implementation would be based upon big.Int.
//
// TODO(adonovan): experiment with making the root block indirect (nil
// iff IsEmpty). This would reduce the memory usage when empty and
// might simplify the aliasing invariants.
import (
"bytes"
@ -50,7 +54,7 @@ type word uintptr
const (
_m = ^word(0)
bitsPerWord = 8 << (_m>>8&1 + _m>>16&1 + _m>>32&1)
bitsPerBlock = 128
bitsPerBlock = 256 // optimal value for go/pointer solver performance
wordsPerBlock = bitsPerBlock / bitsPerWord
)
@ -161,9 +165,9 @@ func (b *block) min(take bool) int {
if w != 0 {
tz := ntz(w)
if take {
b.bits[i] = w &^ (1 << tz)
b.bits[i] = w &^ (1 << uint(tz))
}
return b.offset + int(i*bitsPerWord) + int(tz)
return b.offset + int(i*bitsPerWord) + tz
}
}
panic("BUG: empty block")

View File

@ -44,11 +44,11 @@ func nlz(x word) int {
// ntz returns the number of trailing zeros of x.
// From Hacker's Delight, fig 5.13.
func ntz(x word) word {
func ntz(x word) int {
if x == 0 {
return bitsPerWord
}
var n word = 1
n := 1
if bitsPerWord == 64 {
if (x & 0xffffffff) == 0 {
n = n + 32
@ -71,5 +71,5 @@ func ntz(x word) word {
n = n + 2
x = x >> 2
}
return n - x&1
return n - int(x&1)
}