RFR: 8351556: Optimize Location.locationFor/isModuleOrientedLocation [v2]

Chen Liang liach at openjdk.org
Mon Mar 10 19:39:00 UTC 2025


On Mon, 10 Mar 2025 18:34:34 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> Leyden's JavacBenchApp benchmarks shows ~1% of time of the very first iteration is spent doing `Pattern.compile` in `Location.isModuleOrientedLocation`. Additionally, `Pattern.compile` becomes hot and requires JIT compilation, which eats CPU time. We can do better.
>> 
>> This improvement naturally spills over to `Location.locationFor`.
>> 
>> Things we do:
>>  1. Avoid `putIfAbsent` on fast-path.
>>  2. Lazily cache `Pattern` and reuse for all related paths.
>>  3. Capture output/module flags ahead of time, instead of recomputing them.
>>  4. (for interpreter/startup benefit) Use direct class type instead of interface.
>> 
>> There is also a little matching bug ([JDK-8351561](https://bugs.openjdk.org/browse/JDK-8351561)), which this performance improvement would retain.
>> 
>> Additional testing:
>>  - [x] New regression test still works
>>  - [x] New benchmark shows improvements
>>  - [x] Linux x86_64 server fastdebug/release, `langtools_all`
>
> Aleksey Shipilev has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Also direct properties test
>  - Touchups

src/java.compiler/share/classes/javax/tools/StandardLocation.java line 166:

> 164:             boolean isOutputLocation = name.endsWith("_OUTPUT");
> 165:             boolean isModuleOrientedLocation = computeIsModuleOrientedLocation(name);
> 166:             newLoc = new Location() {

So something like:

record CustomLocation(String name, boolean isOutputLocation, boolean isModuleOrientedLocation) implements Location {
    static final Pattern MODULE_WORD_PATTERN = Pattern.compile("\\bMODULE\\b");
    static boolean computeIsModuleOrientedLocation(String name) {
        return MODULE_WORD_PATTERN.matcher(name).matches();
    }
    @Override public String getName() { return name; }
}
boolean isOutputLocation = name.endsWith("_OUTPUT");
boolean isModuleOrientedLocation = CustomLocation.computeIsModuleOrientedLocation(name);
return new CustomLocation(name, isOutputLocation, isModuleOrientedLocation);

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

PR Review Comment: https://git.openjdk.org/jdk/pull/23973#discussion_r1987907251


More information about the compiler-dev mailing list