8374308: ImageBufferCache has no effect and can be removed#29043
8374308: ImageBufferCache has no effect and can be removed#29043david-beaumont wants to merge 4 commits intoopenjdk:masterfrom
Conversation
|
👋 Welcome back david-beaumont! A progress list of the required criteria for merging this PR into |
|
@david-beaumont 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: 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 22 new commits pushed to the
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 (@AlanBateman, @RogerRiggs) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
|
@david-beaumont The following label will be automatically applied to this pull request:
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. |
Webrevs
|
| } | ||
| } | ||
|
|
||
| private static ByteBuffer allocateBuffer(long size) { |
There was a problem hiding this comment.
The only bit of code from ImageBufferCache that was being used in existing code.
| return buffer != null ? getBufferBytes(buffer) : null; | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
Seemed worth stressing that this is a new buffer allocation (since there's no "release" option to callers anymore, it's a usage/lifetime change).
| * Returns the content of a resource node in a possibly cached byte buffer. | ||
| * Callers of this method must call {@link #releaseByteBuffer(ByteBuffer)} | ||
| * when they are finished with it. | ||
| * Returns the content of a resource node in a newly allocated byte buffer. |
There was a problem hiding this comment.
While the "ByteBuffer" instance is newly allocated, it's almost always a cheap slice of the memory mapped file. I'm not sure what we want as the best wording here, since:
- We want people to know they don't need to worry about "releasing" it, but also
- we don't want people to think it's using a newly allocated/copied buffer.
Happy to take suggestion on better wording here and for "getResourceBuffer" above.
| .map(reader::getResourceBuffer); | ||
| } | ||
|
|
||
| @Override |
There was a problem hiding this comment.
Can be removed since ModuleReader has a default method with the null check in it.
| if (size < 0 || Integer.MAX_VALUE < size) { | ||
| throw new IndexOutOfBoundsException("size"); | ||
| } | ||
| ByteBuffer result = ByteBuffer.allocateDirect((int) ((size + 0xFFF) & ~0xFFF)); |
There was a problem hiding this comment.
Can you remind me why it allocates multiples of 4k?
Also, would be useful to know if using ByteByte.allocate (to allocate a heap buffer) would be okay here.
There was a problem hiding this comment.
I can't, I've never seen this code before. No comments, no tests for why 4k is a good size. Probably just a "page size" heuristic. I'm just removing dead code and moving what's not dead out of the class to be deleted.
Changing details beyond that should probably be a different PR.
There was a problem hiding this comment.
Actually, thinking about this more, it would have the benefit (in the situation where caching happens) of making buckets more likely to be reususeable, so if we're willing to assume that, and since the cache is going away, we could justify dropping the rounding and just allocating the exact size.
There was a problem hiding this comment.
I can't see any reason to round up now so I think we should remove it. I don't mind if its this PR or a separate PR.
I'm interested to know if this can be changed to use allocate as it doesn't need a direct buffer here. FileChannel.read uses the thread-local buffer cache when called with a heap-buffer.
|
|
||
| private static ByteBuffer allocateBuffer(long size) { | ||
| if (size < 0 || Integer.MAX_VALUE < size) { | ||
| throw new IndexOutOfBoundsException("size"); |
There was a problem hiding this comment.
I don't think it can happen but I assume IAE would be more appropriate here as size is not an index.
There was a problem hiding this comment.
Only needs to check for the Integer.MAX_VALUE in any case, as ByteBuffer::allocateDirect throws on size < 0. It also uses IAE
There was a problem hiding this comment.
Fair points. This is just moved code, but IOOBE is clearly not the right choice here.
There was a problem hiding this comment.
In fact, taking a more holistic view, the size check is already done by the caller, so there's some nice simplification possible here.
There was a problem hiding this comment.
You do need a negative check when using long because of underflow.
If longValue is 0xFFFFFFFF00000001 (negative, invalid) then '(int) longValue' is 1 (positive, valid).
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
Or you could use a different style of check:
int intValue = (int) longValue;
if (intValue != longValue) { throw ... }
|
Please update the issue with the change in scope of the change. |
I think we should leave the API docs as is as it allows for a ModuleReader to use off-heap memory in the future. |
| } | ||
|
|
||
| ByteBuffer buffer = allocateBuffer(size); | ||
| ByteBuffer buffer = ByteBuffer.allocate(checkedSize); |
There was a problem hiding this comment.
I changed this to just "allocate" (not "allocateDirect") after carefully reading up on direct buffers.
It's just possible this might have an observable effect (positive or negative) on either performance or memory pressure, so I'm happy to change it back if people are worried.
It's the only bit of this change that I think might not be a guaranteed no-op.
There was a problem hiding this comment.
It avoids allocating direct memory and leaving it to GC reference processing to free, so I think better than just removing the caching.
It should have no impact on usage at runtime, at least not the main stream platforms that are 64-bit and thus memory map the jimage file. On 64-bit, the built-in class loaders and other usages at runtime use slices of the mapping rather rather than allocate.
I think ARM is the only 32-bit port that is still maintained to some degree. 32-bit, and jrtfs when targeting a different run-time image, and the only cases that will use file I/O. I would expect at least some tests for javac -system should exercise this code, and we should have a few jimage tests that run with -Djdk.image.map.all=false to exercise this code (maybe we need more). If you want, you could increase confidence by testing with TEST_OPTS_JAVA_OPTIONS=-Djdk.image.map.all=false. Also since we are early in JDK 27 it means there is time for course correction in case any creepy crawlies come out of the woodwork.
| } | ||
| } | ||
|
|
||
| private static ByteBuffer allocateBuffer(long size) { |
There was a problem hiding this comment.
Since the size check already happened in the caller, this check is never triggered, and if the rounding is removed, the limit() call isn't needed, so this method collapses into a single allocate method call which can be inlined.
|
/integrate |
|
@david-beaumont |
|
/sponsor |
|
Going to push as commit 7c979c1.
Your commit was automatically rebased without conflicts. |
|
@RogerRiggs @david-beaumont Pushed as commit 7c979c1. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Remove ineffective/unused ImageBufferCache class, and simplify callers / remove dead code.
I removed the release methods in the internal classes, but the public ModuleReader API method is still there (the override can go away though since the default implementation also tests for non-null, so removing the override has no risk).
I suspect there are no implementations of ModuleReader that implement release semantics after this change, so perhaps we could relax the documentation around it? Thoughts welcome.
Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/29043/head:pull/29043$ git checkout pull/29043Update a local copy of the PR:
$ git checkout pull/29043$ git pull https://git.openjdk.org/jdk.git pull/29043/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 29043View PR using the GUI difftool:
$ git pr show -t 29043Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/29043.diff
Using Webrev
Link to Webrev Comment