-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize CIDR aggregation to improve performance and reduce memory usage
- Loading branch information
Showing
5 changed files
with
446 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package iputil | ||
|
||
// setBitAt sets the bit at the i-th position in the byte slice to the given value. | ||
// Panics if the index is out of bounds. | ||
// For example, | ||
// - setBitAt([0x00, 0x00], 8, 1) returns [0x00, 0b1000_0000]. | ||
// - setBitAt([0xff, 0xff], 0, 0) returns [0b0111_1111, 0xff]. | ||
func setBitAt(bytes []byte, i int, bit uint8) { | ||
if bit == 1 { | ||
bytes[i/8] |= 1 << (7 - i%8) | ||
} else { | ||
bytes[i/8] &^= 1 << (7 - i%8) | ||
} | ||
} | ||
|
||
// bitAt returns the bit at the i-th position in the byte slice. | ||
// The return value is either 0 or 1 as uint8. | ||
// Panics if the index is out of bounds. | ||
func bitAt(bytes []byte, i int) uint8 { | ||
return bytes[i/8] >> (7 - i%8) & 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package iputil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_bitAt(t *testing.T) { | ||
bytes := []byte{0b1010_1010, 0b0101_0101} | ||
assert.Equal(t, uint8(1), bitAt(bytes, 0)) | ||
assert.Equal(t, uint8(0), bitAt(bytes, 1)) | ||
assert.Equal(t, uint8(1), bitAt(bytes, 2)) | ||
assert.Equal(t, uint8(0), bitAt(bytes, 3)) | ||
|
||
assert.Equal(t, uint8(1), bitAt(bytes, 4)) | ||
assert.Equal(t, uint8(0), bitAt(bytes, 5)) | ||
assert.Equal(t, uint8(1), bitAt(bytes, 6)) | ||
assert.Equal(t, uint8(0), bitAt(bytes, 7)) | ||
|
||
assert.Equal(t, uint8(0), bitAt(bytes, 8)) | ||
assert.Equal(t, uint8(1), bitAt(bytes, 9)) | ||
assert.Equal(t, uint8(0), bitAt(bytes, 10)) | ||
assert.Equal(t, uint8(1), bitAt(bytes, 11)) | ||
|
||
assert.Equal(t, uint8(0), bitAt(bytes, 12)) | ||
assert.Equal(t, uint8(1), bitAt(bytes, 13)) | ||
assert.Equal(t, uint8(0), bitAt(bytes, 14)) | ||
assert.Equal(t, uint8(1), bitAt(bytes, 15)) | ||
|
||
assert.Panics(t, func() { bitAt(bytes, 16) }) | ||
} | ||
|
||
func Test_setBitAt(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
initial []byte | ||
index int | ||
bit uint8 | ||
expected []byte | ||
}{ | ||
{ | ||
name: "Set first bit to 1", | ||
initial: []byte{0b0000_0000}, | ||
index: 0, | ||
bit: 1, | ||
expected: []byte{0b1000_0000}, | ||
}, | ||
{ | ||
name: "Set last bit to 1", | ||
initial: []byte{0b0000_0000}, | ||
index: 7, | ||
bit: 1, | ||
expected: []byte{0b0000_0001}, | ||
}, | ||
{ | ||
name: "Set middle bit to 1", | ||
initial: []byte{0b0000_0000}, | ||
index: 4, | ||
bit: 1, | ||
expected: []byte{0b0000_1000}, | ||
}, | ||
{ | ||
name: "Set bit to 0", | ||
initial: []byte{0b1111_1111}, | ||
index: 3, | ||
bit: 0, | ||
expected: []byte{0b1110_1111}, | ||
}, | ||
{ | ||
name: "Set bit in second byte", | ||
initial: []byte{0b0000_0000, 0b0000_0000}, | ||
index: 9, | ||
bit: 1, | ||
expected: []byte{0b0000_0000, 0b0100_0000}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
setBitAt(tt.initial, tt.index, tt.bit) | ||
assert.Equal(t, tt.expected, tt.initial) | ||
}) | ||
} | ||
|
||
assert.Panics(t, func() { setBitAt([]byte{0x00}, 8, 1) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.