Skip to content

8355719: Reduce memory consumption of BigInteger.pow() #24690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 62 commits into from

Conversation

fabioromano1
Copy link
Contributor

@fabioromano1 fabioromano1 commented Apr 16, 2025

This PR optimizes BigInteger.pow(int) method. The primary enhancement in pow() is not concerned most on execution time, but rather in memory optimization, because the PR implementation does the "shift of the exponent" squaring the result rather than the base, so the base is not squared like in the current implementation, and this permits to save about half of the memory.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8355719: Reduce memory consumption of BigInteger.pow() (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24690/head:pull/24690
$ git checkout pull/24690

Update a local copy of the PR:
$ git checkout pull/24690
$ git pull https://git.openjdk.org/jdk.git pull/24690/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24690

View PR using the GUI difftool:
$ git pr show -t 24690

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24690.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 16, 2025

👋 Welcome back fabioromano1! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Apr 16, 2025

@fabioromano1 This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8355719: Reduce memory consumption of BigInteger.pow()

Reviewed-by: rgiulietti

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 229 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@rgiulietti) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk
Copy link

openjdk bot commented Apr 16, 2025

@fabioromano1 The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Apr 16, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 16, 2025
@mlbridge
Copy link

mlbridge bot commented Apr 16, 2025

Webrevs

@fabioromano1
Copy link
Contributor Author

@rgiulietti In this PR I've implemented nth root for BigIntegers using Newton's method and optimized BigInteger.pow(int) method. I've also uploaded a proof of convergence for the recurrence used to compute nth root.

@openjdk openjdk bot removed the rfr Pull request is ready for review label Apr 17, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 17, 2025
@openjdk
Copy link

openjdk bot commented Apr 17, 2025

