Skip to content

Fix the new Swift NSMutableArray subclass to match edge case behavior in two more cases #27504

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

Merged
merged 1 commit into from
Oct 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions stdlib/public/core/SwiftNativeNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ extension __SwiftNativeNSArrayWithContiguousStorage: _NSArrayCore {

@objc(exchangeObjectAtIndex:withObjectAtIndex:)
dynamic internal func exchange(at index: Int, with index2: Int) {
swap(&contents[index], &contents[index2])
contents.swapAt(index, index2)
}

@objc(replaceObjectsInRange:withObjects:count:)
Expand All @@ -263,7 +263,13 @@ extension __SwiftNativeNSArrayWithContiguousStorage: _NSArrayCore {
count: Int) {
let range = range.location ..< range.location + range.length
let buf = UnsafeBufferPointer(start: objects, count: count)
contents.replaceSubrange(range, with: buf)
if range == contents.startIndex..<contents.endIndex {
contents = Array(buf)
} else {
// We make an Array here to make sure that something is holding onto the
// objects in `buf`, since replaceSubrange could release them
Copy link
Contributor

Choose a reason for hiding this comment

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

Oof. Optimize later to check, I guess?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it looks relevant in practice, sure

Copy link
Contributor

Choose a reason for hiding this comment

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

You're performing an extra copy this way, at least formally.

Copy link
Contributor

Choose a reason for hiding this comment

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

(which is the point, of course)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, but that just brings us down to NSMutableArray's speed, in theory. Anything better here would be a win on top of that which (probably) makes it nice to have rather than necessary.

contents.replaceSubrange(range, with: Array(buf))
}
}

@objc(insertObjects:count:atIndex:)
Expand Down