shinseongsu opened a new pull request, #1538: URL: https://github.com/apache/httpcomponents-core/pull/1538
I was profiling the classic client with async-profiler while comparing it with a few other Java HTTP clients, and `EntityUtils.toByteArray` stood out on large responses. For a 1 MB body it allocates about 2 MB per call (the `ByteArrayBuffer` sized to Content-Length, then the `toByteArray()` copy) and reads the socket in 4 KB chunks, so roughly 256 `read()` calls per MB. `toString` adds an 8 KB `InputStreamReader` buffer, a `char[1024]` and a `CharArrayBuffer` on top of that. With this change, when Content-Length is known and within `maxResultLength`, the content is read straight into a `byte[contentLength]` and then `read()` is called once more to confirm the end of the stream. `toString` decodes that array with `new String(bytes, charset)`. If the stream turns out to be longer than declared, the bytes already read are handed to the old loop, so the result is the same as before. Unknown length takes the old path unchanged. Numbers below are the classic client (httpclient5 master) against a Go HTTP/1.1 server on loopback, JDK 21, 60 s per run after a warm-up, two rounds in alternating order. Only the httpcore5 jar differs: 5.5 vs 5.5 with this change (`EntityUtils.java` in 5.5 is the same as master). Request counts match the server's own counter in every run. | read with | body | rps | alloc / request | CPU / request | p99 | |---|---|---|---|---|---| | `toByteArray` | 1 MB, 16 in flight | 5.2k → 13.1k | 1,976 → 983 KB | 1,881 → 232 µs | 9.6 → 2.5 ms | | `toByteArray` | 1 KB, 64 in flight | ~145k → ~145k | 11.9 → 6.8 KB | 30.4 → 29.3 µs | 1.39 → 1.36 ms | | `toString` | 1 MB, 16 in flight | 8.3k → 11.8k | 2,941 → 1,921 KB | 916 → 311 µs | 6.8 → 3.5 ms | | `toString` | 1 KB, 64 in flight | ~143k → ~143k | 19.5 → 8.3 KB | 32.5 → 30.8 µs | 1.39 → 1.39 ms | At 1 KB the load generator on the same machine is the limit, so the difference there is only allocation. Code that reads `getContent()` directly isn't affected; this helps `EntityUtils` and `BasicHttpClientResponseHandler` users.  <details> <summary>round 2 and CPU flame graphs (1 MB, before / after)</summary>    </details> To check that behavior didn't change, I compared the old and new `EntityUtils` on 300k random inputs: declared length shorter, exact, longer, zero or unknown, different `maxResultLength` values and charsets, random read sizes, and streams that throw at the end. Result, exception and `close()` were identical in every case except a stream that is longer than its declared length and also throws at the end, which `ContentLengthInputStream` can't produce. Truncated bodies still fail with `ConnectionClosedException: Premature end of Content-Length delimited message body`, and connection reuse under load is unchanged. Tested: `mvn -pl httpcore5 verify` passes (2031 tests), and httpclient5 master passes against the patched core. Two questions: - Is master enough, or would you like this on 5.4.x too? - When the stream is longer than Content-Length I kept the old behavior (keep reading up to `maxResultLength`). Would you rather treat that as an error? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
