Thank you, Ulf!
maybe the old code wins for looong arrays, so there could be a
threshold to decide between old and new code:
I've modified the benchmark code to test arrays with 90'000 to 100'000
elements. (Previously was testing 1 to 100 elements.)
The performance gain turns out to be even more significant.
On my machine tests show that with that many elements the new code runs
40% faster.
Honestly, I didn't expect that. I thought my code might be a bit slower
and hoped that not much slower.
Sincerely,
Ivan
Am 02.04.2013 23:11, schrieb Ivan Gerasimov:
Thanks for this change. There is a tradeoff here. If the element
is never present, then the older code might be a little faster,
because we can avoid re-traversing the array. Otherwise, the new
code is better.
I've done a little testing on my side.
I used Integer as an underlying type and set length of the array to
the values from 1 to 100.
My code shows a little performance gain - approximately 9%.
I understand it may not be there for all cases, but at least for some
cases it is there.
I prefer it your way (I hate unneeded allocation), but the code was
intentionally written the other way. Let's hear from Doug...
Martin
On Tue, Apr 2, 2013 at 1:38 PM, Ivan Gerasimov
<ivan.gerasi...@oracle.com <mailto:ivan.gerasi...@oracle.com>> wrote:
Please review my proposal for the
CopyOnWriteArrayList.addIfAbsent() method optimization.
http://washi.ru.oracle.com/~igerasim/webrevs/8011215/webrev/index.html
<http://washi.ru.oracle.com/%7Eigerasim/webrevs/8011215/webrev/index.html>
This URL is not readable by external reviewers.
The webrev has been copied here:
http://cr.openjdk.java.net/~coffeys/webrev.8011215.ivan/
<http://cr.openjdk.java.net/%7Ecoffeys/webrev.8011215.ivan/>
The "master" version of CopyOnWriteArrayList is here:
http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java?view=markup
Thanks for the link!
I see that the code in the master version is identical to the one
I've been working on.
So the optimization still could be applied.
Sincerely,
Ivan