RFR: 8285947: Avoid redundant HashMap.containsKey calls in ZoneName
Andrey Turbanov
aturbanov at openjdk.java.net
Fri Apr 29 21:25:43 UTC 2022
On Fri, 29 Apr 2022 06:31:22 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:
> `Map.containsKey` call is sometimes unnecessary, when it's known that Map doesn't contain `null` values.
> Instead we can just use Map.get and compare result with `null`.
> I found one of such place, where Map.containsKey calls could be eliminated - `java.time.format.ZoneName`.
> it gives a bit of performance for `toZid`.
>
>
> Benchmark Mode Cnt Score Error Units
> ZoneNamesBench.useExistingCountry avgt 5 10,738 ± 0,065 ns/op
> ZoneNamesBench.useExistingCountryOld avgt 5 13,679 ± 0,089 ns/op
>
>
> <details>
> <summary>Benchmark</summary>
>
>
> @BenchmarkMode(value = Mode.AverageTime)
> @OutputTimeUnit(TimeUnit.NANOSECONDS)
> @Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
> @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
> @Fork(1)
> @State(Scope.Benchmark)
> public class ZoneNamesBench {
>
> @Benchmark
> public String useExistingCountry() {
> return ZoneName.toZid("Africa/Tunis", Locale.ITALY);
> }
>
> @Benchmark
> public String useExistingCountryOld() {
> return ZoneName.toZidOld("Africa/Tunis", Locale.ITALY);
> }
> }
>
>
>
> public static String toZid(String zid, Locale locale) {
> String mzone = zidToMzone.get(zid);
> if (mzone == null) {
> String alias = aliases.get(zid);
> if (alias != null) {
> zid = alias;
> mzone = zidToMzone.get(zid);
> }
> }
> if (mzone != null) {
> Map<String, String> map = mzoneToZidL.get(mzone);
> if (map == null || ((zid = map.get(locale.getCountry())) == null)) {
> zid = mzoneToZid.get(mzone);
> }
> }
> return toZid(zid);
> }
>
> public static String toZid(String zid) {
> return aliases.getOrDefault(zid, zid);
> }
>
> public static String toZidOld(String zid, Locale locale) {
> String mzone = zidToMzone.get(zid);
> if (mzone == null && aliases.containsKey(zid)) {
> zid = aliases.get(zid);
> mzone = zidToMzone.get(zid);
> }
> if (mzone != null) {
> Map<String, String> map = mzoneToZidL.get(mzone);
> if (map != null && map.containsKey(locale.getCountry())) {
> zid = map.get(locale.getCountry());
> } else {
> zid = mzoneToZid.get(mzone);
> }
> }
> return toZidOld(zid);
> }
>
> public static String toZidOld(String zid) {
> if (aliases.containsKey(zid)) {
> return aliases.get(zid);
> }
> return zid;
> }
>
> </details>
Hm, build of this branch fails with Crash on `Optimizing the exploded image` on one of my machines.
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000000000000, pid=57300, tid=10552
[hs_err_pid49380.log](https://github.com/openjdk/jdk/files/8594756/hs_err_pid49380.log)
[hs_err_pid57300.log](https://github.com/openjdk/jdk/files/8594758/hs_err_pid57300.log)
-------------
PR: https://git.openjdk.java.net/jdk/pull/8463
More information about the core-libs-dev
mailing list