andygrove opened a new pull request, #6419:
URL: https://github.com/apache/arrow-rs/pull/6419

   # 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
   
   # 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 performance optimization.
   
   # 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.
   -->
   
   
   I noticed two areas of overhead in the current approach to verifying decimal 
precision.
   
   1. In the event of an overflow, we are building a formatted string for an 
error message, which is then discarded in some cases, so we should avoid that 
cost
   2. The range check code introduces a memcpy which can be avoided
   
   I tested the following variations of the decimal precision check in Rust 
playground.
   
   ```rust
   fn validate_decimal_precision1(value: i128, precision: u8) -> bool {
       if precision > DECIMAL128_MAX_PRECISION {
           return false;
       }
       let idx = usize::from(precision) - 1;
       value >= MIN_DECIMAL_FOR_EACH_PRECISION[idx] && value <= 
MAX_DECIMAL_FOR_EACH_PRECISION[idx]
   }
   
   // based on arrow-rs version
   fn validate_decimal_precision2(value: i128, precision: u8) -> bool {
       if precision > DECIMAL128_MAX_PRECISION {
           return false;
       }
   
       let max = MAX_DECIMAL_FOR_EACH_PRECISION[usize::from(precision) - 1];
       let min = MIN_DECIMAL_FOR_EACH_PRECISION[usize::from(precision) - 1];
   
       if value > max {
           false
       } else if value < min {
           false
       } else {
           true
       }
   }
   ```
   
   `validate_decimal_precision1` avoids a `memcpy` that appears in 
`validate_decimal_precision2`:
   
   ```
   playground::validate_decimal_precision1:
        subq    $1304, %rsp
        movb    %dl, %al
        movb    %al, 23(%rsp)
        movq    %rsi, 24(%rsp)
        movq    %rdi, 32(%rsp)
        movq    %rdi, 1264(%rsp)
        movq    %rsi, 1272(%rsp)
        movb    %al, 1287(%rsp)
        cmpb    $38, %al
        ja      .LBB9_2
        movb    23(%rsp), %al
        movb    %al, 1303(%rsp)
        movzbl  %al, %eax
        movq    %rax, %rcx
        subq    $1, %rcx
        movq    %rcx, 8(%rsp)
        cmpq    $1, %rax
        jb      .LBB9_4
        jmp     .LBB9_3
   
   playground::validate_decimal_precision2:
        subq    $1368, %rsp
        movb    %dl, %al
        movb    %al, 55(%rsp)
        movq    %rsi, 56(%rsp)
        movq    %rdi, 64(%rsp)
        movq    %rdi, 1296(%rsp)
        movq    %rsi, 1304(%rsp)
        movb    %al, 1327(%rsp)
        cmpb    $38, %al
        ja      .LBB10_2
        leaq    80(%rsp), %rdi
        leaq    .L__unnamed_5(%rip), %rsi
        movl    $608, %edx
        callq   memcpy@PLT   <----- MEMCPY HERE
        movb    55(%rsp), %al
        movb    %al, 1367(%rsp)
        movzbl  %al, %eax
        movq    %rax, %rcx
        subq    $1, %rcx
        movq    %rcx, 40(%rsp)
        cmpq    $1, %rax
        jb      .LBB10_4
        jmp     .LBB10_3
   ```
   
   # Are there any user-facing changes?
   
   No
   
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking 
change` label.
   -->
   


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

Reply via email to