Re: RFR: 8273140: Replace usages of Enum.class.getEnumConstants() with Enum.values() where possible [v4]
On Tue, 31 Aug 2021 20:04:23 GMT, Сергей Цыпанов wrote: >> Just a very tiny clean-up. >> >> There are some places in JDK code base where we call >> `Enum.class.getEnumConstants()` to get all the values of the referenced >> `enum`. This is excessive, less-readable and slower than just calling >> `Enum.values()` as in `getEnumConstants()` we have volatile access: >> >> public T[] getEnumConstants() { >> T[] values = getEnumConstantsShared(); >> return (values != null) ? values.clone() : null; >> } >> >> private transient volatile T[] enumConstants; >> >> T[] getEnumConstantsShared() { >> T[] constants = enumConstants; >> if (constants == null) { /* ... */ } >> return constants; >> } >> >> Calling values() method is slightly faster: >> >> @BenchmarkMode(Mode.AverageTime) >> @OutputTimeUnit(TimeUnit.NANOSECONDS) >> @Fork(jvmArgsAppend = {"-Xms2g", "-Xmx2g"}) >> public class EnumBenchmark { >> >> @Benchmark >> public Enum[] values() { >> return Enum.values(); >> } >> >> @Benchmark >> public Enum[] getEnumConstants() { >> return Enum.class.getEnumConstants(); >> } >> >> private enum Enum { >> A, >> B >> } >> } >> >> >> BenchmarkMode Cnt >> Score Error Units >> EnumBenchmark.getEnumConstants avgt 15 >> 6,265 ± 0,051 ns/op >> EnumBenchmark.getEnumConstants:·gc.alloc.rateavgt 15 >> 2434,075 ± 19,568 MB/sec >> EnumBenchmark.getEnumConstants:·gc.alloc.rate.norm avgt 15 >> 24,002 ± 0,001B/op >> EnumBenchmark.getEnumConstants:·gc.churn.G1_Eden_Space avgt 15 >> 2433,709 ± 70,216 MB/sec >> EnumBenchmark.getEnumConstants:·gc.churn.G1_Eden_Space.norm avgt 15 >> 23,998 ± 0,659B/op >> EnumBenchmark.getEnumConstants:·gc.churn.G1_Survivor_Space avgt 15 >> 0,009 ± 0,003 MB/sec >> EnumBenchmark.getEnumConstants:·gc.churn.G1_Survivor_Space.norm avgt 15 >> ≈ 10⁻⁴ B/op >> EnumBenchmark.getEnumConstants:·gc.count avgt 15 >> 210,000counts >> EnumBenchmark.getEnumConstants:·gc.time avgt 15 >> 119,000ms >> >> EnumBenchmark.values avgt 15 >> 4,164 ± 0,134 ns/op >> EnumBenchmark.values:·gc.alloc.rate avgt 15 >> 3665,341 ± 120,721 MB/sec >> EnumBenchmark.values:·gc.alloc.rate.norm avgt 15 >> 24,002 ± 0,001B/op >> EnumBenchmark.values:·gc.churn.G1_Eden_Space avgt 15 >> 3660,512 ± 137,250 MB/sec >> EnumBenchmark.values:·gc.churn.G1_Eden_Space.normavgt 15 >> 23,972 ± 0,529B/op >> EnumBenchmark.values:·gc.churn.G1_Survivor_Space avgt 15 >> 0,017 ± 0,003 MB/sec >> EnumBenchmark.values:·gc.churn.G1_Survivor_Space.normavgt 15 >> ≈ 10⁻⁴ B/op >> EnumBenchmark.values:·gc.count avgt 15 >> 262,000counts >> EnumBenchmark.values:·gc.timeavgt 15 >> 155,000ms > > Сергей Цыпанов has updated the pull request incrementally with one additional > commit since the last revision: > > 8273140: Fix copyright year Lgtm. - Marked as reviewed by tschatzl (Reviewer). PR: https://git.openjdk.java.net/jdk/pull/5303
Re: RFR: 8273140: Replace usages of Enum.class.getEnumConstants() with Enum.values() where possible [v4]
> Just a very tiny clean-up. > > There are some places in JDK code base where we call > `Enum.class.getEnumConstants()` to get all the values of the referenced > `enum`. This is excessive, less-readable and slower than just calling > `Enum.values()` as in `getEnumConstants()` we have volatile access: > > public T[] getEnumConstants() { > T[] values = getEnumConstantsShared(); > return (values != null) ? values.clone() : null; > } > > private transient volatile T[] enumConstants; > > T[] getEnumConstantsShared() { > T[] constants = enumConstants; > if (constants == null) { /* ... */ } > return constants; > } > > Calling values() method is slightly faster: > > @BenchmarkMode(Mode.AverageTime) > @OutputTimeUnit(TimeUnit.NANOSECONDS) > @Fork(jvmArgsAppend = {"-Xms2g", "-Xmx2g"}) > public class EnumBenchmark { > > @Benchmark > public Enum[] values() { > return Enum.values(); > } > > @Benchmark > public Enum[] getEnumConstants() { > return Enum.class.getEnumConstants(); > } > > private enum Enum { > A, > B > } > } > > > BenchmarkMode Cnt > Score Error Units > EnumBenchmark.getEnumConstants avgt 15 > 6,265 ± 0,051 ns/op > EnumBenchmark.getEnumConstants:·gc.alloc.rateavgt 15 > 2434,075 ± 19,568 MB/sec > EnumBenchmark.getEnumConstants:·gc.alloc.rate.norm avgt 15 > 24,002 ± 0,001B/op > EnumBenchmark.getEnumConstants:·gc.churn.G1_Eden_Space avgt 15 > 2433,709 ± 70,216 MB/sec > EnumBenchmark.getEnumConstants:·gc.churn.G1_Eden_Space.norm avgt 15 > 23,998 ± 0,659B/op > EnumBenchmark.getEnumConstants:·gc.churn.G1_Survivor_Space avgt 15 > 0,009 ± 0,003 MB/sec > EnumBenchmark.getEnumConstants:·gc.churn.G1_Survivor_Space.norm avgt 15 > ≈ 10⁻⁴ B/op > EnumBenchmark.getEnumConstants:·gc.count avgt 15 > 210,000counts > EnumBenchmark.getEnumConstants:·gc.time avgt 15 > 119,000ms > > EnumBenchmark.values avgt 15 > 4,164 ± 0,134 ns/op > EnumBenchmark.values:·gc.alloc.rate avgt 15 > 3665,341 ± 120,721 MB/sec > EnumBenchmark.values:·gc.alloc.rate.norm avgt 15 > 24,002 ± 0,001B/op > EnumBenchmark.values:·gc.churn.G1_Eden_Space avgt 15 > 3660,512 ± 137,250 MB/sec > EnumBenchmark.values:·gc.churn.G1_Eden_Space.normavgt 15 > 23,972 ± 0,529B/op > EnumBenchmark.values:·gc.churn.G1_Survivor_Space avgt 15 > 0,017 ± 0,003 MB/sec > EnumBenchmark.values:·gc.churn.G1_Survivor_Space.normavgt 15 > ≈ 10⁻⁴ B/op > EnumBenchmark.values:·gc.count avgt 15 > 262,000counts > EnumBenchmark.values:·gc.timeavgt 15 > 155,000ms Сергей Цыпанов has updated the pull request incrementally with one additional commit since the last revision: 8273140: Fix copyright year - Changes: - all: https://git.openjdk.java.net/jdk/pull/5303/files - new: https://git.openjdk.java.net/jdk/pull/5303/files/171ecd4e..a5f58fe2 Webrevs: - full: https://webrevs.openjdk.java.net/?repo=jdk=5303=03 - incr: https://webrevs.openjdk.java.net/?repo=jdk=5303=02-03 Stats: 2 lines in 2 files changed: 0 ins; 0 del; 2 mod Patch: https://git.openjdk.java.net/jdk/pull/5303.diff Fetch: git fetch https://git.openjdk.java.net/jdk pull/5303/head:pull/5303 PR: https://git.openjdk.java.net/jdk/pull/5303