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

Claes Redestad redestad at openjdk.org
Thu Sep 21 15:26:51 UTC 2023


`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.

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

Commit messages:
 - Inline encodeByte et.c
 - Add latin1 encode/decode tests
 - flush and reset after each encoding block
 - Rewrite URLEncoder.encode to encode using small reusable buffers

Changes: https://git.openjdk.org/jdk/pull/15865/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=15865&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8316681
  Stats: 70 lines in 3 files changed: 51 ins; 13 del; 6 mod
  Patch: https://git.openjdk.org/jdk/pull/15865.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/15865/head:pull/15865

PR: https://git.openjdk.org/jdk/pull/15865


More information about the net-dev mailing list