-
Notifications
You must be signed in to change notification settings - Fork 109
Add bulk read method to InputStream implementations #961
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
Conversation
Adds a recipe to detect and fix InputStream subclasses that only override the single-byte read() method. Java's default bulk read implementation calls read() in a loop, which can cause up to 350x slower performance. For simple delegation patterns, the recipe adds the missing bulk read method. For complex bodies (side effects, transformations), it adds a search marker for manual review. Moved from openrewrite/rewrite#6383 per review feedback.
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethodTest.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/io/AddInputStreamBulkReadMethod.java
Show resolved
Hide resolved
timtebeek
left a comment
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.
Nice one! Should help folks and fix performance problems they might not even know they have.
For now we've not added this recipe to any larger composite, meaning folks would need to seek out and run this recipe specifically; shall we keep it that way?
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(new DeclaresType<>(JAVA_IO_INPUT_STREAM, true), new JavaIsoVisitor<ExecutionContext>() { |
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.
This required a small change upstream to allow the precondition to be used: openrewrite/rewrite@e91804a
I dont really know what the practice here has been in the past. What do you advise? |
|
I think it's fine to keep as is for now: especially since we can't automatically fix any case it might be best not to repeatedly warn about any such cases if folks decide not to follow up. |
Problem
Java's default
InputStream.read(byte[], int, int)implementation calls the single-byteread()in a loop, causing up to 350x slower performance for bulk reads. Many InputStream subclasses only override the single-byteread()method but not the bulk read method.Solution
This recipe detects InputStream subclasses that:
read()but notread(byte[], int, int)InputStreamFor simple delegation patterns, it adds the missing bulk read method. For complex bodies (side effects, transformations), it adds a search marker for manual review.
Handles both anonymous and named classes, ternary and if-statement null check styles, and skips FilterInputStream subclasses.
org.openrewrite.java.migrate.iopackage.