zeroshade commented on code in PR #1389:
URL: https://github.com/apache/arrow-go/pull/1389#discussion_r4328409638


##########
arrow/bitutil/bitmaps_test.go:
##########
@@ -214,6 +214,58 @@ func TestCopyBitmap(t *testing.T) {
        }
 }
 
+func TestTransferBitmapOverlapping(t *testing.T) {
+       tests := []struct {
+               name               string
+               srcSlice, dstSlice int
+               srcOffset          int
+               dstOffset          int
+               length             int
+       }{
+               {name: "aligned right partial", srcOffset: 0, dstOffset: 8, 
length: 31},
+               {name: "aligned left partial", srcOffset: 8, dstOffset: 0, 
length: 31},
+               {name: "aligned full bytes", srcOffset: 0, dstOffset: 16, 
length: 64},
+               {name: "unaligned right", srcOffset: 3, dstOffset: 11, length: 
145},
+               {name: "unaligned left", srcOffset: 11, dstOffset: 3, length: 
145},
+               {name: "same bit range", srcOffset: 5, dstOffset: 5, length: 
145},
+               {name: "aliased slice views", srcSlice: 2, dstSlice: 3, 
srcOffset: 3, dstOffset: 2, length: 145},
+               {name: "adjacent bits", srcOffset: 5, dstOffset: 6, length: 1},

Review Comment:
   Could you add a case where source and destination start in the same byte at 
different bit offsets and span several bytes, for example 0→1 with length 9? 
Skipping the copy when both start in the same byte still passes all 18 
subtests, but corrupts that case.



##########
arrow/bitutil/bitmaps_transfer_benchmark_test.go:
##########
@@ -0,0 +1,93 @@
+// 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 bitutil_test
+
+import (
+       "strconv"
+       "testing"
+
+       "github.com/apache/arrow-go/v18/arrow/bitutil"
+)
+
+// BenchmarkTransferBitmapAliasing measures the overlap snapshot path and the
+// allocation-free path, including disjoint ranges in the same backing buffer.
+func BenchmarkTransferBitmapAliasing(b *testing.B) {
+       operations := []struct {
+               name     string
+               transfer func([]byte, int, int, []byte, int)
+       }{
+               {name: "copy", transfer: bitutil.CopyBitmap},
+               {name: "invert", transfer: bitutil.InvertBitmap},
+       }
+       offsets := []struct {
+               name     string
+               src, dst int
+       }{
+               {name: "aligned"},
+               {name: "unaligned", src: 3, dst: 5},
+       }
+       layouts := []struct {
+               name               string
+               separate, disjoint bool
+               srcByte, dstByte   int
+       }{
+               {name: "separate", separate: true},
+               {name: "disjoint-same-buffer", disjoint: true},
+               {name: "overlap-forward", dstByte: 1},
+               {name: "overlap-backward", srcByte: 1},
+               {name: "same-range"},
+       }
+
+       for _, operation := range operations {
+               b.Run(operation.name, func(b *testing.B) {
+                       for _, nbytes := range []int{32, 1024, 64 * 1024} {
+                               b.Run(strconv.Itoa(nbytes), func(b *testing.B) {
+                                       for _, offset := range offsets {
+                                               b.Run(offset.name, func(b 
*testing.B) {
+                                                       for _, layout := range 
layouts {
+                                                               
b.Run(layout.name, func(b *testing.B) {
+                                                                       var 
src, dst []byte
+                                                                       if 
layout.separate {
+                                                                               
src = make([]byte, nbytes+8)
+                                                                               
dst = make([]byte, nbytes+8)
+                                                                       } else {
+                                                                               
backing := make([]byte, 2*nbytes+32)
+                                                                               
srcByte, dstByte := layout.srcByte, layout.dstByte
+                                                                               
if layout.disjoint {
+                                                                               
        dstByte = nbytes + 16
+                                                                               
}
+                                                                               
src, dst = backing[srcByte:], backing[dstByte:]
+                                                                       }
+                                                                       for i 
:= range src {
+                                                                               
src[i] = byte(i*37 + 0x5b)
+                                                                       }
+
+                                                                       
b.SetBytes(int64(nbytes))
+                                                                       
b.ReportAllocs()
+                                                                       
b.ResetTimer()
+                                                                       for i 
:= 0; i < b.N; i++ {

Review Comment:
   Nit: please use `for b.Loop()`. This benchmark could also live in 
`bitmaps_test.go` next to the other bitmap benchmarks.



##########
arrow/bitutil/bitmaps.go:
##########
@@ -421,6 +421,25 @@ func transferBitmap(mode transferMode, src []byte, 
srcOffset, length int, dst []
                return
        }
 
+       // Both transfer paths can overwrite source bytes before reading them 
when
+       // the ranges alias. Only snapshot the source when their byte spans 
overlap.
+       srcStart := srcOffset / 8
+       dstStart := dstOffset / 8
+       srcBytes := src[srcStart : 
srcStart+int(BytesForBits(int64(srcOffset%8+length)))]
+       dstBytes := dst[dstStart : 
dstStart+int(BytesForBits(int64(dstOffset%8+length)))]

Review Comment:
   On 386, `dstOffset%8+length` overflows before the `int64` conversion. With 
256 MiB buffers, `srcOffset=0`, `dstOffset=1` and `length=2147483647`, both 
copy and invert panic here with a negative slice bound. Both work on `main`. 
Please widen before adding, as `int64(dstOffset%8) + int64(length)`, on the 
source line above as well.



-- 
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]

Reply via email to