Skip to content

8374308: ImageBufferCache has no effect and can be removed#29043

Closed
david-beaumont wants to merge 4 commits intoopenjdk:masterfrom
david-beaumont:jdk_8374308_removed
Closed

8374308: ImageBufferCache has no effect and can be removed#29043
david-beaumont wants to merge 4 commits intoopenjdk:masterfrom
david-beaumont:jdk_8374308_removed

Conversation

@david-beaumont
Copy link
Contributor

@david-beaumont david-beaumont commented Jan 5, 2026

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

  • 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-8374308: ImageBufferCache has no effect and can be removed (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 29043

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 5, 2026

👋 Welcome back david-beaumont! 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 Jan 5, 2026

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

8374308: ImageBufferCache has no effect and can be removed

Reviewed-by: alanb, rriggs

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 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 (@AlanBateman, @RogerRiggs) 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 core-libs core-libs-dev@openjdk.org label Jan 5, 2026
@openjdk
Copy link

openjdk bot commented Jan 5, 2026

@david-beaumont 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 rfr Pull request is ready for review label Jan 5, 2026
@mlbridge
Copy link

mlbridge bot commented Jan 5, 2026

Webrevs

}
}

private static ByteBuffer allocateBuffer(long size) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only bit of code from ImageBufferCache that was being used in existing code.

return buffer != null ? getBufferBytes(buffer) : null;
}

/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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:

  1. We want people to know they don't need to worry about "releasing" it, but also
  2. 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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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));
Copy link
Contributor

@AlanBateman AlanBateman Jan 5, 2026

Choose a reason for hiding this comment

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

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.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

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");
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it can happen but I assume IAE would be more appropriate here as size is not an index.

Copy link
Contributor

Choose a reason for hiding this comment

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

Only needs to check for the Integer.MAX_VALUE in any case, as ByteBuffer::allocateDirect throws on size < 0. It also uses IAE

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair points. This is just moved code, but IOOBE is clearly not the right choice here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, taking a more holistic view, the size check is already done by the caller, so there's some nice simplification possible here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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 ... }

@RogerRiggs
Copy link
Contributor

Please update the issue with the change in scope of the change.

@AlanBateman
Copy link
Contributor

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.

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);
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 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

@david-beaumont david-beaumont changed the title 8374308: ImageBufferCache does not work as intended 8374308: ImageBufferCache has no effect and can be removed Jan 6, 2026
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 6, 2026
Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

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

looks good.

@david-beaumont
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jan 6, 2026
@openjdk
Copy link

openjdk bot commented Jan 6, 2026

@david-beaumont
Your change (at version 7d54c8e) is now ready to be sponsored by a Committer.

@RogerRiggs
Copy link
Contributor

/sponsor

@openjdk
Copy link

openjdk bot commented Jan 6, 2026

Going to push as commit 7c979c1.
Since your change was applied there have been 23 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 Jan 6, 2026
@openjdk openjdk bot closed this Jan 6, 2026
@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 Jan 6, 2026
@openjdk
Copy link

openjdk bot commented Jan 6, 2026

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

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.

4 participants