RFR: 8283063: Optimize Observable{List/Set/Map}Wrapper.retainAll/removeAll

John Hendrikx jhendrikx at openjdk.org
Thu Mar 30 13:14:00 UTC 2023


On Sat, 12 Mar 2022 04:57:37 GMT, Michael Strauß <mstrauss at openjdk.org> wrote:

> `Observable{List/Set/Map}Wrapper.retainAll/removeAll` can be optimized for some edge cases.
> 
> 1. `removeAll(c)`:
> This is a no-op if 'c' is empty.
> For `ObservableListWrapper`, returning early skips an object allocation. For `ObservableSetWrapper` and `ObservableMapWrapper`, returning early prevents an enumeration of the entire collection.
> 
> 2. `retainAll(c)`:
> This is a no-op if the backing collection is empty, or equivalent to `clear()` if `c` is empty.
> 
> I've added some tests to verify the optimized behavior for each of the three classes.

Changes requested by jhendrikx (Committer).

modules/javafx.base/src/main/java/com/sun/javafx/collections/ObservableListWrapper.java line 172:

> 170:     @Override
> 171:     public boolean removeAll(Collection<?> c) {
> 172:         if (backingList.isEmpty() || c.isEmpty()) {

I think you should do an explicit `null` check here on `c` or swap the order of these arguments so it always throws an NPE here if `c` is `null` as per collection contract.  If you don't, it will do this implicit `null` check just after `beginChange`, and as I don't see a `try/finally` there to call `endChange`, it would mean the wrapper / changeListBuilder gets in a bad state.

modules/javafx.base/src/main/java/com/sun/javafx/collections/ObservableListWrapper.java line 195:

> 193:     @Override
> 194:     public boolean retainAll(Collection<?> c) {
> 195:         if (backingList.isEmpty()) {

I think we need to check `c` for `null` here first to conform to the collection contract.

modules/javafx.base/src/main/java/com/sun/javafx/collections/ObservableMapWrapper.java line 328:

> 326: 
> 327:         private boolean removeRetain(Collection<?> c, boolean remove) {
> 328:             if (c.isEmpty()) {

There is an implicit `null` check here, but it makes `removeAll` and `retainAll` conform to the collection contract at least.

If you want to make this explicit (or document it), then this comment should be applied to all the `removeRetain` methods.

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

PR Review: https://git.openjdk.org/jfx/pull/751#pullrequestreview-1365073384
PR Review Comment: https://git.openjdk.org/jfx/pull/751#discussion_r1153233153
PR Review Comment: https://git.openjdk.org/jfx/pull/751#discussion_r1153235221
PR Review Comment: https://git.openjdk.org/jfx/pull/751#discussion_r1153239642


More information about the openjfx-dev mailing list