JDK-8292670 : Class.getRecordComponents is surprisingly slow
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 19
  • Priority: P4
  • Status: New
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2022-08-19
  • Updated: 2022-08-19
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
I was doing some profiling and JFR showed that Class.getRecordComponents() is surprisingly slow. I made a quick jmh test for this. testRecordComponents calls getRecordComponents(). testRecordComponentsCached uses a HashMap to return a cached array of record components. I would expect the call to getRecordComponents on the Class object to be faster than a HashMap lookup - this is static information after all but it's orders of magnitude slower.

Benchmark                                Mode  Cnt          Score          Error  Units
MyBenchmark.testRecordComponents        thrpt    3    1552733.925 ±   152277.772  ops/s
MyBenchmark.testRecordComponentsCached  thrpt    3  432760682.251 ± 11713860.326  ops/s




Comments
Can we request the benchmark code, which I doubt may be incorrectly set up, from the submitter? Since Class::getRecordComponents should return a side-effect-free array, implying an array copy on every call, I would instead anticipate a hashmap lookup without array copy be significantly faster putting other factors aside. However, Class::getRecordComponents does request the VM to read the Record attribute of the class file and prepare all RecordComponent objects and the array on every call, which theoretically can be sped up by caching the RecordComponent array in the class reflection data.
19-08-2022

Moved to JDK for further investigations.
19-08-2022