RFR: 8286366: (cs) Charset.put can use putIfAbsent instead of containsKey+put
Alan Bateman
alanb at openjdk.java.net
Sun May 15 08:39:14 UTC 2022
On Sat, 30 Apr 2022 08:27:10 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:
> The method `Charset.put(Iterator<Charset> i, Map<String,Charset> m)` could be improved by usage of `Map.putIfAbsent` instead of pair `containsKey`+`put`. This method is called only with `TreeMap` as an argument and `TreeMap` has an effective implementation of `putIfAbsent` which avoid traversal Tree twice.
> According to [JDK-8176894](https://bugs.openjdk.java.net/browse/JDK-8176894) usage of `TreeMap.putIfAbsent` could improve performance by 40%-100% compared with previous approach.
src/java.base/share/classes/java/nio/charset/Charset.java line 569:
> 567: while (i.hasNext()) {
> 568: Charset cs = i.next();
> 569: m.putIfAbsent(cs.name(), cs);
Yes, that would be a nicer. I've created JDK-8286366 for this. It could be changed to use for-each while you are there.
-------------
PR: https://git.openjdk.java.net/jdk/pull/8480
More information about the nio-dev
mailing list