Skip to content
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

8281631: HashMap copy constructor and putAll can over-allocate table #7431

Conversation

XenoAmess
Copy link
Contributor

@XenoAmess XenoAmess commented Feb 10, 2022


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8281631: HashMap copy constructor and putAll can over-allocate table

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/7431/head:pull/7431
$ git checkout pull/7431

Update a local copy of the PR:
$ git checkout pull/7431
$ git pull https://git.openjdk.java.net/jdk pull/7431/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 7431

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/7431.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 10, 2022

👋 Welcome back XenoAmess! 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 Feb 10, 2022

@XenoAmess 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.

@XenoAmess
Copy link
Contributor Author

According to the discussion at mailing list, we decide to try only change the calculation inside HashMap and WeakHashMap, and see what would happen.
The next step is fixing all such size/0.75+1 in jdk (expected several hundreds places...)

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Feb 10, 2022
@XenoAmess
Copy link
Contributor Author

I investigated most of the usages.
They just give a size, and get a capacity, even not change the 0.75
So maybe we can use some int calculation to replace the 0.75, thus replace Math.ceil for such situations.

@XenoAmess XenoAmess changed the title 9072610: HashMap.putAll can cause redundant space waste 8281631: HashMap.putAll can cause redundant space waste Feb 11, 2022
@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 11, 2022
@stefan-zobel
Copy link

I investigated most of the usages. They just give a size, and get a capacity, even not change the 0.75 So maybe we can use some int calculation to replace the 0.75, thus replace Math.ceil for such situations.

FWIW, (int) Math.ceil(expected / 0.75) and (int) ((expected * 4L + 2L) / 3L) would be equivalent.

@XenoAmess
Copy link
Contributor Author

XenoAmess commented Feb 11, 2022

I investigated most of the usages. They just give a size, and get a capacity, even not change the 0.75 So maybe we can use some int calculation to replace the 0.75, thus replace Math.ceil for such situations.

FWIW, (int) Math.ceil(expected / 0.75) and (int) ((expected * 4L + 2L) / 3L) would be equivalent.

Yes, and (int) ((expected * 4L + 2L) / 3L) actually equals (expected + (expected + 2) / 3) IMO, thus even avoid cast to long.

@XenoAmess XenoAmess force-pushed the 9072610_HashMap.putAll_can_cause_redundant_space_waste branch from b4098ac to f18fc5e Compare February 11, 2022 12:55
@XenoAmess XenoAmess force-pushed the 9072610_HashMap.putAll_can_cause_redundant_space_waste branch from f18fc5e to bb42df9 Compare February 11, 2022 13:01
@stuart-marks
Copy link
Member

FWIW, (int) Math.ceil(expected / 0.75) and (int) ((expected * 4L + 2L) / 3L) would be equivalent.

No, they are not equivalent. If expected exceeds a certain value around 1.6bn, then the intermediate result using the second expression will be greater than Integer.MAX_VALUE. Casting this to int can result in a negative number.

In the first expression, a double value that exceeds the range of int will saturate at Integer.MAX_VALUE.

jshell> (int) ((1700000000 * 4L + 2L) / 3L)
$24 ==> -2028300629

jshell> (int) Math.ceil(1700000000 / 0.75)
$25 ==> 2147483647

Let's stick with the Math.ceil expression please.

@stuart-marks
Copy link
Member

