On Wed, 4 Nov 2020 09:21:12 GMT, Tagir F. Valeev <tval...@openjdk.org> wrote:

>> src/java.base/share/classes/java/util/stream/Stream.java line 1192:
>> 
>>> 1190:     @SuppressWarnings("unchecked")
>>> 1191:     default List<T> toList() {
>>> 1192:         return (List<T>) Collections.unmodifiableList(new 
>>> ArrayList<>(Arrays.asList(this.toArray())));
>> 
>> Why can't we return `listFromTrustedArrayNullsAllowed` here as in 
>> `ReferencePipeline`?
>> Or at least, we should avoid unnecessary copying of arrays. See [how this is 
>> done](https://github.com/amaembo/streamex/blob/master/src/main/java/one/util/streamex/AbstractStreamEx.java#L1313)
>>  in StreamEx.
>
> `listFromTrustedArrayNullsAllowed` is clearly not an option, as it will 
> produce shared-secret leaking (see 
> [JDK-8254090](https://bugs.openjdk.java.net/browse/JDK-8254090) for a similar 
> case). StreamEx solution is dirty as it relies on the implementation detail. 
> I believe, OpenJDK team is not very interested in providing optimal 
> implementations for third-party stream implementations, as third-party 
> implementations will likely update by themselves when necessary. At least, my 
> suggestion to make the default `mapMulti` implementation better was declined.

This implementation duplicates the array twice, once in this.toArray() and once 
in the constructor of ArrayList that calls toArray() on Collections.ArrayList.

I'm sure there is a better way

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

PR: https://git.openjdk.java.net/jdk/pull/1026

Reply via email to