zeroshade commented on code in PR #1398: URL: https://github.com/apache/arrow-go/pull/1398#discussion_r4339828906
########## internal/bitutils/bit_block_counter_benchmark_test.go: ########## @@ -0,0 +1,55 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 bitutils_test + +import ( + "fmt" + "math/rand/v2" + "testing" + + "github.com/apache/arrow-go/v18/arrow/bitutil" + "github.com/apache/arrow-go/v18/internal/bitutils" +) + +var fourWordsBenchmarkSink int64 + +func BenchmarkFourWordsUnaligned(b *testing.B) { + for _, length := range []int64{512, 1024, 4096} { + for _, offset := range []int64{1, 3, 7} { + bitmap := make([]byte, bitutil.BytesForBits(offset+length)) + rng := &rand.ChaCha8{} + _, _ = rng.Read(bitmap) + + b.Run(fmt.Sprintf("%dbits/offset%d", length, offset), func(b *testing.B) { + b.ReportAllocs() + b.SetBytes(int64(bitutil.BytesForBits(length))) + b.ResetTimer() + + var totalPopcnt int64 + for i := 0; i < b.N; i++ { Review Comment: Nit: please use `for b.Loop()` and drop `b.ResetTimer()`; go.mod already requires Go 1.26. ########## internal/bitutils/bit_block_counter_test.go: ########## @@ -199,3 +199,35 @@ func TestFourWordsRandomData(t *testing.T) { checkWithOffset(offsetI) } } + +func TestFourWordsUnalignedBoundaries(t *testing.T) { + for offset := int64(1); offset < 8; offset++ { + for _, length := range []int64{ + 255, 256, 257, + 320 - offset - 1, 320 - offset, 320 - offset + 1, + 512, 1024, 1280 - offset - 1, 1280 - offset, + } { + // Allocate only the bytes required by the requested bit range. + // In particular, the fast path must never read past the bitmap. + bitmap := make([]byte, bitutil.BytesForBits(offset+length)) Review Comment: An exact-length `make` can't catch an over-read here, because `loadWord` dereferences an unsafe `*uint64` without bounds checks. Changing the guard to `bitsRemaining < fourWordsBits` still passes this test, while a bitmap that ends right before an inaccessible page faults on the fifth load. Could you add a test that places the bitmap's last needed byte immediately before a page made inaccessible with `syscall.Mmap` + `syscall.Mprotect` (behind a linux/darwin build tag), covering lengths just below, at and above the threshold? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
