[swift-evolution] [Review] Constraining AnySequence.init

Dmitri Gribenko gribozavr at gmail.com
Sat Dec 19 00:20:46 CST 2015


On Fri, Dec 18, 2015 at 10:08 PM, Kevin Ballard <kevin at sb.org> wrote:
> Well actually...
>
> LazyCollection right now has just this very behavior. The SubSequence of a
> LazyCollection<T> is a LazyCollection<Slice<T>>. If you say
> `[1,2,3].lazy.prefixUpTo(3).prefixUpTo(3)` you end up with a
> LazyCollection<Slice<Slice<[Int]>>>. So the only way we can constrain
> SubSequence this way is by also making it possible for LazyCollection to
> "flatten" the slices.

Indeed, thanks for noticing this!  Multiple levels of Slice<T>
wrapping is bad not just because of the type, but because each Slice
has its own index bounds stored, and every access through a Slice
needs to perform a range check.  So having multiple levels of wrapping
would affect performance in this case.

If we tested LazyCollection with `addForwardCollectionTests()` from
StdlibUnittest, we would have noticed.

The fix might be here:

// stdlib/public/core/LazyCollection.swift
public subscript(bounds: Range<Index>) -> LazyCollection<Slice<Base>>
>>>
public subscript(bounds: Range<Index>) -> LazyCollection<Base.SubSequence>

(Haven't compiled it.)

I opened https://bugs.swift.org/browse/SR-318 for this issue.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list