-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[java-generator] performance improvementes #4867
Changes from 2 commits
6ca4012
9745e19
5bb577a
cfc7f3d
2cc3928
a8b6ead
62141fa
6d9b466
067784b
6b5b279
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -32,6 +32,8 @@ | |||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
import java.util.Locale; | ||||||
import java.util.concurrent.CompletableFuture; | ||||||
import java.util.stream.Collectors; | ||||||
import java.util.stream.Stream; | ||||||
|
||||||
import static io.fabric8.java.generator.CRGeneratorRunner.groupToPackage; | ||||||
|
@@ -83,32 +85,50 @@ private void runOnSingleSource(File source, File basePath) { | |||||
resources.add(deserialized); | ||||||
} | ||||||
|
||||||
resources.parallelStream() | ||||||
.forEach( | ||||||
rawResource -> { | ||||||
if (rawResource != null && rawResource instanceof HasMetadata) { | ||||||
final HasMetadata resource = (HasMetadata) rawResource; | ||||||
writeCRCompilationUnits(basePath, generateWritableCRCompilationUnits(resources)); | ||||||
|
||||||
if (resource != null && resource.getKind() | ||||||
.toLowerCase(Locale.ROOT) | ||||||
.equals("customresourcedefinition")) { | ||||||
CustomResourceDefinition crd = (CustomResourceDefinition) resource; | ||||||
|
||||||
final String basePackage = groupToPackage(crd.getSpec().getGroup()); | ||||||
|
||||||
crGeneratorRunner.generate(crd, basePackage).parallelStream() | ||||||
.forEach(w -> w.writeAllJavaClasses(basePath)); | ||||||
} else { | ||||||
LOGGER.warn("Not generating nothing for resource of kind: {}", resource.getKind()); | ||||||
} | ||||||
} else { | ||||||
LOGGER.warn("Not generating nothing for unrecognized resource: {}", Serialization.asYaml(rawResource)); | ||||||
} | ||||||
}); | ||||||
} catch (FileNotFoundException e) { | ||||||
throw new JavaGeneratorException("File " + source.getAbsolutePath() + " not found", e); | ||||||
} catch (IOException e) { | ||||||
throw new JavaGeneratorException("Exception reading " + source.getAbsolutePath(), e); | ||||||
} | ||||||
} | ||||||
|
||||||
private List<WritableCRCompilationUnit> generateWritableCRCompilationUnits(List<Object> resources) { | ||||||
return resources.parallelStream() | ||||||
.flatMap( | ||||||
rawResource -> { | ||||||
if (rawResource != null && rawResource instanceof HasMetadata) { | ||||||
final HasMetadata resource = (HasMetadata) rawResource; | ||||||
|
||||||
if (resource != null && resource.getKind() | ||||||
.toLowerCase(Locale.ROOT) | ||||||
.equals("customresourcedefinition")) { | ||||||
CustomResourceDefinition crd = (CustomResourceDefinition) resource; | ||||||
|
||||||
final String basePackage = groupToPackage(crd.getSpec().getGroup()); | ||||||
return crGeneratorRunner.generate(crd, basePackage).stream(); | ||||||
} else { | ||||||
LOGGER.warn("Not generating nothing for resource of kind: {}", resource.getKind()); | ||||||
} | ||||||
} else { | ||||||
LOGGER.warn("Not generating nothing for unrecognized resource: {}", Serialization.asYaml(rawResource)); | ||||||
} | ||||||
return Stream.empty(); | ||||||
}) | ||||||
.collect(Collectors.toList()); | ||||||
} | ||||||
|
||||||
private void writeCRCompilationUnits(File basePath, List<WritableCRCompilationUnit> wCUs) { | ||||||
CompletableFuture<Void>[] futures = new CompletableFuture[wCUs.size()]; | ||||||
|
||||||
int i = 0; | ||||||
for (WritableCRCompilationUnit w : wCUs) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
futures[i++] = CompletableFuture.runAsync(() -> w.writeAllJavaClasses(basePath)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you attempted to use a different Thread Pool than the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At that point the code generation performed in the parallel stream is finished and that pool should be completely available to write files. Also consider that creating a brand new thread pool could be very likely much more expansive than writing the file, that's why imo it is a better idea to reuse an already existing pool. Anyway if we had the JMH benchmark that I'm proposing in the other comment we could properly measure this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can find the yaml I was using here: https://github.com/kserve/kserve/releases/download/v0.10.0/kserve.yaml Feel free to add a JMH benchmark if you think that it's useful, but I'm convinced that this PR is perfectly enough for a first iteration. Thanks again! |
||||||
} | ||||||
|
||||||
for (int j = 0; j < futures.length; j++) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
futures[j].join(); | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] can you, please, move
wCUs.size()
to a val (e.g.total
) and reuse it as a boundary in the followingfor
loops?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.