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 issue is related to SR-11479. Currently enumerated() returns an EnumeratedSequence lazy evaluated struct. This struct is a sequence, even if Base is a Collection such as an Array, so it does not preserve the sequence type and associated performances of the underlying sequence type (Base).
For instance the following code does not work as expected. Because enumerated()
returns a Sequence, reversed() is forced to create a new array instead of a ReversedCollection:
// `elements` is an Array<(Int, String)>letelements=["a","b","c"].enumerated().reversed()
This is a performance issue in for-loops using the above pattern as the operation is now O👎 instead of O(1) and allocated storage.
This also cause a cause a gap in the API, for example it is currently impossible to subscript or get the last element efficiently.
Additional Detail from JIRA
md5: 2b6c41ac1cc8a67e9b18be3b820d1805
Issue Description:
This issue is related to SR-11479. Currently
enumerated()
returns anEnumeratedSequence
lazy evaluated struct. This struct is a sequence, even ifBase
is aCollection
such as anArray
, so it does not preserve the sequence type and associated performances of the underlying sequence type (Base
).For instance the following code does not work as expected. Because
enumerated()
returns a
Sequence
,reversed()
is forced to create a new array instead of aReversedCollection
:This is a performance issue in for-loops using the above pattern as the operation is now
O👎
instead ofO(1)
and allocated storage.This also cause a cause a gap in the API, for example it is currently impossible to subscript or get the last element efficiently.
An implementation of a fix could be this:
Another related issue:
EnumeratedSequence
does not forward laziness likeReverseCollection
does which is in my opinion not expected:Can fix code like this:
The text was updated successfully, but these errors were encountered: