On Tue, 10 Aug 2021 21:06:00 GMT, Сергей Цыпанов 
<github.com+10835776+stsypa...@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.
>
> Сергей Цыпанов has updated the pull request with a new target base due to a 
> merge or a rebase. The incremental webrev excludes the unrelated changes 
> brought in by the merge/rebase. The pull request contains five additional 
> commits since the last revision:
> 
>  - 8267844: Add benchmarks for Integer/Long.decode()
>  - 8267844: Rid useless substring when calling Integer/Long.parse*()
>  - Merge branch 'master' into 8267844
>  - Merge branch 'master' into 8267844
>  - 8267844: Replace Integer/Long.valueOf() with Integer/Long.parse*() where 
> applicable

OK even if we keep out the edge case in the try block the 
"parseLong(nm.substring(index), radix)" could be replaced as already mentioned 
with parseLong(nm. index, nm.length(), radix)
and in the catch block  the idea to throw an "nice" error can be misleading.
since -02147483648 for example would become -2147483648 because the radix is 8. 
Since per Radix only one String is possible to get through would if not be 
faster and more clear to check (compare) if it is the matching string and 
return the correct value else throw the error. This is also easy to read and 
even if is on the edge avoid substring , concationation and reparsing.

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

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

Reply via email to