On Sat, 12 Mar 2022 04:57:37 GMT, Michael Strauß <[email protected]> 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.
Not sure if you want to do this as part of this fix, but
`ModifiableObservableListBase#addAll` can also be optimized in a similar way by
checking if `c` is empty. The other methods there can also be optimized, but
the `ObservableListWrappe` class overrides them anyway, so it depends on how
inclusive you want this change to be.
modules/javafx.base/src/main/java/com/sun/javafx/collections/ObservableSetWrapper.java
line 359:
> 357:
> 358: return false;
> 359: }
This is good, but if `!c.isEmpty()` then we can optimize too, I think: if
`backingSet.isEmpty()`, then removing/retaining all will also return `false`.
Then again, the iterator will return quickly, so it might not do much.
If you take this path, I think that doing these optimizations in the
`removeAll` and `retainAll` before calling this method will be clearer,
similarly to how it's done in the `List` case.
Same comment for the `Map` wrapper.
-------------
PR: https://git.openjdk.org/jfx/pull/751