-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache ServiceLoader-calls without result
Iterating over the ServiceLoader results in class- and resource-loading, which becomes expensive if done extensively. A common pattern used (in e.g. "com.sun.xml.ws.api.pipe.TransportTubeFactory::create") is to search first for multiple factory-implementations before falling back to a "default" factory / implementation: public Type exampleFunc(...) { for (_ : ServiceFinder.find(FactoryType1.class) { return if FactoryType1-impl found } for (_ : ServiceFinder.find(FactoryType2.class) { return if FactoryType2-impl found } return DEFAULT_FACTORY.createType(..); } If there are no other implementations present besides the default-fallback-implementation, then each call to a method with this structure starts searching (again) for all non-default implementations - only to not find any implementing classes and finally falling back to the default-implementation. Invoking such method-structures often, results in multiple unnecessary ServiceLoader-calls, because if the corresponding service-class and classloader are identical to a previous call and for this previous call the classloader was not able to determine the service-implementation, then it still won't be able to find it when retrying the ServiceLoader-call with the same parameters.
- Loading branch information
Showing
2 changed files
with
67 additions
and
11 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/util/ServiceClassLoaderCacheKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.sun.xml.ws.util; | ||
|
||
import java.util.Objects; | ||
|
||
public class ServiceClassLoaderCacheKey<T> { | ||
private final Class<T> service; | ||
private final ClassLoader loader; | ||
|
||
public ServiceClassLoaderCacheKey(Class<T> service, ClassLoader loader) { | ||
this.service = service; | ||
this.loader = loader; | ||
} | ||
|
||
public Class<T> getService() { | ||
return service; | ||
} | ||
|
||
public ClassLoader getLoader() { | ||
return loader; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof ServiceClassLoaderCacheKey)) return false; | ||
ServiceClassLoaderCacheKey<?> that = (ServiceClassLoaderCacheKey<?>) o; | ||
return Objects.equals(service, that.service) && Objects.equals(loader, that.loader); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(service, loader); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters