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

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

Commit messages:
 - [PATCH] Avoid redundant HashMap.containsKey calls in ZoneName
 - [PATCH] Avoid redundant HashMap.containsKey calls in ZoneName

Changes: https://git.openjdk.java.net/jdk/pull/8463/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=8463&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8285947
  Stats: 14 lines in 1 file changed: 3 ins; 5 del; 6 mod
  Patch: https://git.openjdk.java.net/jdk/pull/8463.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/8463/head:pull/8463

PR: https://git.openjdk.java.net/jdk/pull/8463

Reply via email to