cbandy opened a new pull request, #303:
URL: https://github.com/apache/arrow-go/pull/303

   ### Rationale for this change
   
   We can check the bounds of the arguments to avoid an int64 division. The 
`Mul` and `Mul64` functions perform better on the systems I tested with this 
benchmark:
   
   <details>
   <summary><code>internal/utils/math_bench_test.go</code></summary>
   
   ```go
   // 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 utils
   
   import (
        "fmt"
        "math"
        "testing"
   
        "github.com/JohnCGriffin/overflow"
   )
   
   func names(x, y int64) (string, string) {
        xs := fmt.Sprint(x)
        ys := fmt.Sprint(y)
   
        if x == math.MaxInt32 {
                xs = "MaxInt32"
        }
        if x == math.MaxInt16 {
                xs = "MaxInt16"
        }
        if x == math.MaxInt {
                xs = "MaxInt"
        }
        if y == math.MaxInt32 {
                ys = "MaxInt32"
        }
        if y == math.MaxInt16 {
                ys = "MaxInt16"
        }
        if y == math.MaxInt {
                ys = "MaxInt"
        }
   
        return xs, ys
   }
   
   func BenchmarkAdd(b *testing.B) {
        b.Run("int", func(b *testing.B) {
                for _, bb := range [][]int{
                        {8192, 8192},
                        {math.MaxInt16, 1},
                        {math.MaxInt16, 5},
                        {math.MaxInt16, math.MaxInt16},
                        {math.MaxInt32, 1},
                        {math.MaxInt32, 5},
                        {math.MaxInt32, math.MaxInt32},
                        {math.MaxInt, math.MaxInt},
                } {
                        x, y := bb[0], bb[1]
                        xs, ys := names(int64(x), int64(y))
   
                        b.Run(fmt.Sprintf("New: %s + %s", xs, ys), func(b 
*testing.B) {
                                for b.Loop() {
                                        Add(x, y)
                                }
                        })
   
                        b.Run(fmt.Sprintf("Old: %s + %s", xs, ys), func(b 
*testing.B) {
                                for b.Loop() {
                                        overflow.Add(x, y)
                                }
                        })
                }
        })
   }
   
   func BenchmarkMul(b *testing.B) {
        b.Run("int", func(b *testing.B) {
                for _, bb := range [][]int{
                        {8192, 8192},
                        {math.MaxInt16, 1},
                        {math.MaxInt16, 5},
                        {math.MaxInt16, math.MaxInt16},
                        {math.MaxInt32, 1},
                        {math.MaxInt32, 5},
                        {math.MaxInt32, math.MaxInt32},
                        {math.MaxInt, math.MaxInt},
                } {
                        x, y := bb[0], bb[1]
                        xs, ys := names(int64(x), int64(y))
   
                        b.Run(fmt.Sprintf("New: %s × %s", xs, ys), func(b 
*testing.B) {
                                for b.Loop() {
                                        Mul(x, y)
                                }
                        })
   
                        b.Run(fmt.Sprintf("Old: %s × %s", xs, ys), func(b 
*testing.B) {
                                for b.Loop() {
                                        overflow.Mul(x, y)
                                }
                        })
                }
        })
   }
   
   func BenchmarkMul64(b *testing.B) {
        b.Run("int64", func(b *testing.B) {
                for _, bb := range [][]int64{
                        {8192, 8192},
                        {math.MaxInt16, 1},
                        {math.MaxInt16, 5},
                        {math.MaxInt16, math.MaxInt16},
                        {math.MaxInt32, 1},
                        {math.MaxInt32, 5},
                        {math.MaxInt32, math.MaxInt32},
                        {math.MaxInt64, math.MaxInt64},
                } {
                        x, y := bb[0], bb[1]
                        xs, ys := names(int64(x), int64(y))
   
                        b.Run(fmt.Sprintf("New: %s × %s", xs, ys), func(b 
*testing.B) {
                                for b.Loop() {
                                        Mul64(x, y)
                                }
                        })
   
                        b.Run(fmt.Sprintf("Old: %s × %s", xs, ys), func(b 
*testing.B) {
                                for b.Loop() {
                                        overflow.Mul64(x, y)
                                }
                        })
                }
        })
   }
   ```
   
   </details>
   
   This is an older machine:
   
   ```
   goos: linux
   goarch: amd64
   pkg: github.com/apache/arrow-go/v18/internal/utils
   cpu: Intel(R) Core(TM) i5-7Y57 CPU @ 1.20GHz
                                     │ bench_amd64_old.txt │         
bench_amd64_new.txt         │
                                     │       sec/op        │    sec/op     vs 
base               │
   Add/int/8192_+_8192-4                      2.323n ± 15%   2.249n ± 12%   
-3.16% (p=0.041 n=6)
   Add/int/MaxInt16_+_1-4                     2.439n ±  9%   2.355n ± 10%       
 ~ (p=0.310 n=6)
   Add/int/MaxInt16_+_5-4                     2.605n ± 34%   2.575n ± 27%       
 ~ (p=0.937 n=6)
   Add/int/MaxInt16_+_MaxInt16-4              2.478n ± 14%   2.456n ±  2%       
 ~ (p=0.485 n=6)
   Add/int/MaxInt32_+_1-4                     2.454n ±  6%   2.413n ±  5%       
 ~ (p=0.240 n=6)
   Add/int/MaxInt32_+_5-4                     2.502n ±  3%   2.530n ±  4%       
 ~ (p=0.589 n=6)
   Add/int/MaxInt32_+_MaxInt32-4              2.521n ± 29%   2.487n ±  6%       
 ~ (p=0.234 n=6)
   Add/int/MaxInt_+_MaxInt-4                  2.506n ±  5%   2.457n ±  4%       
 ~ (p=0.132 n=6)
   Mul/int/8192_×_8192-4                     15.135n ±  5%   3.917n ±  3%  
-74.12% (p=0.002 n=6)
   Mul/int/MaxInt16_×_1-4                    15.310n ± 13%   4.113n ± 41%  
-73.14% (p=0.002 n=6)
   Mul/int/MaxInt16_×_5-4                    15.825n ±  2%   3.950n ±  3%  
-75.04% (p=0.002 n=6)
   Mul/int/MaxInt16_×_MaxInt16-4             15.545n ±  6%   3.956n ± 13%  
-74.55% (p=0.002 n=6)
   Mul/int/MaxInt32_×_1-4                    15.855n ±  4%   3.882n ±  3%  
-75.51% (p=0.002 n=6)
   Mul/int/MaxInt32_×_5-4                    15.600n ± 34%   3.992n ±  4%  
-74.41% (p=0.002 n=6)
   Mul/int/MaxInt32_×_MaxInt32-4             16.390n ±  4%   4.052n ±  3%  
-75.28% (p=0.002 n=6)
   Mul/int/MaxInt_×_MaxInt-4                  15.68n ±  6%   17.09n ±  6%   
+8.96% (p=0.002 n=6)
   Mul64/int64/8192_×_8192-4                 14.195n ± 60%   4.029n ±  3%  
-71.61% (p=0.002 n=6)
   Mul64/int64/MaxInt16_×_1-4                14.870n ± 24%   7.782n ± 52%  
-47.67% (p=0.002 n=6)
   Mul64/int64/MaxInt16_×_5-4                14.375n ±  4%   4.066n ± 12%  
-71.71% (p=0.002 n=6)
   Mul64/int64/MaxInt16_×_MaxInt16-4         14.955n ±  3%   4.076n ±  2%  
-72.74% (p=0.002 n=6)
   Mul64/int64/MaxInt32_×_1-4                14.820n ±  2%   3.911n ±  2%  
-73.61% (p=0.002 n=6)
   Mul64/int64/MaxInt32_×_5-4                14.655n ±  2%   3.988n ±  4%  
-72.79% (p=0.002 n=6)
   Mul64/int64/MaxInt32_×_MaxInt32-4         14.580n ± 18%   3.876n ±  8%  
-73.42% (p=0.002 n=6)
   Mul64/int64/MaxInt_×_MaxInt-4              14.90n ±  4%   16.12n ±  3%   
+8.19% (p=0.002 n=6)
   geomean                                    8.287n         3.918n        
-52.72%
   ```
   
   The following is inside a `docker.io/i386/ubuntu` container on that same 
