You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This creates a ClosedRange from the CollectionstartIndex and endIndex with the ..< operator which also checks for upperBound >= lowerBound which is always true since 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.
The text was updated successfully, but these errors were encountered:
Additional Detail from JIRA
md5: e5fa71a2300e8f29150d9c980cc4d71f
Issue Description:
Several default implementations in
Collection
andBidirectionalCollection
are using a bound check that seems sub-optimal.For instance here in
Collection
, the slice subscript default implementation:This creates a
ClosedRange
from theCollection
startIndex
andendIndex
with the..<
operator which also checks forupperBound >= lowerBound
which is always true sincestartIndex <= endIndex
.This check could be optimized in the same way as it is done in
RandomAccessCollection
default implementations, i.e. with theuncheckedBounds
init: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 returnsstartIndex = 10
andendIndex = 0
. IMO this kind of init is purposely unsafe and should not hamper performance elsewhere.EDIT: The issue is also present in
StrideTo
andStrideThrough
.The text was updated successfully, but these errors were encountered: