This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new c60fb003 perf(compute): speed up bounds checks for dense take indices 
(#1653)
c60fb003 is described below

commit c60fb0034a2f362e3d36a1153eaad48a4db302a8
Author: kurtoskalac <[email protected]>
AuthorDate: Mon Nov 23 18:31:36 2026 +0100

    perf(compute): speed up bounds checks for dense take indices (#1653)
    
    ### Why
    
    Checking a dense index array can take a substantial part of a checked
    `take` call. The random-take benchmark went from 932 to 475 µs for
    131,072 indices.
    Medians from ten single-core runs on Go 1.26.8 against `f1ab644`.
    
    ### What
    
    Use the existing vectorized min/max reductions for large, dense 32- and
    64-bit index arrays.
    
    ### Implementation
    
    The extrema prove bounds for a valid batch. If that check fails, scan in
    input order to report the first invalid index.
    
    ---------
    
    Co-authored-by: Minh Vu <[email protected]>
---
 arrow/compute/internal/kernels/helpers.go          | 38 +++++++++----
 .../compute/take_checked_random_benchmark_test.go  | 64 ++++++++++++++++++++++
 arrow/compute/vector_selection_test.go             | 39 +++++++++++++
 3 files changed, 131 insertions(+), 10 deletions(-)

diff --git a/arrow/compute/internal/kernels/helpers.go 
b/arrow/compute/internal/kernels/helpers.go
index 5daa31a1..8abe669f 100644
--- a/arrow/compute/internal/kernels/helpers.go
+++ b/arrow/compute/internal/kernels/helpers.go
@@ -29,6 +29,7 @@ import (
        "github.com/apache/arrow-go/v18/arrow/memory"
        "github.com/apache/arrow-go/v18/arrow/scalar"
        "github.com/apache/arrow-go/v18/internal/bitutils"
+       sharedutils "github.com/apache/arrow-go/v18/internal/utils"
        "golang.org/x/exp/constraints"
 )
 
@@ -941,16 +942,9 @@ func checkIndexBoundsImpl[T arrow.IntType | 
arrow.UintType](values *exec.ArraySp
        }
        return bitutils.VisitSetBitRuns(bitmap, values.Offset, values.Len,
                func(pos, length int64) error {
-                       outOfBounds := false
-                       for i := int64(0); i < length; i++ {
-                               outOfBounds = outOfBounds || 
isOutOfBounds(valuesData[pos+i])
-                       }
-                       if outOfBounds {
-                               for i := int64(0); i < length; i++ {
-                                       if isOutOfBounds(valuesData[pos+i]) {
-                                               return fmt.Errorf("%w: %d out 
of bounds",
-                                                       arrow.ErrIndex, 
valuesData[pos+i])
-                                       }
+                       for _, index := range valuesData[pos : pos+length] {
+                               if isOutOfBounds(index) {
+                                       return fmt.Errorf("%w: %d out of 
bounds", arrow.ErrIndex, index)
                                }
                        }
                        return nil
@@ -958,6 +952,9 @@ func checkIndexBoundsImpl[T arrow.IntType | 
arrow.UintType](values *exec.ArraySp
 }
 
 func checkIndexBounds(values *exec.ArraySpan, upperLimit uint64) error {
+       if values.Nulls == 0 && values.Len >= 64 && denseIndexBoundsOK(values, 
upperLimit) {
+               return nil
+       }
        switch values.Type.ID() {
        case arrow.INT8:
                return checkIndexBoundsImpl[int8](values, upperLimit)
@@ -980,6 +977,27 @@ func checkIndexBounds(values *exec.ArraySpan, upperLimit 
uint64) error {
        }
 }
 
+// Use the existing vectorized reductions for dense 32- and 64-bit indices.
+// On failure, the regular path identifies the first offending index.
+func denseIndexBoundsOK(values *exec.ArraySpan, upperLimit uint64) bool {
+       switch values.Type.ID() {
+       case arrow.INT32:
+               minimum, maximum := 
sharedutils.GetMinMaxInt32(exec.GetSpanValues[int32](values, 1))
+               return minimum >= 0 && uint64(maximum) < upperLimit
+       case arrow.UINT32:
+               _, maximum := 
sharedutils.GetMinMaxUint32(exec.GetSpanValues[uint32](values, 1))
+               return uint64(maximum) < upperLimit
+       case arrow.INT64:
+               minimum, maximum := 
sharedutils.GetMinMaxInt64(exec.GetSpanValues[int64](values, 1))
+               return minimum >= 0 && uint64(maximum) < upperLimit
+       case arrow.UINT64:
+               _, maximum := 
sharedutils.GetMinMaxUint64(exec.GetSpanValues[uint64](values, 1))
+               return maximum < upperLimit
+       default:
+               return false
+       }
+}
+
 func checkIndexBoundsChunked(values *arrow.Chunked, upperLimit uint64) error {
        var span exec.ArraySpan
        for _, v := range values.Chunks() {
diff --git a/arrow/compute/take_checked_random_benchmark_test.go 
b/arrow/compute/take_checked_random_benchmark_test.go
new file mode 100644
index 00000000..899506b4
--- /dev/null
+++ b/arrow/compute/take_checked_random_benchmark_test.go
@@ -0,0 +1,64 @@
+// 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 compute_test
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/compute"
+       "github.com/apache/arrow-go/v18/arrow/memory"
+)
+
+func BenchmarkCheckedRandomTake(b *testing.B) {
+       const rows = 1 << 18
+       mem := memory.DefaultAllocator
+       values := make([]int64, rows)
+       for i := range values {
+               values[i] = int64(i)
+       }
+       indices := make([]uint32, rows/2)
+       state := uint32(1)
+       for i := range indices {
+               state = state*1664525 + 1013904223
+               indices[i] = state >> 14
+       }
+       valuesBuilder := array.NewInt64Builder(mem)
+       valuesBuilder.AppendValues(values, nil)
+       input := valuesBuilder.NewInt64Array()
+       valuesBuilder.Release()
+       defer input.Release()
+       indicesBuilder := array.NewUint32Builder(mem)
+       indicesBuilder.AppendValues(indices, nil)
+       selection := indicesBuilder.NewUint32Array()
+       indicesBuilder.Release()
+       defer selection.Release()
+
+       ctx := context.Background()
+       opts := compute.TakeOptions{BoundsCheck: true}
+       b.ReportAllocs()
+       b.SetBytes(int64(len(indices) * arrow.Int64SizeBytes))
+       for b.Loop() {
+               out, err := compute.TakeArrayOpts(ctx, input, selection, opts)
+               if err != nil {
+                       b.Fatal(err)
+               }
+               out.Release()
+       }
+}
diff --git a/arrow/compute/vector_selection_test.go 
b/arrow/compute/vector_selection_test.go
index 91160f4c..717cd633 100644
--- a/arrow/compute/vector_selection_test.go
+++ b/arrow/compute/vector_selection_test.go
@@ -1992,6 +1992,45 @@ func TestDictionarySelectionLengthAndBounds(t 
*testing.T) {
        })
 }
 
+func TestTakeChecksDenseSlicedIndices(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+       defer mem.AssertSize(t, 0)
+       builder := array.NewInt32Builder(mem)
+       defer builder.Release()
+
+       const count = 128
+       values := make([]int32, count)
+       for i := range values {
+               values[i] = int32(i)
+       }
+       builder.AppendValues(values, nil)
+       input := builder.NewInt32Array()
+       defer input.Release()
+
+       indices := append([]int32{-1}, values...)
+       builder.AppendValues(indices, nil)
+       whole := builder.NewInt32Array()
+       defer whole.Release()
+       sliced := array.NewSlice(whole, 1, count+1)
+       defer sliced.Release()
+       out, err := compute.TakeArray(context.Background(), input, sliced)
+       require.NoError(t, err)
+       defer out.Release()
+       require.True(t, array.Equal(input, out))
+
+       for _, invalid := range []int32{-2, count} {
+               indices[65] = invalid
+               builder.AppendValues(indices, nil)
+               whole := builder.NewInt32Array()
+               sliced := array.NewSlice(whole, 1, count+1)
+               _, err := compute.TakeArray(context.Background(), input, sliced)
+               sliced.Release()
+               whole.Release()
+               require.ErrorIs(t, err, arrow.ErrIndex)
+               require.ErrorContains(t, err, fmt.Sprintf("%d out of bounds", 
invalid))
+       }
+}
+
 func TestTakeKernels(t *testing.T) {
        suite.Run(t, new(TakeKernelTest))
        for _, dt := range numericTypes {

Reply via email to