Skip to content

Conversation

schlosna
Copy link
Contributor

@schlosna schlosna commented May 12, 2025

Before this PR

Small responses <= 8KiB would always allocate 8KiB ByteBuffer as InputStreamReader creates a StreamDecoder that allocates a fixed 8192 byte ByteBuffer. 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 / HeapByteBuffer overhead 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?

@changelog-app
Copy link

changelog-app bot commented May 12, 2025

Generate changelog in changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services
    and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the
    operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration,
    performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Optimize DialogueFeignClient small response reader

Check the box to generate changelog(s)

  • Generate changelog entry

Copy link

stale bot commented Jun 27, 2025

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.

@stale stale bot added the stale label Jun 27, 2025
@stale stale bot removed the stale label Aug 12, 2025
@changelog-app
Copy link

changelog-app bot commented Aug 12, 2025

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.

@schlosna schlosna force-pushed the davids/small-feign branch from e336971 to 2d19331 Compare August 12, 2025 16:57
@schlosna schlosna requested a review from Copilot August 12, 2025 16:59
Copy link

@Copilot Copilot AI left a 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()) {
Copy link
Preview

Copilot AI Aug 12, 2025

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.

Suggested change
try (InputStream inputStream = asInputStream()) {
InputStream inputStream = asInputStream();
try {

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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

@schlosna schlosna requested review from a team and tpetracca and removed request for a team August 14, 2025 20:35
// 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));
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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()) {
Copy link
Contributor

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));
Copy link
Contributor

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?

Copy link
Contributor Author

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

@schlosna schlosna force-pushed the davids/small-feign branch from 2d19331 to 9423be2 Compare August 20, 2025 19:39
@bulldozer-bot bulldozer-bot bot merged commit 7386ebc into develop Aug 21, 2025
5 checks passed
@bulldozer-bot bulldozer-bot bot deleted the davids/small-feign branch August 21, 2025 10:43
@autorelease3
Copy link

autorelease3 bot commented Aug 21, 2025

Released 8.24.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants