You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2024/09/18 17:33:48 UTC

[PR] perf: Avoid memcpy during decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

andygrove opened a new pull request, #952:
URL: https://github.com/apache/datafusion-comet/pull/952

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   N/A
   
   Builds on https://github.com/apache/datafusion-comet/pull/948
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   Small optimization to overflow check in decimal aggregates
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## How are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "kazuyukitanimura (via GitHub)" <gi...@apache.org>.
kazuyukitanimura commented on code in PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#discussion_r1772046900


##########
native/core/src/execution/datafusion/expressions/sum_decimal.rs:
##########
@@ -127,19 +127,22 @@ impl AggregateUDFImpl for SumDecimal {
     fn reverse_expr(&self) -> ReversedUDAF {
         ReversedUDAF::Identical
     }
+
+    fn is_nullable(&self) -> bool {
+        // SumDecimal is always nullable because overflows can cause null values

Review Comment:
   Wondering if this is true for ANSI.
   It looks the previous code is also hardcoding true, but this may be a good time to file an issue if there is not yet.



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Avoid memcpy during decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#issuecomment-2359162079

   10 runs of TPC-H q1 @ 100 GB:
   
   ## main branch
   
   ```
   
    "1": [
           13.25664234161377,
           11.419574975967407,
           11.131508111953735,
           11.158952474594116,
           11.132988929748535,
           11.101924657821655,
           11.077952146530151,
           11.080657720565796,
           11.011901378631592,
           11.053855419158936
       ]
   ```
   
   ## this PR
   
   ```
   "1": [
           12.989612579345703,
           10.942750453948975,
           10.66578459739685,
           10.707809686660767,
           10.62846064567566,
           10.636594295501709,
           10.699990034103394,
           10.733628034591675,
           10.689185380935669,
           10.626504182815552
       ]
   ```


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#discussion_r1773481241


##########
native/core/src/execution/datafusion/expressions/sum_decimal.rs:
##########
@@ -127,19 +127,22 @@ impl AggregateUDFImpl for SumDecimal {
     fn reverse_expr(&self) -> ReversedUDAF {
         ReversedUDAF::Identical
     }
+
+    fn is_nullable(&self) -> bool {
+        // SumDecimal is always nullable because overflows can cause null values

Review Comment:
   Good point. I filed https://github.com/apache/datafusion-comet/issues/961



-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#issuecomment-2371484341

   > I was a bit surprised to see a performance win from changing an if-else to a compound boolean expression, since this seems like something that an optimizing compiler should handle well. I think I confirmed that part by putting the example code above in the Rust playground, and in Release mode the `memcpy` doesn't exist. If I leave it in Debug mode, I see the memcpy the same as described above. That doesn't explain the real performance win in Comet, however.
   > 
   > My next guess was that hoisting the implementation from arrow-rs into Comet enabled better inlining opportunities. My ARM assembly is not as proficient as x86, but I believe the relevant bits are below. [This is current main branch disassembly](https://github.com/user-attachments/files/17068008/main.txt) for `comet::execution::datafusion::expressions::avg_decimal::AvgDecimalGroupsAccumulator::update_single`:
   > 
   > ```
   > bl arrow_data::decimal::validate_decimal_precision
   > ```
   > 
   > There's a branch and link that isn't present in this [PR's disassembly](https://github.com/user-attachments/files/17068009/pr.txt). The compiler is able to better inline the hoisted code.
   > 
   > I am not as familiar with Rust's build environment, so I'm not sure if this is expected when calling into code from other crates. I see Comet currently does `thin` LTO. I am curious if `full` would enable better inlining of functions like this, or if we're just at the limits of what the compiler can do. In general, this makes me curious about the performance limits of arrow-rs kernels in hot code paths, and may guide our future optimizations.
   
   Thanks @mbutrovich. This is very insightful. I'd like to go ahead and merge this PR but would also like to have a better understanding of why this is actually faster.


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Avoid memcpy during decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#issuecomment-2359324891

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/952?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   All modified and coverable lines are covered by tests :white_check_mark:
   > Project coverage is 34.02%. Comparing base [(`cbaf1c2`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/cbaf1c20505f4f1c57b2db977eb3d739c16ca87f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`e46643c`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/e46643c534ce80655cb39d97a0d478764e29eb9e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   > Report is 1 commits behind head on main.
   
   <details><summary>Additional details and impacted files</summary>
   
   
   ```diff
   @@             Coverage Diff              @@
   ##               main     #952      +/-   ##
   ============================================
   + Coverage     33.80%   34.02%   +0.22%     
   - Complexity      852      876      +24     
   ============================================
     Files           112      112              
     Lines         43276    43276              
     Branches       9572     9572              
   ============================================
   + Hits          14629    14725      +96     
   + Misses        25634    25518     -116     
   - Partials       3013     3033      +20     
   ```
   
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/952?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Avoid memcpy during decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#issuecomment-2362244962

   Comparison between main branch and this PR:
   
   ![tpch_allqueries](https://github.com/user-attachments/assets/688ad559-81f7-4465-9b1e-c49a018d3428)
   
   ![tpch_queries_speedup_rel](https://github.com/user-attachments/assets/7d18efd7-776f-45ed-ab12-ed22520e0a2c)
   


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Avoid memcpy during decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#issuecomment-2362415067

   I was a bit surprised to see a performance win from changing an if-else to a compound boolean expression, since this seems like something that an optimizing compiler should handle well. I think I confirmed that part by putting the example code above in the Rust playground, and in Release mode the `memcpy` doesn't exist. If I leave it in Debug mode, I see the memcpy the same as described above. That doesn't explain the real performance win in Comet, however.
   
   My next guess was that hoisting the implementation from arrow-rs into Comet enabled better inlining opportunities. My ARM assembly is not as proficient as x86, but I believe the relevant bits are below. [This is current main branch disassembly](https://github.com/user-attachments/files/17068008/main.txt) for `comet::execution::datafusion::expressions::avg_decimal::AvgDecimalGroupsAccumulator::update_single`:
   ```
   bl arrow_data::decimal::validate_decimal_precision
   ```
   There's a branch and link that isn't present in this [PR's disassembly](https://github.com/user-attachments/files/17068009/pr.txt). The compiler is able to better inline the hoisted code.
   
   I am not as familiar with Rust's build environment, so I'm not sure if this is expected when calling into code from other crates. I see Comet currently does `thin` LTO. I am curious if `full` would enable better inlining of functions like this, or if we're just at the limits of what the compiler can do. In general, this makes me curious about the performance limits of arrow-rs kernels in hot code paths, and may guide our future optimizations.


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Avoid memcpy during decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#issuecomment-2359057787

   Benchmark results:
   
   ```
   aggregate/avg_decimal_comet
                           time:   [905.95 µs 907.59 µs 909.32 µs]
                           change: [-4.4075% -4.0254% -3.6624%] (p = 0.00 < 0.05)
                           Performance has improved.
   Found 4 outliers among 100 measurements (4.00%)
     4 (4.00%) high mild
   
   aggregate/sum_decimal_comet
                           time:   [781.78 µs 786.56 µs 791.40 µs]
                           change: [-15.686% -15.052% -14.420%] (p = 0.00 < 0.05)
                           Performance has improved.
   ```


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Avoid memcpy during decimal precision check in decimal aggregates (sum and avg) [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #952:
URL: https://github.com/apache/datafusion-comet/pull/952#issuecomment-2359207093

   ```
   aggregate/avg_decimal_datafusion
                           time:   [648.82 µs 651.35 µs 654.29 µs]
   aggregate/avg_decimal_comet
                           time:   [882.83 µs 883.65 µs 884.48 µs]
   aggregate/sum_decimal_datafusion
                           time:   [695.71 µs 696.20 µs 696.70 µs]
   aggregate/sum_decimal_comet
                           time:   [784.45 µs 786.38 µs 788.42 µs]
   ```


-- 
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-unsubscribe@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org