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 34e0018f perf(arrow/decimal): return MaxPrecision as a constant per 
width (#1520)
34e0018f is described below

commit 34e0018f28af719b232e75d20ac7a57bee114e39
Author: Harrison Crosse <[email protected]>
AuthorDate: Wed Nov 11 17:13:14 2026 -0500

    perf(arrow/decimal): return MaxPrecision as a constant per width (#1520)
    
    ### Rationale for this change
    
    `MaxPrecision[T]()` recomputes a fixed value with `math.Pow` and
    `math.Log10` on every call, about 16 ns, and `Decimal32FromFloat` and
    `Decimal64FromFloat` call it for every value. Returning the literals is
    simpler to check and lets the call inline.
    
    ### What changes are included in this PR?
    
    `MaxPrecision[T]()` returns 9, 18, 38 or 76 by type width.
    `Decimal32FromFloat` and `Decimal64FromFloat` benefit per value,
    including when their builders parse JSON numbers. `Decimal32.Rescale`
    and `Decimal64.Rescale` benefit only when scales differ and the value is
    nonzero. `NewDecimalType` and `NarrowestDecimalType` benefit once per
    call.
    
    On an Apple M4 Max with 14 cores, macOS arm64 and Go 1.26.10, I measured
    ten interleaved samples per version:
    
    ```text
                             before ns/op     after ns/op      change
    MaxPrecisionConcrete     16.455  ±  5%    1.148  ± 17%     -93.02%
    MaxPrecisionGeneric      15.5950 ±  3%    0.7817 ± 14%     -94.99%
    Decimal64FromFloat       19.685  ±  3%    3.882  ± 20%     -80.28%
    Decimal64Rescale         18.885  ±  5%    2.138  ± 15%     -88.68%
    NewDecimalType           27.245  ±  5%    7.793  ±  8%     -71.40%
    NarrowestDecimalType     59.060  ± 13%    7.215  ± 17%     -87.78%
    ```
    
    ### Are these changes tested?
    
    `arrow`'s existing `TestNewDecimalTypeValidatesPrecision` and
    `TestNarrowestDecimalType` check all four values.
    
    Vet, `-race -tags assert,test` tests, lint, the Release Audit Tool and
    targeted `GOARCH=386` tests passed.
    
    ### Are there any user-facing changes?
    
    None. There is no API change.
---
 arrow/decimal/decimal.go | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/arrow/decimal/decimal.go b/arrow/decimal/decimal.go
index a51b8563..75ac8ef9 100644
--- a/arrow/decimal/decimal.go
+++ b/arrow/decimal/decimal.go
@@ -72,9 +72,18 @@ type (
 )
 
 func MaxPrecision[T DecimalTypes]() int {
-       // max precision is computed by Floor(log10(2^(nbytes * 8 - 1) - 1))
+       // Floor(log10(2^(8*Sizeof(T)-1) - 1)), as literals so the call inlines.
        var z T
-       return int(math.Floor(math.Log10(math.Pow(2, 
float64(unsafe.Sizeof(z))*8-1) - 1)))
+       switch unsafe.Sizeof(z) {
+       case 4:
+               return 9
+       case 8:
+               return 18
+       case 16:
+               return 38
+       default:
+               return 76
+       }
 }
 
 func (d Decimal32) Negate() Decimal32 { return -d }

Reply via email to