Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-13824] Some Collection default implementations have a redundant bound check #56222

Open
swift-ci opened this issue Nov 4, 2020 · 1 comment
Labels
improvement standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 4, 2020

Previous ID SR-13824
Radar rdar://problem/71040553
Original Reporter laclouis5 (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement
Assignee None
Priority Medium

md5: e5fa71a2300e8f29150d9c980cc4d71f

Issue Description:

Several default implementations in Collection and BidirectionalCollection are using a bound check that seems sub-optimal.

For instance here in Collection, the slice subscript default implementation:

_failEarlyRangeCheck(bounds, bounds: startIndex..<endIndex)

This creates a ClosedRange from the Collection startIndex and endIndex with the ..< operator which also checks for upperBound >= lowerBound which is always true since startIndex <= endIndex.

This check could be optimized in the same way as it is done in RandomAccessCollection default implementations, i.e. with the uncheckedBounds init:

_failEarlyRangeCheck(i, bounds: Range(uncheckedBounds: (startIndex, endIndex)))

There are other places where this optimization could be done (here, here, here, ... but also in BidirectionalCollection) since the bound check is already performed before.

However, this may not be a bug but intended as some (rare) collections can be initialized in an illformed state, like let range = Range(uncheckedBounds: (10, 0)) which then returns startIndex = 10 and endIndex = 0. IMO this kind of init is purposely unsafe and should not hamper performance elsewhere.

EDIT: The issue is also present in StrideTo and StrideThrough.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants