Minor performance improvement to java.lang.ModuleLayer.findModule(String name)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Minor performance improvement to java.lang.ModuleLayer.findModule(String name)

Andrej Golovnin
Hi all,

when we try to find a module which is not in the current layer, then
we access the map 'nameToModule' in the parent layers two times in the
lines 849-850:

846        return layers()
847                   .skip(1)  // skip this layer
848                   .map(l -> l.nameToModule)
849                   .filter(map -> map.containsKey(name))  // <-- first access
850                   .map(map -> map.get(name))                // <--
second access
851                   .findAny();

This can be improved when we map to the module in the line 848 and
then filter for non-null values:

846        return layers()
847                   .skip(1)  // skip this layer
848                   .map(l -> l.nameToModule.get(name))
849                   .filter(Objects::nonNull)
850                   .findAny();

The suggested change is attached as diff.

Best regards,
Andrej Golovnin

ModuleLayer.diff (844 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Minor performance improvement to java.lang.ModuleLayer.findModule(String name)

Claes Redestad
Hi Andrej,

this seems like a very reasonable improvement. I can help sponsor it.

What I think is really needed in this area is some microbenchmarks that
sets up
multiple and more complex (but somewhat realistic) Layers of modules and
test various
queries, so that we can profile and get a tab on where improvements
matter most.

Thanks!

/Claes

On 2017-12-07 11:00, Andrej Golovnin wrote:

> Hi all,
>
> when we try to find a module which is not in the current layer, then
> we access the map 'nameToModule' in the parent layers two times in the
> lines 849-850:
>
> 846        return layers()
> 847                   .skip(1)  // skip this layer
> 848                   .map(l -> l.nameToModule)
> 849                   .filter(map -> map.containsKey(name))  // <-- first access
> 850                   .map(map -> map.get(name))                // <--
> second access
> 851                   .findAny();
>
> This can be improved when we map to the module in the line 848 and
> then filter for non-null values:
>
> 846        return layers()
> 847                   .skip(1)  // skip this layer
> 848                   .map(l -> l.nameToModule.get(name))
> 849                   .filter(Objects::nonNull)
> 850                   .findAny();
>
> The suggested change is attached as diff.
>
> Best regards,
> Andrej Golovnin

Reply | Threaded
Open this post in threaded view
|

Re: Minor performance improvement to java.lang.ModuleLayer.findModule(String name)

Alan Bateman
In reply to this post by Andrej Golovnin
On 07/12/2017 10:00, Andrej Golovnin wrote:
> Hi all,
>
> when we try to find a module which is not in the current layer, then
> we access the map 'nameToModule' in the parent layers two times in the
> lines 849-850:
>
The change looks okay (I'm pretty sure it was just an oversight as this
code changed several times during JDK 9).

-Alan
Reply | Threaded
Open this post in threaded view
|

Re: Minor performance improvement to java.lang.ModuleLayer.findModule(String name)

Andrej Golovnin
In reply to this post by Claes Redestad
Hi Claes,

> this seems like a very reasonable improvement. I can help sponsor it.

Thanks a lot!

> What I think is really needed in this area is some microbenchmarks that sets up
> multiple and more complex (but somewhat realistic) Layers of modules and test various
> queries, so that we can profile and get a tab on where improvements matter most.

Yes, that would be nice.

Best regards,
Andrej Golovnin