Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FlipRightBit by doing it directly. #1610

Merged
merged 1 commit into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ func (n *NodeID) Copy() *NodeID {

// FlipRightBit flips the ith bit from LSB
func (n *NodeID) FlipRightBit(i int) *NodeID {
n.SetBit(i, n.Bit(i)^1)
bIndex := (n.PathLenBits() - i - 1) / 8
n.Path[bIndex] ^= 1 << uint(i%8)
return n
}

Expand Down Expand Up @@ -370,8 +371,7 @@ func (n *NodeID) MaskLeft(depth int) *NodeID {
// in the binary tree (often termed sibling node).
func (n *NodeID) Neighbor() *NodeID {
height := n.PathLenBits() - n.PrefixLenBits
n.FlipRightBit(height)
return n
return n.FlipRightBit(height)
}

// Siblings returns the siblings of the given node.
Expand Down
9 changes: 8 additions & 1 deletion storage/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func TestSetBit(t *testing.T) {
}
}

func TestFlipBit(t *testing.T) {
func TestFlipRightBit(t *testing.T) {
for _, tc := range []struct {
index []byte
i int
Expand Down Expand Up @@ -869,3 +869,10 @@ func BenchmarkAsKey(b *testing.B) {
_ = nID.AsKey()
}
}

func BenchmarkFlipRightBit(b *testing.B) {
nID := NewNodeIDFromHash(h2b("000102030405060708090A0B0C0D0E0F10111213"))
for i := 0; i < b.N; i++ {
nID.FlipRightBit(27)
}
}