RFR [8011215] optimization of CopyOnWriteArrayList.addIfAbsent() (original) (raw)
Ivan Gerasimov ivan.gerasimov at oracle.com
Tue Apr 2 18:53:09 UTC 2013
- Previous message: RFR: JDK-8007799 Base64.getEncoder(0, byte[]) returns encoder that unexpectedly inserts line separator when using some of the encoding methods
- Next message: RFR [8011215] optimization of CopyOnWriteArrayList.addIfAbsent()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello everybody!
Please review my proposal for the CopyOnWriteArrayList.addIfAbsent() method optimization.
http://washi.ru.oracle.com/~igerasim/webrevs/8011215/webrev/index.html
Here is the original function body:
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = new Object[len + 1]; <-- allocate new array in advance
for (int i = 0; i < len; ++i) {
if (eq(e, elements[i])) <-- check whether e is null on every iteration
return false; // exit, throwing away copy
else
newElements[i] = elements[i]; <-- copy elements one by one
}
newElements[len] = e;
setArray(newElements);
The proposed change is to reuse CopyOnWriteArrayList.indexOf() function to check if e is already in the array. If the check passed, new array is allocated withArrays.copyOf(). It uses native System.arraycopy(), which is probably faster than copying elements in the loop.
Sincerely yours, Ivan
- Previous message: RFR: JDK-8007799 Base64.getEncoder(0, byte[]) returns encoder that unexpectedly inserts line separator when using some of the encoding methods
- Next message: RFR [8011215] optimization of CopyOnWriteArrayList.addIfAbsent()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]