Skip to content

Conversation

jmschonfeld
Copy link
Contributor

Unfortunately the mutableSpan and mutableBytes accessors on Data do not currently perform a uniqueness check to trigger CoW. This means that you can mutate a Data via its mutable span even if it references a shared storage. This updates the implementation to trigger CoW for the shared storage representations when accessing the mutable spans as well as a unit test to ensure a copy occurs when not uniquely referenced (and that no copy occurs if the storage is uniquely referenced).

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 We might want to also add a test to exercise the large slice code path.

}

@Test func validateMutation_cow_mutableBytes() {
var data = Data(Array(repeating: 0, count: 32))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have Data(count: 32) to get a zero-initialized instance?

@jmschonfeld
Copy link
Contributor Author

👍 We might want to also add a test to exercise the large slice code path.

Good call, added tests for the large slice too!

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit a8cdd87 into swiftlang:release/6.2 Oct 1, 2025
19 checks passed
@jmschonfeld jmschonfeld deleted the data-span-cow branch October 1, 2025 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants