- 
                Notifications
    You must be signed in to change notification settings 
- Fork 96
Optimize DialogueFeignClient small response reader #3114
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
| Generate changelog in  | 
| This PR has been automatically marked as stale because it has not been touched in the last 14 days. If you'd like to keep it open, please leave a comment or add the 'long-lived' label, otherwise it'll be closed in 7 days. | 
| ✅ Successfully generated changelog entry!What happened?Your changelog entries have been stored in the database as part of our migration to ChangelogV3. Need to regenerate?Simply interact with the changelog bot comment again to regenerate these entries. | 
e336971    to
    2d19331      
    Compare
  
    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.
Pull Request Overview
This PR optimizes the DialogueFeignClient by avoiding InputStreamReader and HeapByteBuffer overhead for small responses (less than 8KiB). The optimization reads the entire response into memory as a string for small payloads, which can be more efficient than stream-based reading for small data.
Key changes:
- Added optimization for small response bodies in the asReader()method
- Updated URL decoding to use StandardCharsets.UTF_8 directly instead of string literal
- Removed unused UnsupportedEncodingException import
| if (maybeLength != null && maybeLength < 8192) { | ||
| // Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, | ||
| // see https://github.com/FasterXML/jackson-core/pull/1081 | ||
| try (InputStream inputStream = asInputStream()) { | 
    
      
    
      Copilot
AI
    
    
    
      Aug 12, 2025 
    
  
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.
The try-with-resources block closes the InputStream after creating the StringReader, but the StringReader contains the full content and doesn't need the stream to remain open. However, this creates a potential issue if asInputStream() returns the same underlying stream instance that might be needed elsewhere. Consider ensuring this optimization only applies when it's safe to consume the entire stream.
| try (InputStream inputStream = asInputStream()) { | |
| InputStream inputStream = asInputStream(); | |
| try { | 
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.
when the response is 8KiB or less we want to fully consume the response input stream and release the response after converting to an immutable String & StringReader for consumption by Jackson deserialization
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.
Is there a guarantee that if asReader() has been called, nothing will then call asInputStream()? I imagine it doesn't make much sense to call both, but I'm not very familiar with this API.
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.
adjusted the logic per below
        
          
                ...a-jaxrs-client/src/main/java/com/palantir/conjure/java/client/jaxrs/DialogueFeignClient.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | // Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, | ||
| // see https://github.com/FasterXML/jackson-core/pull/1081 | ||
| try (InputStream inputStream = asInputStream()) { | ||
| return new StringReader(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)); | 
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.
Is there any way to allocate the string more directly? inputStream.readAllBytes() will allocate a byte[] and new String is going to copy that byte array into a new one, leading to twice the allocation. I'm not clear whether that is in fact more efficient than the current behavior if the length is more than 4096
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.
unfortunately there is not a more direct way to allocate the String without extra array copy outside of JDK.
The relevant bits of benchmark FasterXML/jackson-benchmarks#9 (comment) show that StringReader version is beneficial or equivalent up to 32KiB, so would prefer to just keep the 8KiB limit here
Benchmark                                       (mode)  (size)               (type)  Mode  Cnt   Score    Error  Units
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE     128  INPUT_STREAM_READER  avgt    3   0.475 ±  0.006  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE     128        STRING_READER  avgt    3   0.144 ±  0.002  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE     512  INPUT_STREAM_READER  avgt    3   0.786 ±  0.035  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE     512        STRING_READER  avgt    3   0.388 ±  0.096  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    1024  INPUT_STREAM_READER  avgt    3   1.105 ±  0.004  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    1024        STRING_READER  avgt    3   0.698 ±  0.004  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    2048  INPUT_STREAM_READER  avgt    3   1.537 ±  0.088  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    2048        STRING_READER  avgt    3   1.456 ±  3.178  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    4096  INPUT_STREAM_READER  avgt    3   3.047 ±  0.010  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    4096        STRING_READER  avgt    3   3.113 ±  0.040  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    7168  INPUT_STREAM_READER  avgt    3  10.650 ±  2.855  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    7168        STRING_READER  avgt    3  11.025 ± 23.020  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    8192  INPUT_STREAM_READER  avgt    3  13.048 ±  0.202  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE    8192        STRING_READER  avgt    3  12.708 ±  0.513  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE   16384  INPUT_STREAM_READER  avgt    3  32.922 ±  0.386  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE   16384        STRING_READER  avgt    3  32.588 ±  0.890  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE   32768  INPUT_STREAM_READER  avgt    3  72.577 ± 11.414  us/op
JsonArbitraryFieldNameBenchmark.parse  NO_CANONICALIZE   32768        STRING_READER  avgt    3  72.497 ± 13.592  us/op
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.
Also worth noting that when InputStreamReader is returned, feign.jackson.JacksonDecoder#decode must wrap it in a BufferedReader with a char[1] buffer as InputStreamReader doesn't support mark/reset, while StringReader does.
| if (maybeLength != null && maybeLength < 8192) { | ||
| // Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, | ||
| // see https://github.com/FasterXML/jackson-core/pull/1081 | ||
| try (InputStream inputStream = asInputStream()) { | 
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.
Is there a guarantee that if asReader() has been called, nothing will then call asInputStream()? I imagine it doesn't make much sense to call both, but I'm not very familiar with this API.
| // Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, | ||
| // see https://github.com/FasterXML/jackson-core/pull/1081 | ||
| try (InputStream inputStream = asInputStream()) { | ||
| return new StringReader(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8)); | 
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.
Other question: Should we guard against malicious/bad responses that return an incorrect length? (e.g. I imagine there is a corner case where the length is set to 0, which doesn't make sense)
Here we'll buffer the entire response in memory. If the content length header lied to us, we might buffer a lot more than we expected, since we just read the entire thing in memory. Should we just read the first length bytes only?
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.
improved the handling here in cases where content-length is incorrect
Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs, see FasterXML/jackson-core#1081
2d19331    to
    9423be2      
    Compare
  
    | Released 8.24.0 | 
Before this PR
Small responses <= 8KiB would always allocate 8KiB
ByteBufferasInputStreamReadercreates aStreamDecoderthat allocates a fixed 8192 byteByteBuffer. This allocation becomes a scalability bottleneck for high throughput RPCs with small responses (think something returning timestamps, locks, authorization results, etc.)See https://github.com/openjdk/jdk/blob/4c03e5938df0a9cb10c2379af81163795dd3a086/src/java.base/share/classes/sun/nio/cs/StreamDecoder.java#L248
After this PR
==COMMIT_MSG==
Avoid
InputStreamReader/HeapByteBufferoverhead for small (less than 8KiB) inputs, see FasterXML/jackson-core#1081 and FasterXML/jackson-benchmarks#9 (comment) for benchmarks showing between 2x and 10x speedup handling deserialization of small values.==COMMIT_MSG==
Possible downsides?