Skip to content

8359119: Change Charset to use StableValue #25727

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

minborg
Copy link
Contributor

@minborg minborg commented Jun 10, 2025

Fields and methods can better leverage the Stable Value API compared to using DCL and holder classes. There are also some fields that can be marked @Stable.

This PR passes tier1, tier2, and tier3 tests on multiple platforms.


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-8359119: Change Charset to use StableValue (Enhancement - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25727

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 10, 2025

👋 Welcome back pminborg! 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 Jun 10, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Jun 10, 2025

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

  • nio

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 nio nio-dev@openjdk.org label Jun 10, 2025
@minborg minborg marked this pull request as ready for review June 10, 2025 14:13
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 10, 2025
@mlbridge
Copy link

mlbridge bot commented Jun 10, 2025

Webrevs

@minborg minborg changed the title 8359119: There are several opportunities to use Stable Values in j.n.c.Charset 8359119: Change Charset to use StableValue Jun 10, 2025
@minborg
Copy link
Contributor Author

minborg commented Jun 10, 2025

Current startup performance on an M1 macOS using brr -b perfstartup-noop.

Base:

Wall clock:       29.5 ms/op
  :.cycles:       100702748 average cycles elapsed
  :.instructions: 196311919 average instructions retired
  :.maxrss:       39100416 bytes maximum resident set size
  :.taskclock:    23.0 ms/op

Patch:

Wall clock:       29.0 ms/op
  :.cycles:       101696210 average cycles elapsed
  :.instructions: 196761541 average instructions retired
  :.maxrss:       39067648 bytes maximum resident set size
  :.taskclock:    22.5 ms/op

static final ThreadTracker TRACKER = new ThreadTracker();
}
private static final Supplier<ThreadTracker> TRACKER = StableValue.supplier(
new Supplier<>() { public ThreadTracker get() { return new ThreadTracker(); }});
Copy link
Contributor

@AlanBateman AlanBateman Jun 10, 2025

Choose a reason for hiding this comment

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

ThreadTracker pre-dates ScopedValue. We could potentially use a ScopedValue<Boolean> IN_LOOKUP here, or StableValue<ScopedValue<Boolean>> if there are issues running the class initializer in early VM startup. Separate discussion but we don't need ThreadTracker now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, revert the changes here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's okay, it's just that the change is a reminder that all ThreadTracker usages can be replaced with a ScopedValue but may require a bit of work to allow using during early VM startup.

Copy link
Contributor

Choose a reason for hiding this comment

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

I chatted with Andrew Haley yesterday about allowing ScopedValue be used in early startup. Charset is initialized very early in initPhase1, when initializing the system properties, so easy to get recursive initialization issues that surface or NPE or other exceptions. There are several ways we can fix this and it means that TRACKER is not needed. I don't mind if we change it to use a StableValue now but I expect we will change it very soon to remove ThreadTracker and just replace with ScopedValue.

Copy link
Contributor

Choose a reason for hiding this comment

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

The issue with using ScopedValue early in startup is resolved so we can use private static final ScopedValue<Boolean> IN_LOOKUP = ScopedValue.newInstance(); and get rid of the use of ThreadTracker from this class.

int n = 0;
ServiceLoader<CharsetProvider> sl =
private static final Supplier<List<CharsetProvider>> EXTENDED_PROVIDERS = StableValue.supplier(
new Supplier<>() { public List<CharsetProvider> get() { return extendedProviders0(); }});
Copy link
Contributor

Choose a reason for hiding this comment

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

This one looks okay.

private static Charset defaultCharset0() {
return Objects.requireNonNullElse(
standardProvider.charsetForName(StaticProperty.fileEncoding()),
sun.nio.cs.UTF_8.INSTANCE);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would more readable to keep the if-then-else rather than using requireNonNullElse here.

private final String name; // tickles a bug in oldjavac
@Stable
Copy link
Contributor

Choose a reason for hiding this comment

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

If we touching these then we can remove the "tickles a bug in oldjavac" comment as I think that goes back to before JDK 5.

@AlanBateman
Copy link
Contributor

Overall I think this is okay but will need to run startup benchmarks due to this class being used in early startup.

@wenshao
Copy link
Contributor

wenshao commented Jul 20, 2025

After this proposal, the startup will load 9 more classes

  • Java Code
public class Startup {
    public static void main(String[] args) throws Exception {
        Thread.sleep(1000);
    }
}
./build/macosx-aarch64-server-release/images/jdk/bin/java -verbose:class Startup

By comparing the output before and after this proposal, we can see that 9 more classes are loaded, as follows:

[info][class,load] java.nio.charset.Charset$2
[info][class,load] java.lang.StableValue
[info][class,load] java.util.Objects
[info][class,load] jdk.internal.lang.stable.StableSupplier
[info][class,load] jdk.internal.lang.stable.StableValueImpl
[info][class,load] java.nio.charset.Charset$3
[info][class,load] java.nio.charset.Charset$4
[info][class,load] java.nio.charset.Charset$5
[info][class,load] jdk.internal.ref.Cleaner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nio nio-dev@openjdk.org rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants