Gabriel39 opened a new pull request, #70921:
URL: https://github.com/apache/doris/pull/70921

   ### Background
   
   Decimal multiplication checks native integer overflow before enforcing 
decimal precision and rescaling. The signed int128 check currently uses a 
compiler-rt-derived implementation with 128-bit division. This implementation 
avoids a missing `__muloti4` runtime symbol on some ARM toolchains.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #51715 (introduced the ARM-compatible software check), #51533 
(fixed signed int128 overflow detection)
   PR Type: Performance
   
   Problem Summary:
   
   The division-based check adds CPU overhead to each checked Decimal 
multiplication. Replace it with unsigned builtin multiplication on operand 
magnitudes and a sign-dependent bound, preserving the ARM compatibility 
workaround and the complete signed int128 range. Results, decimal types, 
precision checks and overflow behavior remain unchanged; most of the diff adds 
tests.
   
   ### Details
   
   - **Change map:** 46 production lines changed (14 added, 32 removed), 240 
unit/regression test lines added, and 51 automatically generated 
expected-output lines added.
   - **Why it was slow:** Ordinary nonzero/nonunit products perform a 128-bit 
division to detect overflow; the baseline code-generation probes call 
`__udivti3`.
   - **Optimization:** Compute magnitudes in unsigned arithmetic so even 
`INT128_MIN` is safe, use unsigned `__builtin_mul_overflow`, then apply the 
signed bound. Negative products can have magnitude `2^127`, for example 
`(2^126) * (-2)`. Preserve the zero/unit fast path and wrapped result bits on 
overflow. The approach is inspired by [DuckDB's checked hugeint 
multiplication](https://github.com/duckdb/duckdb/blob/d54cbbe3df/src/common/types/hugeint.cpp#L155-L266).
   - **Results:** Local release-mode primitive microbenchmark on a dual-socket 
Intel Xeon Platinum 8457C, Clang 20.1.8, `-O3 -DNDEBUG -march=x86-64-v3`. Both 
implementations use the same input/output arrays in one binary: 65,536 pairs, 
five warm-up batches, 100 batches per sample, 11 paired samples with 
alternating order, pinned to one logical CPU. The shared host was busy, so the 
table reports median thread CPU time rather than query latency.
   
   | Synthetic input distribution | Before, ns/product | After, ns/product |
   | --- | ---: | ---: |
   | Discount-shaped positive operands | 13.37 | 10.28 |
   | Charge-shaped positive operands | 12.33 | 8.77 |
   | Mixed signs | 18.16 | 9.23 |
   | Full-width overflowing products | 23.18 | 9.53 |
   | Random zero/unit factors | 6.22 | 6.36 |
   
   - **Applicability and cost:** Applies to checked signed int128 
multiplication, including Decimal arithmetic. No allocations or type changes 
are added. The zero/unit workload is about 2.4% slower by these CPU-time 
medians despite retaining its fast path; the other measured distributions 
improve. These measurements do not establish an end-to-end TPC-H or SSB speedup.
   
   ### Testing
   
   - Local ASan/UBSan BE build and FE build completed; the BE executable passed 
its GLIBC 2.17 symbol gate.
   - New unit tests compare 85,141 products with an independent 256-bit 
reference, plus aliased-output cases. Clang ASan/UBSan and GCC UBSan standalone 
runs pass. A deliberately broken minimum-value bound fails the boundary and 
power tests.
   - All six targeted BE tests pass through the standard ASan/UBSan runner: 
`run-be-ut.sh --run --filter='ArithmeticOverflowTest.*:CheckOverFlowTest.*'`.
   - Local regression suites `test_decimal128_checked_multiply`, 
`test_arithmetic_expressions` and `test_decimalv3_overflow` pass against the 
newly built BE. The new suite covers column/constant forms, nulls, signed and 
unsigned machine overflow, decimal precision overflow, and scaled 
wide-intermediate fallback. Its generated 41 result rows were independently 
checked with Python arbitrary-precision integers and Decimal.
   - Clang 16/20 x86_64 and ARM64 code-generation probes, plus GCC x86_64, 
contain no division or `__muloti4` call for the new helper. ARM validation is 
compile/code-generation only.
   - Local self-review, clang-format v16, clang-tidy, build hygiene and 
whitespace checks completed.
   
   ### Release note
   
   Reduce CPU overhead of checked Decimal multiplication while preserving 
existing results and overflow behavior.
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: No
   - Pre-submission status: Ready
   
   ### AI Disclosure
   
   - Tool: Codex (GPT-6)
   - Generated: Code, unit and regression tests, and this description.
   - Human review: Not yet reviewed by a human; local self-review and 
verification were performed by Codex.
   


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to