Skip to content

Eager array bridging #5488

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
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
19 changes: 6 additions & 13 deletions stdlib/public/SDK/Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,12 @@ extension Array : _ObjectiveCBridgeable {
internal init(_cocoaArray: NSArray) {
_sanityCheck(_isBridgedVerbatimToObjectiveC(Element.self),
"Array can be backed by NSArray only when the element type can be bridged verbatim to Objective-C")
// FIXME: We would like to call CFArrayCreateCopy() to avoid doing an
// objc_msgSend() for instances of CoreFoundation types. We can't do that
// today because CFArrayCreateCopy() copies array contents unconditionally,
// resulting in O(n) copies even for immutable arrays.
//
// <rdar://problem/19773555> CFArrayCreateCopy() is >10x slower than
// -[NSArray copyWithZone:]
//
// The bug is fixed in: OS X 10.11.0, iOS 9.0, all versions of tvOS
// and watchOS.
self = Array(
_immutableCocoaArray:
unsafeBitCast(_cocoaArray.copy() as AnyObject, to: _NSArrayCore.self))
self.init()
let n = _cocoaArray.count
self.reserveCapacity(n)
for i in 0..<n {
append(_cocoaArray.object(at: i) as! Element)
Copy link
Contributor

Choose a reason for hiding this comment

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

This has n objc_msgSends, perhaps we should thunk out for getObjects:range: here to just have a C function callout and a single objc msg send.

that way you would send a UnsafeMutablePointer<Unmanaged<NSObject>> across to be populated and iterate in swift with taking the unretained value back out of the buffer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't this the kind of thing that NSFastEnumeration was designed to do?

}
}

@_semantics("convertToObjectiveC")
Expand Down
Loading