RFR: 8266972: Use String.concat() in j.l.Class where invokedynamic-based String concatenation is not available

Claes Redestad redestad at openjdk.java.net
Wed May 12 13:15:23 UTC 2021


On Wed, 12 May 2021 11:00:25 GMT, Сергей Цыпанов <github.com+10835776+stsypanov at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/Class.java line 4351:
>> 
>>> 4349: 
>>> 4350:         if (isArray()) {
>>> 4351:             return "[".concat(componentType.descriptorString());
>> 
>> This could be optimized further for multi-dimensional arrays:
>> 
>> if (isArray()) {
>>   int arrayDepth = 1;
>>   Class<?> ct = componentType;
>>   while (ct.isArray()) {
>>     arrayDepth++;
>>     ct = ct.componentType;
>>   }
>>   return "[".repeat(arrayDepth).concat(ct.descriptorString());
>
> But isn't `componentType.descriptorString()` does this itself? Also multi-dimensional arrays are quite an infrequent usecase, aren't they?

Yeah, it's just an optimization. Current code wouldbuild "[[..[[LFoo;" by recursively going down to `Foo`, build and return "LFoo;", then do "[" + "LFoo;", and so on. Infrequent enough that we can ignore it, sure, but since it's effectively reducing the algorithmic complexity from O(n*m) to O(n+m) I should at least mention it.

-------------

PR: https://git.openjdk.java.net/jdk/pull/3627


More information about the core-libs-dev mailing list