RFR: 8267844: Replace Integer/Long.valueOf() with Integer/Long.parse*() where applicable
Thomas Lußnig
openjdk at suche.org
Tue Aug 10 19:00:53 UTC 2021
Hi,
i wonder why all usages of decode should be replaced.
Since Integer.valueOf(text,radix) =
Integer.valueOf(Ineger.parseInt(text,radix))
The double allocation with
result = Integer.valueOf(nm.substring(index), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result;
could be replace with either
int result = Integer.parseInt(nm.substring(index),radix)
result = Integer.valueOf(negative ? -result : result);
or
result = Integer.valueOf(negative
? -Integer.parseInt(nm.substring(index),radix)
: Integer.parseInt(nm.substring(index),radix));
this way only one place is changed and the performance of parse is
improved wihtout change it.
Gruß Thomas Lußnig
On 10.08.2021 19:43:15, Сергей Цыпанов wrote:
> On Tue, 10 Aug 2021 14:56:11 GMT, Claes Redestad <redestad at openjdk.org> wrote:
>
>>> The code in `Integer.decode()` and `Long.decode()` might allocate two instances of Integer/Long for the negative values less than -127:
>>>
>>> Integer result;
>>>
>>> result = Integer.valueOf(nm.substring(index), radix);
>>> result = negative ? Integer.valueOf(-result.intValue()) : result;
>>>
>>> To avoid this we can declare 'result' as `int` and use `Integer.parseInt()` method. Same applicable for `Long` and some other classes.
>> src/java.base/share/classes/java/lang/Integer.java line 1450:
>>
>>> 1448:
>>> 1449: try {
>>> 1450: result = parseInt(nm.substring(index), radix);
>> Possibly a follow-up, but I think using `parseInt/-Long(nm, index, nm.length(), radix)` could give an additional speed-up in these cases (by avoiding a substring allocation).
> Good point! Let me check this.
>
> -------------
>
> PR: https://git.openjdk.java.net/jdk/pull/5068
More information about the security-dev
mailing list