⚠️ @fabioromano1 This pull request contains merges that bring in commits not present in the target repository. Since this is not a "merge style" pull request, these changes will be squashed when this pull request in integrated. If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change the title of this pull request to Merge <project>:<branch> where <project> is the name of another project in the OpenJDK organization (for example Merge jdk:master).

} else {
if ((long)bitLength() * exponent / Integer.SIZE > MAX_MAG_LENGTH) {
if ((bitLength() - 1L) * exponent >= (long) MAX_MAG_LENGTH << 5) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ((bitLength() - 1L) * exponent >= (long) MAX_MAG_LENGTH << 5) {
if ((bitLength() - 1L) * exponent >= 32L * MAX_MAG_LENGTH) {

or

Suggested change
if ((bitLength() - 1L) * exponent >= (long) MAX_MAG_LENGTH << 5) {
if ((bitLength() - 1L) * exponent >= (long) Integer.SIZE * MAX_MAG_LENGTH) {

Both variant are easier to read, more honest, and exactly as efficient as with the shift. The right-hand sides are compile-time constants, so they have no impact on runtime performance.

More generally, the runtime compilers are perfectly capable to optimize multiplications by constant powers of 2 and replace them with shifts, even if the other operand is not a constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rgiulietti What about (bitLength() - 1L) * exponent >= Integer.MAX_VALUE?

Copy link
Contributor

@rgiulietti rgiulietti May 7, 2025

Choose a reason for hiding this comment

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

Ah right, but you probably want

Suggested change
if ((bitLength() - 1L) * exponent >= (long) MAX_MAG_LENGTH << 5) {
if ((bitLength() - 1L) * exponent > Integer.MAX_VALUE) {

I mean > rather than >=

Copy link
Contributor Author

@fabioromano1 fabioromano1 May 7, 2025

Choose a reason for hiding this comment

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

Ah right, but you probably want

No, the sufficient condition to get the overflow is (bitLength() - 1L) * exponent + 1L > Integer.MAX_VALUE, which is equivalent to that one I wrote above.

Copy link
Contributor

Choose a reason for hiding this comment

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

At some point you proposed

(bitLength() - 1L) * exponent >= (long) MAX_MAG_LENGTH << 5

Given the value of MAX_MAG_LENGTH, which is 2^26, this is equivalent to

(bitLength() - 1L) * exponent >= 1L << 31

that is, to

(bitLength() - 1L) * exponent > Integer.MAX_VALUE

What am I missing?

Copy link
Contributor Author

@fabioromano1 fabioromano1 May 7, 2025

Choose a reason for hiding this comment

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

The condition A := (bitLength() - 1L) * exponent + 1L > Integer.MAX_VALUE is more accurate, as it compares the bit length of the result, in fact B := (bitLength() - 1L) * exponent >= (long) MAX_MAG_LENGTH << 5 implies A, but A does not imply B. The BigIntegers can have a mag length up to MAX_MAG_LENGTH, but MAX_MAG_LENGTH * Integer.SIZE > Integer.MAX_VALUE.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK.

But then your original expression

(((bitLength() - 1L) * exponent) >>> 5) + 1L > MAX_MAG_LENGTH

was a bit too restrictive as well, right?

Copy link
Contributor Author

@fabioromano1 fabioromano1 May 7, 2025

Choose a reason for hiding this comment

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

OK.

But then your original expression

(((bitLength() - 1L) * exponent) >>> 5) + 1L > MAX_MAG_LENGTH

was a bit too restrictive as well, right?

On the contrary, it was too loose, as it admitted a bit length equal to MAX_MAG_LENGTH * Integer.SIZE == 2^31 > Integer.MAX_VALUE.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, if B strictly implies A, then B is more restrictive (stronger).
Since B is equivalent to your original formulation, to me it means that it was more restrictive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean, if B strictly implies A, then B is more restrictive (stronger). Since B is equivalent to your original formulation, to me it means that it was more restrictive.

Exactly.

@rgiulietti
Copy link
Contributor

The large number algorithm loop in pow() might deserve an invariant in a comment.

Did you make sure that the existing tests also cover the unsignedLongPow() sufficiently well?

@rgiulietti
Copy link
Contributor

JMH benchmarks look good for the random exponents as well.

@fabioromano1
Copy link
Contributor Author

Did you make sure that the existing tests also cover the unsignedLongPow() sufficiently well?

@rgiulietti There are already the tests for BigInteger's pow, since the code of unsignedLongPow() is basically equivalent to that one in the current implementation.

Copy link
Contributor

@rgiulietti rgiulietti left a comment

Choose a reason for hiding this comment

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

Last nits.

I'll approve the PR in the next couple of days for last minute small changes from you.

@rgiulietti
Copy link
Contributor

rgiulietti commented May 8, 2025

Please update the 2nd copyright year in BigInteger.

Otherwise looks good.
As soon as you feel comfortable with your changes, let me know so that I can approve.

Thanks for yet another nice contribution!

@fabioromano1
Copy link
Contributor Author

@rgiulietti Could be useful this optimization for int bases whose result does not fit in a long?

@rgiulietti
Copy link
Contributor

@rgiulietti Could be useful this optimization for int bases whose result does not fit in a long?

There are endless optimizations and fast paths that one could conceive, but at some point we must decide to stop and turn our attention to other aspects of the platform. I think we have reached the turning point for this great PR.

Besides, the performance gains of yet another fast-path might be negligible in general usage, at the cost of another 50 lines or so of code that would probably only benefit very narrow use cases.

@fabioromano1
Copy link
Contributor Author

fabioromano1 commented May 8, 2025

Otherwise looks good. As soon as you feel comfortable with your changes, let me know so that I can approve.

@rgiulietti For me, now the code is ready.

@rgiulietti
Copy link
Contributor

Good.

Let's give another 24 hours for people around the globe to chime in for last minute comments.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 9, 2025
@fabioromano1
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label May 9, 2025
@openjdk
Copy link

openjdk bot commented May 9, 2025

@fabioromano1
Your change (at version 261dd31) is now ready to be sponsored by a Committer.

@rgiulietti
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented May 9, 2025

Going to push as commit 1c5eb37.
Since your change was applied there have been 229 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 9, 2025
@openjdk openjdk bot closed this May 9, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels May 9, 2025
@openjdk
Copy link

openjdk bot commented May 9, 2025

@rgiulietti @fabioromano1 Pushed as commit 1c5eb37.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

6 participants