8058779: Faster implementation of String.replace(CharSequence, CharSequence) (original) (raw)
Ivan Gerasimov ivan.gerasimov at oracle.com
Sun May 31 15:21:23 UTC 2015
- Previous message: 8058779: Faster implementation of String.replace(CharSequence, CharSequence)
- Next message: 8058779: Faster implementation of String.replace(CharSequence, CharSequence)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 31.05.2015 13:28, Ulf Zibis wrote:
Am 31.05.2015 um 08:38 schrieb Peter Levart: Hi,
Yes, this one is much easier to grasp. As I understand the check is to avoid overflow in calculation of StringBuilder initial capacity (newLenHint). If overflow happened, newLenHint would be negative and StringBuilder construction would fail with NegativeArraySizeException. But the calculation of newLenHint: int newLenHint = value.length - targLen + replValue.length; ...considering the following: value.length >= 0 targLength >= 0 replValue.length >= 0 _targLength <= value.length_ _in case of overflow, it can only produce a negative value. So the_ _check could simply be:_ _if (newLenHint < 0) {_ _throw new OutOfMemoryError();_ _}_ _Hm, what has this situation to do with Out-Of-Memory ?_ That should indicate that we were about to try to allocate an array of size > Integer.MAX_VALUE. E.g. java.util.AbstractCollection.java:
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError
("Required array size too large");
In other words, IMHO NegativeArraySizeException is much better here and additionally saves performance. Additionally you could propagate it to InvalidArgumentException. I don't think it is InvalidArgument case. It just happens that the result is larger then allowed, so let's throw OOM!
Sincerely yours, Ivan
-Ulf
- Previous message: 8058779: Faster implementation of String.replace(CharSequence, CharSequence)
- Next message: 8058779: Faster implementation of String.replace(CharSequence, CharSequence)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]