RFR: 8302603: Use Set.of in java.nio.charset.Charset

Alan Bateman alanb at openjdk.org
Thu Feb 16 08:07:31 UTC 2023


On Wed, 15 Feb 2023 09:42:53 GMT, Glavo <duke at openjdk.org> wrote:

> A small patch, using `Set.of` instead of `Collections.unmodifiableSet()` of `Charset#aliasSet`.
> 
> The alias should not be null, and the aliasSet is an immutable copy, so it is safe to use `Set.of`.
> 
> I have investigated all charsets, most of charsets have no more than 4 aliases, and the maximum is only 17.
> 
> In this case, the performance difference between immutable sets and HashSet can be ignored, and the memory footprint of immutable sets is smaller.
> 
> I need someone to help me open an Issue on JBS. Thank you very much.

src/java.base/share/classes/java/nio/charset/Charset.java line 717:

> 715:     public final Set<String> aliases() {
> 716:         if (aliasSet == null)
> 717:             aliasSet = Set.of(aliases);

Given that this is racy then it might be better to use a local and avoid someone reading this code puzzling over whether it can return null, e.g.

        Set<String> aliasSet = this.aliasSet;
        if (aliasSet == null) {
            this.aliasSet = aliasSet = Set.of(aliases);
        }

You can also drop explicitly initialising it null at line 670.

-------------

PR: https://git.openjdk.org/jdk/pull/12570


More information about the nio-dev mailing list