-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Speedup Hibernate ORM's enhancement of large models
- Loading branch information
Showing
3 changed files
with
146 additions
and
33 deletions.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
...rc/main/java/io/quarkus/hibernate/orm/deployment/integration/QuarkusClassFileLocator.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,43 @@ | ||
package io.quarkus.hibernate.orm.deployment.integration; | ||
|
||
import java.io.IOException; | ||
|
||
import io.quarkus.deployment.util.IoUtil; | ||
import net.bytebuddy.dynamic.ClassFileLocator; | ||
|
||
/** | ||
* Custom implementation of a ClassFileLocator which will load resources | ||
* from the context classloader which is set at the time of the locate() | ||
* operation is being performed. | ||
* Using a regular ForClassLoader implementation would capture the currently | ||
* set ClassLoader and keep a reference to it, while we need it to look | ||
* for a fresh copy during the enhancement. | ||
* Additionally, we might be able to optimize how the resource is actually | ||
* being loaded as we control the ClassLoader implementations | ||
* (Such further optimisations are not implemented yet). | ||
*/ | ||
public final class QuarkusClassFileLocator implements ClassFileLocator { | ||
|
||
public static final QuarkusClassFileLocator INSTANCE = new QuarkusClassFileLocator(); | ||
|
||
private QuarkusClassFileLocator() { | ||
//do not invoke, use INSTANCE | ||
} | ||
|
||
@Override | ||
public Resolution locate(final String name) throws IOException { | ||
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
final byte[] bytes = IoUtil.readClassAsBytes(classLoader, name); | ||
if (bytes != null) { | ||
return new Resolution.Explicit(bytes); | ||
} else { | ||
return new Resolution.Illegal(name); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
//nothing to do | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
.../main/java/io/quarkus/hibernate/orm/deployment/integration/QuarkusEnhancementContext.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,29 @@ | ||
package io.quarkus.hibernate.orm.deployment.integration; | ||
|
||
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; | ||
import org.hibernate.bytecode.enhance.spi.UnloadedField; | ||
|
||
public final class QuarkusEnhancementContext extends DefaultEnhancementContext { | ||
|
||
public static final QuarkusEnhancementContext INSTANCE = new QuarkusEnhancementContext(); | ||
|
||
private QuarkusEnhancementContext() { | ||
//do not invoke, use INSTANCE | ||
} | ||
|
||
@Override | ||
public boolean doBiDirectionalAssociationManagement(final UnloadedField field) { | ||
//Don't enable automatic association management as it's often too surprising. | ||
//Also, there's several cases in which its semantics are of unspecified, | ||
//such as what should happen when dealing with ordered collections. | ||
return false; | ||
} | ||
|
||
@Override | ||
public ClassLoader getLoadingClassLoader() { | ||
//This shouldn't matter as we delegate resource location to QuarkusClassFileLocator; | ||
//make sure of this: | ||
throw new IllegalStateException("The Classloader of the EnhancementContext should not be used"); | ||
} | ||
|
||
} |