----- Mail original ----- > De: "Peter Levart" <peter.lev...@gmail.com> > À: "Stuart Marks" <stuart.ma...@oracle.com> > Cc: "core-libs-dev" <core-libs-dev@openjdk.java.net> > Envoyé: Mercredi 12 Mai 2021 08:28:07 > Objet: Re: [External] : Re: ReversibleCollection proposal
> On 5/12/21 2:55 AM, Stuart Marks wrote: >> As you point out, add() is kind of like addLast(), except without the >> reordering semantics for LinkedHashSet. And reversed().add() is a >> roundabout way of saying addFirst() -- also without the reordering >> semantics for LinkedHashSet. I think most people's reactions would be >> "Why didn't they just provide addFirst/addLast?" Plus the reordering >> would be missing for LHS. >> >> A second-order issue is performance. I'd expect that implementations >> would want to provide a fast-path for addFirst() that is amenable to >> JIT optimization; this seems harder to achieve with reversed().add(). > > > The allocation of a reversed view instance typically goes away when C2 > compiles the method (if the instance isn't cached like in > AbstractMap.keySet/values) so this can be as performant as specialized > addFirst(), but lack of reordering of existent element in LinkedHashSet > is a different problem I haven thought about. Don't forget that the default method implementation is just that a default method, at least the JDK implementations like LinkedHashSet can/should provide a better implementation. > > > Regards, Peter regards, Rémi