[ 
https://issues.apache.org/jira/browse/WICKET-6992?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17561008#comment-17561008
 ] 

ASF subversion and git services commented on WICKET-6992:
---------------------------------------------------------

Commit 04df0167e6986233390dc01e9081a3bac6768af0 in wicket's branch 
refs/heads/master from Alan Stange
[ https://gitbox.apache.org/repos/asf?p=wicket.git;h=04df0167e6 ]

WICKET-6992 Reduce object creation and wasted memory in Url toString() methods

The Url class generates a lot of StringBuilders and temporary buffers.  It's a 
bit frustrating form a performance
standpoint as the builders often need to resize, further compounding the 
performance impact.

- QueryParameter.toString(...) can be updated to not generate any extraneous 
garbage objects using JEP 280 String
concatenation.   This is faster than the original code and avoids extra garbage 
object creation.

- enhance Url.getPath() by using getPathInternal() which allows the code to 
return an optimal result.  "" for no segments;
the String for 1 segment, and the resulting StringBuilder for more than 2 
segments.  This avoids copying the internal byte[]/char[]
array yet again and wasting more memory.  The callers of this method can then 
use the returned object in the way that best
suits them.   NB the case for two segments should probably also be handled by 
explicit invocation of
   seg1 + / + seg2 as is would be faster and save memory.
Note that we now compute the size of the StringBuilder in advance, avoiding the 
repeated resizing of the builder that had been
happening in the original code.

- enhance getQueryString() in a similar manner.  Handle the n=0 and n=1 common 
cases without generating any extra garbage.
Size the StringBuilder a little better in this case as well.  Again, the n=2 
case should probably with String concatenation.

The Url.toString(mode, charset) method is a bit more complicated.  It is rarely 
called (if ever?) with StringMode=FULL and it
appears as if fragment is commonly unset as well, so handle that 99% code path 
at the top efficiently without any
wasted memory.   If not, then fall through into the original code.    The call 
to getPathInternal here avoids making a copy of
that array.

Note that it might be much more efficient to allow this code to pass a 
StringBuilder instance around.  This would avoid some
repeated copying of byte[]/char[] arrays.   These changes here greatly reduce 
the memory waste, but more can still be done.

I'm a bit surprised about the missing ).  Must have fat fingered it at the last 
second.

I'm a bit surprised about the missing ).  Must have fat fingered it at the last 
second.

I'm not a fan of the brace style used in this project... ;-)

Two changes in response to the feedback.
- cache the result of a function call
- use charAt() and avoid a toString() call.

Fix code formatting.


> Reduce object creation and wasted memory in Url toString() methods
> ------------------------------------------------------------------
>
>                 Key: WICKET-6992
>                 URL: https://issues.apache.org/jira/browse/WICKET-6992
>             Project: Wicket
>          Issue Type: Improvement
>    Affects Versions: 10.0.0, 9.11.0
>            Reporter: Andrea Del Bene
>            Assignee: Andrea Del Bene
>            Priority: Minor
>             Fix For: 10.0.0, 9.11.0
>
>
> The processing for toString() in the Url class is a bit expensive. I think we 
> can do better by using String concatenation, avoiding copying byte[]/char[] 
> arrays, and sizing StringBuilders effectively.
> More could be done to improve this, but these changes are not too aggressive. 
> Ideally a common StringBuilder would be passed around, like the charset, to 
> accumulate the String as processing is done.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to