On Mon, 12 Feb 2024 22:52:51 GMT, Attila Szegedi <att...@openjdk.org> wrote:

> Somewhat surprisingly, `ArrayList$Sublist.sort()` is not specialized and will 
> thus fall back to slower default method of `List.sort()` instead of sorting a 
> range of the array in-place in its backing root `ArrayList`. 
> 
> This doesn't change observable behavior, so haven't added tests, and `tier1` 
> tests still all pass except for 
> `test/jdk/java/util/Locale/LocaleProvidersFormat.java` which also currently 
> fails on master too on the machine I tested on.

(Discussion mainly of historical interest.)

@pavelrappo Correct, historically, `Collections.sort` would fail to sort 
`CopyOnWriteArrayList`. You have to go back to JDK 7 to see this. The sorting 
approach used by `Collections.sort` (still present in the default 
implementation of `List.sort`) gets an array using `toArray()`, sorts it, and 
then copies the sorted elements back using `ListIterator.set`. Since 
`CopyOnWriteArrayList` doesn't support modifications using `ListIterator`, it 
fails with `UnsupportedOperationException`. The overrides of `List.sort` have 
fixed this problem.

COWAL still has some problems with other things that use similar techniques, 
such as `Collections.shuffle`. That uses get/set to swap elements, which COWAL 
does support, but it copies the array on each set() operation. This results in 
N copies of the array being made for an N-element list.

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

PR Comment: https://git.openjdk.org/jdk/pull/17818#issuecomment-1945025984

Reply via email to