RFR: 8197594 - String and character repeat

Jim Laskey james.laskey at oracle.com
Thu Feb 15 20:16:48 UTC 2018



> On Feb 15, 2018, at 4:04 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> I'm not sure we need 4, it's just a convenient method that may be slower than if the user code calls toString() (because of profile pollution),
> so i'm not sure i pull it's own weight.
> 
> And about adding a default method into CharSequence, the default method should return a CharSequence or String ? 

If you mean each class returns an instance of its class, I think that overlaps Appendable..

> and what about the other implementations, AbstractStringBuilder and CharBuffer at least ? 

This falls into the Appendable.append( T t, int count) realm mentioned originally.

Long term this could be a goal, and maybe defaulting CharSequence#repeat returning a string would be shortsighted.

But, I think having instance String#repeat returning a CharSequence would limit its use (methods expecting strings.)

— Jim

> 
> Rémi
> 
> ----- Mail original -----
>> De: "Jim Laskey" <james.laskey at oracle.com>
>> À: "Brian Goetz" <brian.goetz at oracle.com>
>> Cc: "core-libs-dev" <core-libs-dev at openjdk.java.net>
>> Envoyé: Jeudi 15 Février 2018 18:34:19
>> Objet: Re: RFR: 8197594 - String and character repeat
> 
>> Very reasonable approach.
>> 
>> 
>>> On Feb 15, 2018, at 1:31 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
>>> 
>>> I suggest merging 1 and 4 by making it an instance method on CS with a default
>>> in CS and an override on string and string builder?
>>> 
>>> Sent from my MacBook Wheel
>>> 
>>>> On Feb 15, 2018, at 9:20 AM, Jim Laskey <james.laskey at oracle.com> wrote:
>>>> 
>>>> This is a pre-CSR code review [1] for String repeat methods (Enhancement).
>>>> 
>>>> The proposal is to introduce four new methods;
>>>> 
>>>> 1. public String repeat(final int count)
>>>> 2. public static String repeat(final char ch, final int count)
>>>> 3. public static String repeat(final int codepoint, final int count)
>>>> 4. public static String repeat(final CharSequence seq, final int count)
>>>> 
>>>> For the sake of transparency, only 1 is necessary, 2-4 are convenience methods.
>>>> In the case of 2, “*”.repeat(10) performs as well as String.repeat(‘*’, 10).
>>>> 3 and 4 convert to String before calling 1.
>>>> 
>>>> Performance runs with jmh (results as comment in [2]) show that these
>>>> methods are significantly faster that StringBuilder equivalents.
>>>> - fewer memory allocations
>>>> - fewer char to byte array conversions
>>>> - faster pyramid replication vs O(N) copying
>>>> 
>>>> I left StringBuilder out of scope. It falls under the category of
>>>> Appendables#append with repeat. A much bigger project.
>>>> 
>>>> All comments welcome. Especially around the need for convenience
>>>> methods, the JavaDoc content and expanding the tests.
>>>> 
>>>> — Jim
>>>> 
>>>> [1] webrev: http://cr.openjdk.java.net//oj/home/jlaskey/8197594/webrev-00
>>>> [2] jbs: https://bugs.openjdk.java.net/browse/JDK-8197594
>>>> 



More information about the core-libs-dev mailing list