(It's too late now, but I'd suggest avoiding force-pushing changes into a branch that's opened in a PR. Doing so confuses the comment history, as the comments refer to code that is no longer present.)

@XenoAmess
Copy link
Contributor Author

FWIW, (int) Math.ceil(expected / 0.75) and (int) ((expected * 4L + 2L) / 3L) would be equivalent.

No, they are not equivalent. If expected exceeds a certain value around 1.6bn, then the intermediate result using the second expression will be greater than Integer.MAX_VALUE. Casting this to int can result in a negative number.

FWIW, (int) Math.ceil(expected / 0.75) and (int) ((expected * 4L + 2L) / 3L) would be equivalent.

that is exactly why we added this check when it can reach such situation.

        if (size > (int) (Integer.MAX_VALUE * 0.75)) {
            return Integer.MAX_VALUE;
        }

@XenoAmess
Copy link
Contributor Author

XenoAmess commented Feb 11, 2022

(It's too late now, but I'd suggest avoiding force-pushing changes into a branch that's opened in a PR. Doing so confuses the comment history, as the comments refer to code that is no longer present.)

@stuart-marks
got it.

@XenoAmess
Copy link
Contributor Author

XenoAmess commented Feb 11, 2022

Let's stick with the Math.ceil expression please.

@stuart-marks
I'm afraid Math.ceil is much too time costing, but fine if you want.
changes pushed.

@stuart-marks
Copy link
Member

OK. The changes to HashMap and WeakHashMap look like they're on the right track. The changes to j.l.Class and the EnumConstantDirectory test don't belong here -- these are uses of HashMap. This bug and fix should focus on HashMap itself, to ensure that the cases in question allocate a table of the right size.

Are there any other maps that have this computation besides HashMap and WeakHashMap?

There should be a regression test for this. It's probably sufficient to base this on your original test program, which puts 12 entries into a HashMap using a variety of techniques. It should assert that the table size is 16 in all cases. Also, should there be a test case for WeakHashMap?

Also, I changed the summary of the bug report to be more precise. The PR title will need to be changed to correspond to it. Thanks.

@XenoAmess
Copy link
Contributor Author

XenoAmess commented Feb 15, 2022

The changes to j.l.Class and the EnumConstantDirectory test don't belong here -- these are uses of HashMap. This bug and fix should focus on HashMap itself, to ensure that the cases in question allocate a table of the right size.

A test will fail if not change codes there. Every pr should pass ci, so I have no choice.

Are there any other maps that have this computation besides HashMap and WeakHashMap?

Good question. Can I get a list of classes where I should check?(I guesd I shall start at LinkedHashMap and hash sets, but have no further ideas)

There should be a regression test for this. It's probably sufficient to base this on your original test program, which puts 12 entries into a HashMap using a variety of techniques. It should assert that the table size is 16 in all cases. Also, should there be a test case for WeakHashMap?

OK I will have a try... a hard part is how to read private field in class but I think I can find some clue in the existed tests.

Also, I changed the summary of the bug report to be more precise. The PR title will need to be changed to correspond to it. Thanks.

OK.

@stuart-marks
Copy link
Member

A test will fail if not change codes there. Every pr should pass ci, so I have no choice.

Hm, yes I recall in the preliminary email that there was some mention of a test. However, the test seemed to use the same (incorrect) calculation, so maybe the test needs to be fixed instead.

Good question. Can I get a list of classes where I should check?(I guesd I shall start at LinkedHashMap and hash sets, but have no further ideas)

Offhand, the HashMap/LinkedHashMap and the corresponding Set classes, and WeakHashMap, are the main places to look. IdentityHashMap and the Map.of() implementations use a different organization so are probably unrelated. ConcurrentHashMap is another obvious place; you might want to investigate there, but depending on the fix (if any) we might want to handle it separately. I'd search for "loadFactor" or "LOAD_FACTOR" and see if anything else turns up.

OK I will have a try... a hard part is how to read private field in class but I think I can find some clue in the existed tests.

There is an incantation in the test header to ensure that the java.base module is opened for reflection. Let me know if you have trouble with it.

@XenoAmess
Copy link
Contributor Author

A test will fail if not change codes there. Every pr should pass ci, so I have no choice.

Hm, yes I recall in the preliminary email that there was some mention of a test. However, the test seemed to use the same (incorrect) calculation, so maybe the test needs to be fixed instead.

These changes in those 2 class already the minimal changes for passing ci, as that test itself seems meaningful so I don't wanna shut it down.

Offhand, the HashMap/LinkedHashMap and the corresponding Set classes, and WeakHashMap, are the main places to look. IdentityHashMap and the Map.of() implementations use a different organization so are probably unrelated. ConcurrentHashMap is another obvious place; you might want to investigate there, but depending on the fix (if any) we might want to handle it separately. I'd search for "loadFactor" or "LOAD_FACTOR" and see if anything else turns up.

Thanks, I will start from there and see if can found something interesting.

@XenoAmess XenoAmess changed the title 8281631: HashMap.putAll can cause redundant space waste 8281631: HashMap copy constructor and putAll can over-allocate table Feb 15, 2022
@XenoAmess
Copy link
Contributor Author

IdentityHashMap...checked.
uninvolved.
ConcurrentHashMap...checked.
fixed.
HashMap and WeakHashMap...checked.
fixed.

@stuart-marks
Copy link
Member

stuart-marks commented Mar 11, 2022

@RogerRiggs Good point about Integer being value-based. We should probably use String instead.

@XenoAmess I don't think we need KeyStructure. It's probably sufficient to change the map types to Map<String, String> and then use String.valueOf(i) to generate strings to use as keys and values. (If we did need something like KeyStructure, it would be a perfect use of record classes.)

Also, I don't think tableSizeFor needs quite as many cases. It's sufficient to test the edge and boundary cases.

@XenoAmess
Copy link
Contributor Author

@XenoAmess I don't think we need KeyStructure. It's probably sufficient to change the map types to Map<String, String> and then use String.valueOf(i) to generate strings to use as keys and values. (If we did need something like KeyStructure, it would be a perfect use of record classes.)

Seems good.

Will do it at about 12 hours later, when I off work.

Also, I don't think tableSizeFor needs quite as many cases. It's sufficient to test the edge and boundary cases.

Well I just don't think there be any cost in adding them... after all they runs fast.

@XenoAmess
Copy link
Contributor Author

image
seems ci fails for network failure.

@stuart-marks I refine the tests using String to replace KeyStructure. Please have a look. thanks!

@stuart-marks
Copy link
Member

stuart-marks commented Mar 11, 2022

Regarding the number of test cases for tableSizeForCases and populatedCapacityCases... the issue is not necessarily with execution time (although if the test is slow it is a problem). The issues are more around: Is this testing anything useful, and does this make sense to the reader?

I think we can rely on the monotonicity of these functions. If populating a map both with 49 and with 96 mappings results in a table length of 128, we don't need to test that all the intermediate inputs also result in a table length of 128. Including all the intermediate inputs makes the source code more bulky and requires future readers/maintainers to hunt around in the long list of tests to figure out which ones are significant. Really, the only ones that are significant are the boundary cases, so just keep those. Adding more tests that aren't relevant actually does hurt, even if they execute quickly. So: please cut out all the extra test cases that aren't near the boundary cases.

I'm not sure what's going on with the build. The builds are in GitHub Actions and they aren't necessarily reliable, so I wouldn't worry about them too much. I'll run the final version through our internal build/test system before integration. (I've also done this previously, and the results were fine.)

@XenoAmess
Copy link
Contributor Author

@stuart-marks all the changes to the test you requested at last review comments are done. please have a look, thanks!

@XenoAmess
Copy link
Contributor Author

XenoAmess commented Mar 12, 2022

I think we can rely on the monotonicity of these functions. If populating a map both with 49 and with 96 mappings results in a table length of 128, we don't need to test that all the intermediate inputs also result in a table length of 128. Including all the intermediate inputs makes the source code more bulky and requires future readers/maintainers to hunt around in the long list of tests to figure out which ones are significant. Really, the only ones that are significant are the boundary cases, so just keep those. Adding more tests that aren't relevant actually does hurt, even if they execute quickly. So: please cut out all the extra test cases that aren't near the boundary cases.

what I worried is, the boundary this is based on the current table size calculating mechanic in HashMap.

If people change the mechanic in HashMap, then the boundary would change.

But well, this is a white box text for HashMap (and HashMap-like) classes after all, so maybe I'm just over overthinking too much.

Copy link
Member

@stuart-marks stuart-marks left a comment

Choose a reason for hiding this comment

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

what I worried is, the boundary this is based on the current table size calculating mechanic in HashMap.
If people change the mechanic in HashMap, then the boundary would change.
But well, this is a white box text for HashMap (and HashMap-like) classes after all, so maybe I'm just over overthinking too much.

Yes, the boundary conditions are sensitive to the exact calculation used for table sizing. If the calculation changes, the boundary results will most likely change, and the test will fail. But that's ok since this is a whitebox test.

In any case, changes look good, and I've run this through our internal build/test system and the results are good. Ready to integrate!

@openjdk
Copy link

openjdk bot commented Mar 16, 2022

@XenoAmess 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:

8281631: HashMap copy constructor and putAll can over-allocate table

Reviewed-by: smarks

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 348 new commits pushed to the master branch:

  • de4f04c: 8253495: CDS generates non-deterministic output
  • 4df24c5: 8283230: Improve @jls usage in ElementType
  • 27fe3d7: 8240756: [macos] SwingSet2:TableDemo:Printed Japanese characters were garbled
  • bacfaa3: 8282414: x86: Enhance the assembler to generate more compact instructions
  • 1465ea9: 8282355: compiler/arguments/TestCodeEntryAlignment.java failed "guarantee(sect->end() <= tend) failed: sanity"
  • ac06bdb: 8282507: Add a separate license file for hsdis
  • 32f8437: 8283075: Bad IllegalArgumentException message for out of range rank from ClassDesc.arrayType(int)
  • 12dca36: 8283189: Bad copyright header in UnsafeCopyMemory.java
  • 05a83e0: 8283124: Add constant for tau to Math and StrictMath
  • 671b6ef: 8283143: Use minimal-length literals to initialize PI and E constants
  • ... and 338 more: https://git.openjdk.java.net/jdk/compare/039313d65d47dc85cb8c91d3e1d2752d365f70f9...master

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 (@stuart-marks) 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 openjdk bot added the ready Pull request is ready to be integrated label Mar 16, 2022
@XenoAmess
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Mar 16, 2022
@openjdk
Copy link

openjdk bot commented Mar 16, 2022

@XenoAmess
Your change (at version a980bda) is now ready to be sponsored by a Committer.

@XenoAmess
Copy link
Contributor Author

@stuart-marks should it mean I shall first rebase all commits? or it would be automatically sqruashed?

@stuart-marks
Copy link
Member

No, you don't need to do any rebasing; when the change is integrated, all these commits will automatically be squashed into a single commit. If that can't be done, the bot will detect it and give a warning, which will probably include instructions. But basically you'd merge from the master branch and commit the merge after resolving any conflicts.

None of that needs to be done in this case, so I'll just go ahead and sponsor this.

@stuart-marks
Copy link
Member

/sponsor

@openjdk
Copy link

openjdk bot commented Mar 16, 2022

Going to push as commit 3e39304.
Since your change was applied there have been 351 commits pushed to the master branch:

  • 0cf291b: 8283234: Improve @jls usage in java.base
  • 9b8afce: 8283260: gcc is not supported on mac
  • 08cadb4: 8271195: Use largest available large page size smaller than LargePageSizeInBytes when available
  • de4f04c: 8253495: CDS generates non-deterministic output
  • 4df24c5: 8283230: Improve @jls usage in ElementType
  • 27fe3d7: 8240756: [macos] SwingSet2:TableDemo:Printed Japanese characters were garbled
  • bacfaa3: 8282414: x86: Enhance the assembler to generate more compact instructions
  • 1465ea9: 8282355: compiler/arguments/TestCodeEntryAlignment.java failed "guarantee(sect->end() <= tend) failed: sanity"
  • ac06bdb: 8282507: Add a separate license file for hsdis
  • 32f8437: 8283075: Bad IllegalArgumentException message for out of range rank from ClassDesc.arrayType(int)
  • ... and 341 more: https://git.openjdk.java.net/jdk/compare/039313d65d47dc85cb8c91d3e1d2752d365f70f9...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Mar 16, 2022
@openjdk openjdk bot closed this Mar 16, 2022
@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 Mar 16, 2022
@openjdk
Copy link

openjdk bot commented Mar 16, 2022

@stuart-marks @XenoAmess Pushed as commit 3e39304.

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

@XenoAmess
Copy link
Contributor Author

XenoAmess commented Mar 16, 2022

No, you don't need to do any rebasing; when the change is integrated, all these commits will automatically be squashed into a single commit. If that can't be done, the bot will detect it and give a warning, which will probably include instructions. But basically you'd merge from the master branch and commit the merge after resolving any conflicts.

None of that needs to be done in this case, so I'll just go ahead and sponsor this.

Thanks.
So the next step would be clean the usage of x / 0.75F+1F in codes.
Should I create one another report at https://bugreport.java.com/bugreport/ ?

@stuart-marks
Copy link
Member

There's already a bug for this: JDK-8186958. This includes creating a new API as well as fixing up a bunch of call sites. There's a partial list of call sites in java.base there. Go ahead and open a PR if you like.

I've added an initial suggestion at an API as a comment on that bug. There may be some bikeshedding about the API. If it gets too bad, a potential fallback position would be to create a JDK-internal utility method and call it instead, and sidestep the creation of a public API. (However, I do think we need a public API for this.) I'd avoid updating all the individual call sites with the actual computation (int) (Math.ceil(expectedSize/0.75)) because we might want to change it in the future.

@XenoAmess
Copy link
Contributor Author

There's already a bug for this: JDK-8186958. This includes creating a new API as well as fixing up a bunch of call sites. There's a partial list of call sites in java.base there. Go ahead and open a PR if you like.

got it.

I'd avoid updating all the individual call sites with the actual computation (int) (Math.ceil(expectedSize/0.75)) because we might want to change it in the future.

Yes, I still prefer my int calculation way...

But I guess a jmh for several implementations is necessary.

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
6 participants