Skip to content

[SR-425] Indexable documents startIndex as O(1) but this isn't true for some lazy collections #43042

@lilyball

Description

@lilyball
mannequin
Previous ID SR-425
Radar None
Original Reporter @lilyball
Type Bug
Status Closed
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 44131a346b66e07109ccf34858455fc1

duplicates:

  • SR-428 Implement new performance terminology in documentation

Issue Description:

Indexable documents the startIndex property as having a Complexity of O(1), but LazyFilterCollection and FlattenCollection don't obey this rule. Accessing the startIndex on either collection runs the predicate up until it finds the first included element. And it does this every time you access startIndex. So that makes it O(N) at least (and a pathological predicate that captures a copy of the same underlying collection can make it arbitrarily expensive).

Luckily endIndex should be able to remain O(1) because collections can use a constant sentinel value instead of actually calculating anything (I haven't checked but I assume the lazy collection types already use a sentinel value for this).

There's two ways this can be fixed:

1. Modify LazyFilterCollection and FlattenCollection to aggressively calculate the index of the first element when they're created. But I think this would be surprising to have lazy collections run the predicate immediately, so I'd rather go with
2. Change the documentation of Indexable.startIndex to say it's O(1) in most cases, but potentially O(N) for lazy collections.

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