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-13879] Dictionary.Indices retains dictionary, causes CoW on mutation during loop #56277

Closed
lilyball mannequin opened this issue Nov 19, 2020 · 3 comments
Closed

[SR-13879] Dictionary.Indices retains dictionary, causes CoW on mutation during loop #56277

lilyball mannequin opened this issue Nov 19, 2020 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Nov 19, 2020

Previous ID SR-13879
Radar rdar://problem/71677707
Original Reporter @lilyball
Type Bug
Status Closed
Resolution Invalid
Environment

Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
Target: x86_64-apple-darwin19.6.0

Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 777a286e1be306db5fbbabc110c4b471

relates to:

  • SR-13888 Dictionary iteration by keys forces copy-on-write for struct elements

Issue Description:

If I'm iterating over a dictionary's indices and mutating the dictionary in the loop, it does a copy-on-write on the first mutation. This is a surprising and hard to discover. I verified this by printing out the variant's native instance address in LLDB to confirm reallocation.

var dict = ["one": 1, "two": 2]
for idx in dict.indices {
    dict.values[idx] += 1
}

Comparing the dict._variant.object.nativeInstance before and after the loop shows different addresses.

This is a consequence of the fact that Dictionary uses DefaultIndices, and DefaultIndices keeps a reference to the collection.

More generally, DefaultIndices does not seem to be safe to use with any mutable collection as there is no way for it to walk the collection's indices without keeping a strong reference to the collection.

@typesanitizer
Copy link

@swift-ci create

@swift-ci
Copy link
Collaborator

Comment by Kyle Macomber (JIRA)

Unfortunately, Dictionary's indices need to retain the collection in order to be able to advance the indices and there's no way (we can think of) to avoid it.

@typesanitizer
Copy link

kylemacomber (JIRA User) Should/Can we update the documentation to reflect this potential performance hazard?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants