Skip to content

[SR-4164] Consider treating LazyFilteredCollection like FlattenCollection #46747

@dabrahams

Description

@dabrahams
Previous ID SR-4164
Radar None
Original Reporter @dabrahams
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug
Assignee Hektve87 (JIRA)
Priority Medium

md5: e621a9d1963ce2fe4205f377c6b2fb16

Issue Description:

The refusal to pre-count elements of a `FlattenCollection` is based on reasoning about the performance of `flatMap`. But `flatMap`, when applied to collections of optionals, doesn't produce a LazyFlattenedCollection<…>, but a LazyMapCollection<LazyFilterCollection<…>>. We are not doing a similar thing for LazyFilterCollection.

The following demonstrates:

struct Foo {
  let i: UInt ;
  init?(_ i: UInt) { print("Foo(\(i))"); if i % 4 == 0 { return nil }; self.i = i }
}
Array((0..<10).lazy.flatMap(Foo.init))

which currently generates Foo(i) 3 times for each i in 0..<10. If this FIXME were addressed it would go down to 2 times (once for counting), but if we take the option suggested in this bug, it would go down to 1 (at the cost of Array reallocations due to failing to pre-count, of course).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrella

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions