On Wed, 18 Mar 2026 12:30:36 GMT, Eirik Bjørsnøs <[email protected]> wrote:
>> This PR optimizes string concatenation in `URL::toExternalForm`. This method >> is called by `URL:toString` and executes often enough to be noticed during >> startup / classloading profiling. It was previously optimized in JDK 10 via >> JDK-8193034, but we can do more. >> >> Key observations: >> >> * The current method concatenates concatenated strings. We can get away with >> less by flattening and concatenating once per call. >> * JIT StringBuilder concat optimizations prefer consecutive append calls >> withot interleaving code / conditionals >> * The current method does not take advantage of the fact that most URL >> components are optional and several are commonly not present. We can >> specialize for those cheaper common concatenations. >> >> This PR reduces runtime with 60% or more, depending on present URL >> components. (Benchmark results in first comment). I also ran the benchmark >> with compilation excluded and found no regressions for the interpreter case. >> >> Working on this PR revealed that current tests miss coverage for the case >> where a query or ref component is the empty string. Some new test cases are >> added to `Constructor.java`. Otherwise, `URL::toString` seems well tested by >> current tests. >> >> Performance refactoring, `noreg-perf`. > > Eirik Bjørsnøs has updated the pull request incrementally with one additional > commit since the last revision: > > Expand code comment explaining optimizations to be more explicit and > accurate The new comment block LGTM. > So I expanded the code comment a bit into what I think is more explicit and > accurate, without diving into details too much. IMHO, the point of this particular comment block is to warn the next person touching to this piece of code that fast paths and branch-free concatenations are there for a reason, don't update it without checking performance figures. The details of how this all works today (`OptimizedStringConcat`, etc.) will likely to not apply in some near future anyway. ------------- Marked as reviewed by vyazici (Reviewer). PR Review: https://git.openjdk.org/jdk/pull/30151#pullrequestreview-3967795968
