RFR: 8197594 - String and character repeat

Jim Laskey james.laskey at oracle.com
Thu Feb 15 17:34:19 UTC 2018


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