RFR: 8316681: Rewrite URLEncoder.encode to use small reusable buffers

Daniel Fuchs dfuchs at openjdk.org
Thu Sep 21 16:19:42 UTC 2023


On Thu, 21 Sep 2023 14:32:46 GMT, Claes Redestad <redestad at openjdk.org> wrote:

> `URLEncoder` currently appends chars that needs encoding into a `java.io.CharArrayWriter`, converts that to a `String`, uses `String::getBytes` to get the encoded bytes and then appends these bytes in a escaped manner to the output stream. This is somewhat inefficient.
> 
> This PR replaces the `CharArrayWriter` with a reusable `CharBuffer` + `ByteBuffer` pair. This allows us to encode to the output `StringBuilder` in small chunks, with greatly reduced allocation as a result.
> 
> The exact size of the buffers is an open question, but generally it seems that a tiny buffer wins by virtue of allocating less, and that the per chunk overheads are relatively small.

src/java.base/share/classes/java/net/URLEncoder.java line 245:

> 243:         CharsetEncoder ce = charset.newEncoder();
> 244:         CharBuffer cb = CharBuffer.allocate(ENCODING_CHUNK_SIZE);
> 245:         ByteBuffer bb = ByteBuffer.allocate((int)(ENCODING_CHUNK_SIZE * (double)ce.maxBytesPerChar()));

The cast to (double) is curious

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/15865#discussion_r1333318700


More information about the net-dev mailing list