machine. I don't have any 32-bit hardware.
   
   ```
   goos: linux
   goarch: 386
   pkg: github.com/apache/arrow-go/v18/internal/utils
   cpu: Intel(R) Core(TM) i5-7Y57 CPU @ 1.20GHz
                                                           │ bench_386_old.txt 
│          bench_386_new.txt          │
                                                           │      sec/op       
│    sec/op     vs base               │
   Add/int/8192_+_8192-4                                          3.167n ±  8%  
 3.576n ± 11%  +12.90% (p=0.015 n=6)
   Add/int/MaxInt16_+_1-4                                         3.204n ±  1%  
 4.079n ± 25%  +27.31% (p=0.002 n=6)
   Add/int/MaxInt16_+_5-4                                         3.345n ±  6%  
 4.026n ±  4%  +20.33% (p=0.002 n=6)
   Add/int/MaxInt16_+_MaxInt16-4                                  3.315n ±  8%  
 4.175n ±  4%  +25.91% (p=0.002 n=6)
   Add/int/MaxInt_+_1-4                                           3.367n ±  4%  
 4.128n ± 12%  +22.60% (p=0.002 n=6)
   Add/int/MaxInt_+_5-4                                           3.289n ±  7%  
 4.197n ± 10%  +27.60% (p=0.002 n=6)
   Add/int/MaxInt_+_MaxInt-4                                      3.341n ±  2%  
 4.266n ±  2%  +27.71% (p=0.002 n=6)
   Add/int/MaxInt_+_MaxInt#01-4                                   3.345n ±  2%  
 4.093n ±  5%  +22.39% (p=0.002 n=6)
   Mul/int/8192_×_8192-4                                         10.640n ±  3%  
 8.125n ±  8%  -23.64% (p=0.002 n=6)
   Mul/int/MaxInt16_×_1-4                                        10.905n ±  2%  
 8.180n ±  4%  -24.99% (p=0.002 n=6)
   Mul/int/MaxInt16_×_5-4                                        10.750n ±  4%  
 8.151n ±  3%  -24.18% (p=0.002 n=6)
   Mul/int/MaxInt16_×_MaxInt16-4                                 10.535n ±  3%  
 7.946n ±  2%  -24.58% (p=0.002 n=6)
   Mul/int/MaxInt_×_1-4                                          10.790n ±  3%  
 6.432n ±  4%  -40.39% (p=0.002 n=6)
   Mul/int/MaxInt_×_5-4                                           11.13n ± 10%  
 10.60n ±  2%   -4.81% (p=0.004 n=6)
   Mul/int/MaxInt_×_MaxInt-4                                      10.52n ±  7%  
 10.92n ±  8%        ~ (p=0.093 n=6)
   Mul/int/MaxInt_×_MaxInt#01-4                                   10.99n ±  3%  
 10.68n ±  4%   -2.78% (p=0.041 n=6)
   Mul64/int64/8192_×_8192-4                                      23.38n ±  4%  
 25.37n ±  4%   +8.49% (p=0.002 n=6)
   Mul64/int64/MaxInt16_×_1-4                                     24.02n ±  2%  
 25.40n ±  2%   +5.75% (p=0.002 n=6)
   Mul64/int64/MaxInt16_×_5-4                                     23.69n ± 13%  
 25.26n ± 19%   +6.61% (p=0.026 n=6)
   Mul64/int64/MaxInt16_×_MaxInt16-4                              23.49n ±  4%  
 24.80n ±  1%   +5.56% (p=0.002 n=6)
   Mul64/int64/MaxInt_×_1-4                                       23.88n ±  7%  
 25.07n ±  2%   +5.01% (p=0.041 n=6)
   Mul64/int64/MaxInt_×_5-4                                       30.39n ±  5%  
 25.20n ±  4%  -17.09% (p=0.002 n=6)
   Mul64/int64/MaxInt_×_MaxInt-4                                  29.63n ± 12%  
 25.55n ±  4%  -13.77% (p=0.002 n=6)
   Mul64/int64/9223372036854775807_×_9223372036854775807-4        24.35n ± 20%  
 33.57n ±  2%  +37.91% (p=0.002 n=6)
   geomean                                                        9.641n        
 9.758n         +1.21%
   ```
   
   ### What changes are included in this PR?
   
   A new generic `Add` function in `internal/utils` returns the sum of any two 
integers and whether or not it overflowed.
   
   New `Mul` and `Mul64` functions in `internal/utils` return the product of 
two `int` or `int64`, respectively, and whether or not it overflowed. These 
functions perform better on the systems I tested.
   
   ### Are these changes tested?
   
   Yes.
   
   ### Are there any user-facing changes?
   
   No effect on the API. This drops one dependency.


-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to