RFR: 8197594 - String and character repeat

Remi Forax forax at univ-mlv.fr
Thu Feb 15 20:24:13 UTC 2018

----- Mail original -----
> De: "Ivan Gerasimov" <ivan.gerasimov at oracle.com>
> À: "Jim Laskey" <james.laskey at oracle.com>, "core-libs-dev" <core-libs-dev at openjdk.java.net>
> Envoyé: Jeudi 15 Février 2018 20:36:44
> Objet: Re: RFR: 8197594 - String and character repeat

> Hello!
> The link with the webrev returned 404, but I could find it at this
> location: http://cr.openjdk.java.net/~jlaskey/8197594/webrev-00/

in static String repeat(char ch, int count) 
  you can remove the 'else's because they are after a throw or a return as you have done in the other methods.

in static String repeat(int codepoint, int count),
  the parenthesis just after the return seems unnecessary,

in String repeat(int count)
  at the end instead of testing isLatin1()  
    return new String(bytes, coder);
  should be equivalent.

in replicate
  you can return 'result' after the call to Arrays.fill, to signal to readers that this is an optimization and that the primary code is below, it will also avoid to shift the primary code to the right.

and nitpicking about the style, can you remove those pesky final, it's the Nashorn style but not the JDK one :)


> On 2/15/18 9:20 AM, Jim Laskey 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
> --
> With kind regards,
> Ivan Gerasimov

More information about the core-libs-dev mailing list