Skip to content

Closes #5043: Investigate performance loss from negative bigint changes - #5044

Merged
ajpotts merged 1 commit into
Bears-R-Us:mainfrom
1RyanK:5043-Investigate_performance_loss_from_negative_bigint_changes
Dec 4, 2025
Merged

ajpotts merged 1 commit into
Bears-R-Us:mainfrom
1RyanK:5043-Investigate_performance_loss_from_negative_bigint_changes

Conversation

@1RyanK

@1RyanK 1RyanK commented Nov 10, 2025 •

Copy link
Copy Markdown
Contributor

While PR #4985 technically fixed the issue of general negative bigint problems, it caused a performance regression. I believe this is primarily due to the overhead of creating the sign array. Instead, here's basically what happens in the code:

    any_neg = np.any(flat < 0)
    req_bits: int
    if any_neg:
        req_bits = max(flat.max().bit_length(), (-flat.min()).bit_length()) + 1
    else:
        req_bits = flat.max().bit_length()

Then the code figures out how many times to pull off uint64 limbs, rather than waiting until everything is zero (which doesn't happen in the negative case, it just continues to be -1).

The code runs out to two three separate Chapel functions depending on the case. If the input is just an int64 or uint64 array, it converts it directly to bigint (similar for float64 or some other kind of floating point input). If the input is numpy's version of a bigint array, it goes to the multi-limb version (unfortunately, this is the case even if everything comes out to just one limb, but I think the performance loss here is not that bad). If the input data had any negative values, it treats the limbs as signed (all bits are positive and the top bit is negative). However, it's hard to create a bigint like this (AFAIK). So Chapel-side, it creates a signs array, strips off the top bit of every limb and treats it as a bool to reference later. Either way, it goes to the "Horner fold" step, which, as ChatGPT tells me, is possibly a faster way to create the bigints. Previously the code was bit shifting the limbs into the right spot and then adding them to the bigint value. The idea here is that you start with the highest limb of the data, then you bitshift it and add in the next limb, bitshift what you have and add in the next limb, and so on. You can read more about the generic version of this here (take x = 2**64). Then it adds in the signed bit as necessary.

It also handles the case where max_bits is not -1.

Hopefully any loss in performance is made up by a few factors:

  1. Previously the Python code was stripping the limbs off by modding by 2**64 and then integer dividing by the same value. I think it could speed the code up to do a bitmask by 2**64 - 1 and then bitshift by 64.
  2. Supposedly the Horner fold has better performance.
  3. If the input is only one limb of int64 or uint64 data, it should go to the single limb version and that should run quicker.

This also handles all cases of numeric data to bigint output in a single function, so if the performance is back up, then I can cut out some code in the array function.

I went ahead and cut the old bigint code out.

Closes #5043: Investigate performance loss from negative bigint changes

@1RyanK
1RyanK force-pushed the 5043-Investigate_performance_loss_from_negative_bigint_changes branch 2 times, most recently from b920070 to 347aca5 Compare November 12, 2025 12:42
@1RyanK
1RyanK force-pushed the 5043-Investigate_performance_loss_from_negative_bigint_changes branch from 850b58d to 1fbf42c Compare November 13, 2025 17:27
Comment thread arkouda/numpy/pdarraycreation.py Resolved

@ajpotts ajpotts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@1RyanK
1RyanK force-pushed the 5043-Investigate_performance_loss_from_negative_bigint_changes branch 4 times, most recently from 2bb82f6 to faee9c4 Compare November 20, 2025 16:13
@ajpotts
ajpotts requested a review from e-kayrakli November 25, 2025 17:03
@1RyanK
1RyanK force-pushed the 5043-Investigate_performance_loss_from_negative_bigint_changes branch 3 times, most recently from f3e4f5a to deeb252 Compare December 1, 2025 17:30
@1RyanK
1RyanK force-pushed the 5043-Investigate_performance_loss_from_negative_bigint_changes branch from deeb252 to 7649045 Compare December 1, 2025 20:06

@drculhane drculhane left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@ajpotts
ajpotts added this pull request to the merge queue Dec 4, 2025
Merged via the queue into Bears-R-Us:main with commit 4429ff0 Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate performance loss from negative bigint changes

3 participants