-
Notifications
You must be signed in to change notification settings - Fork 3k
Speed up Flyway by preventing it from doing classpath scanning #9531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
121 changes: 121 additions & 0 deletions
121
extensions/flyway/deployment/src/main/java/io/quarkus/flyway/ScannerTransformer.java
This file contains hidden or 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,121 @@ | ||
| package io.quarkus.flyway; | ||
|
|
||
| import static org.objectweb.asm.Opcodes.ALOAD; | ||
| import static org.objectweb.asm.Opcodes.ASTORE; | ||
| import static org.objectweb.asm.Opcodes.DUP; | ||
| import static org.objectweb.asm.Opcodes.GETFIELD; | ||
| import static org.objectweb.asm.Opcodes.INVOKEINTERFACE; | ||
| import static org.objectweb.asm.Opcodes.INVOKESPECIAL; | ||
| import static org.objectweb.asm.Opcodes.NEW; | ||
| import static org.objectweb.asm.Opcodes.POP; | ||
| import static org.objectweb.asm.Opcodes.PUTFIELD; | ||
| import static org.objectweb.asm.Opcodes.RETURN; | ||
|
|
||
| import java.util.function.BiFunction; | ||
|
|
||
| import org.flywaydb.core.internal.scanner.Scanner; | ||
| import org.flywaydb.core.internal.scanner.classpath.ResourceAndClassScanner; | ||
| import org.objectweb.asm.ClassVisitor; | ||
| import org.objectweb.asm.MethodVisitor; | ||
|
|
||
| import io.quarkus.flyway.runtime.QuarkusPathLocationScanner; | ||
| import io.quarkus.gizmo.Gizmo; | ||
|
|
||
| /** | ||
| * Transforms {@link Scanner} in a way to take advantage of our build time knowledge | ||
| * This should be removed completely if https://github.com/flyway/flyway/issues/2822 | ||
| * is implemented | ||
| */ | ||
| class ScannerTransformer implements BiFunction<String, ClassVisitor, ClassVisitor> { | ||
|
|
||
| static final String FLYWAY_SCANNER_CLASS_NAME = Scanner.class.getName(); | ||
| private static final String FLYWAY_SCANNER_INTERNAL_CLASS_NAME = FLYWAY_SCANNER_CLASS_NAME.replace('.', '/'); | ||
|
|
||
| private static final String FLYWAY_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME = ResourceAndClassScanner.class.getName(); | ||
| private static final String FLYWAY_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME = FLYWAY_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME | ||
| .replace('.', '/'); | ||
|
|
||
| private static final String QUARKUS_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME = QuarkusPathLocationScanner.class.getName(); | ||
| private static final String QUARKUS_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME = QUARKUS_RESOURCE_AND_CLASS_SCANNER_CLASS_NAME | ||
| .replace('.', '/'); | ||
|
|
||
| private static final String CTOR_METHOD_NAME = "<init>"; | ||
|
|
||
| @Override | ||
| public ClassVisitor apply(String s, ClassVisitor cv) { | ||
| return new ScannerVisitor(cv); | ||
| } | ||
|
|
||
| private static final class ScannerVisitor extends ClassVisitor { | ||
|
|
||
| public ScannerVisitor(ClassVisitor cv) { | ||
| super(Gizmo.ASM_API_VERSION, cv); | ||
| } | ||
|
|
||
| @Override | ||
| public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { | ||
| MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions); | ||
| if (name.equals(CTOR_METHOD_NAME)) { | ||
| return new ConstructorTransformer(mv); | ||
| } | ||
| return mv; | ||
| } | ||
|
|
||
| /** | ||
| * Replaces the constructor of the {@link Scanner} with: | ||
| * | ||
| * <pre> | ||
| * public ScannerSubstitutions(Class<?> implementedInterface, Collection<Location> locations, ClassLoader classLoader, | ||
| * Charset encoding, ResourceNameCache resourceNameCache, LocationScannerCache locationScannerCache) { | ||
| * ResourceAndClassScanner quarkusScanner = new QuarkusPathLocationScanner(locations); | ||
| * resources.addAll(quarkusScanner.scanForResources()); | ||
| * classes.addAll(quarkusScanner.scanForClasses()); | ||
| * } | ||
| * </pre> | ||
| */ | ||
| private static class ConstructorTransformer extends MethodVisitor { | ||
|
|
||
| public ConstructorTransformer(MethodVisitor mv) { | ||
| super(Gizmo.ASM_API_VERSION, mv); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitCode() { | ||
| super.visitVarInsn(ALOAD, 0); | ||
geoand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| super.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", CTOR_METHOD_NAME, "()V", false); | ||
| super.visitVarInsn(ALOAD, 0); | ||
| super.visitTypeInsn(NEW, "java/util/ArrayList"); | ||
| super.visitInsn(DUP); | ||
| super.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", CTOR_METHOD_NAME, "()V", false); | ||
| super.visitFieldInsn(PUTFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "resources", "Ljava/util/List;"); | ||
| super.visitVarInsn(ALOAD, 0); | ||
| super.visitTypeInsn(NEW, "java/util/ArrayList"); | ||
| super.visitInsn(DUP); | ||
| super.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", CTOR_METHOD_NAME, "()V", false); | ||
| super.visitFieldInsn(PUTFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "classes", "Ljava/util/List;"); | ||
| super.visitTypeInsn(NEW, QUARKUS_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME); | ||
| super.visitInsn(DUP); | ||
| super.visitVarInsn(ALOAD, 2); | ||
| super.visitMethodInsn(INVOKESPECIAL, QUARKUS_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME, | ||
| CTOR_METHOD_NAME, "(Ljava/util/Collection;)V", false); | ||
| super.visitVarInsn(ASTORE, 7); | ||
| super.visitVarInsn(ALOAD, 0); | ||
| super.visitFieldInsn(GETFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "resources", "Ljava/util/List;"); | ||
| super.visitVarInsn(ALOAD, 7); | ||
| super.visitMethodInsn(INVOKEINTERFACE, FLYWAY_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME, | ||
| "scanForResources", "()Ljava/util/Collection;", true); | ||
| super.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "addAll", "(Ljava/util/Collection;)Z", true); | ||
| super.visitInsn(POP); | ||
| super.visitVarInsn(ALOAD, 0); | ||
| super.visitFieldInsn(GETFIELD, FLYWAY_SCANNER_INTERNAL_CLASS_NAME, "classes", "Ljava/util/List;"); | ||
| super.visitVarInsn(ALOAD, 7); | ||
| super.visitMethodInsn(INVOKEINTERFACE, FLYWAY_RESOURCE_AND_CLASS_SCANNER_INTERNAL_CLASS_NAME, | ||
| "scanForClasses", "()Ljava/util/Collection;", true); | ||
| super.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "addAll", "(Ljava/util/Collection;)Z", true); | ||
| super.visitInsn(POP); | ||
| super.visitInsn(RETURN); | ||
| super.visitMaxs(3, 8); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
2 changes: 2 additions & 0 deletions
2
.../flyway/deployment/src/test/resources/migrate-at-start-config-named-datasource.properties
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
45 changes: 0 additions & 45 deletions
45
...ns/flyway/runtime/src/main/java/io/quarkus/flyway/runtime/graal/ScannerSubstitutions.java
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.