RFR 8170348: Appendable.appendN(char, int) method to append multiple copies of char
Ivan Gerasimov
ivan.gerasimov at oracle.com
Sun Dec 11 20:35:26 UTC 2016
Thank you Roger for the comments!
On 08.12.2016 1:28, Roger Riggs wrote:
> Hi Ivan,
>
> A few comments...
>
> Appendable.appendN default method:
> I'd expect many time n is small so allocating a new StringBuilder
> every time seems
> not optimal. A simple loop with append(c) would have fewer
> (memory) side effects.
> And in particular for n=0, it could avoid doing anything.
>
The rationale here was to implement `all or nothing` approach.
If we add chars one by one in a loop and hit OOM, then the Appendable
would be partially modified, which is not good.
A special case for the size of zero can be added, of course.
> AbstractStringBuilder:
> I agree with Claes' comment suggesting that IAE for negative
> lengths is a pain
> and defining it to append 0 would be natural in many use cases.
>
I see that most votes are for throwing the exception if n is negative.
Let's leave it this way!
> MethodHandleImpl.toString():
> You could beneficially replace StringBuffer with StringBuilder
> while you are there.
> (and maybe pre-size the builder).
>
Sure, let's use StringBuilder.
I wasn't sure how to easily calculate the required capacity of the
builder here though.
Please find the updated webrev here:
http://cr.openjdk.java.net/~igerasim/8170348/01/webrev/
With kind regards,
Ivan
More information about the core-libs-dev
mailing list