Skip to content

[embedded] Avoid use of a metatype in array implementation for embedded Swift #68614

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
Sep 20, 2023
Merged
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
9 changes: 7 additions & 2 deletions stdlib/public/core/ArrayShared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ func _allocateUninitializedArray<Element>(_ builtinCount: Builtin.Word)
if count > 0 {
// Doing the actual buffer allocation outside of the array.uninitialized
// semantics function enables stack propagation of the buffer.
let storageType: _ContiguousArrayStorage<Element>.Type
#if !$Embedded
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it work to sink this down to getContiguousArrayStorageType()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that originally, but the function call that returns a metatype is a problem in embedded Swift. I think we could improve the MandatoryPerfOptimizations to handle that case, but I think I'd like leave that option as a possible follow-up improvement.

storageType = getContiguousArrayStorageType(for: Element.self)
#else
storageType = _ContiguousArrayStorage<Element>.self
#endif
let bufferObject = Builtin.allocWithTailElems_1(
getContiguousArrayStorageType(for: Element.self),
builtinCount, Element.self)
storageType, builtinCount, Element.self)

let (array, ptr) = Array<Element>._adoptStorage(bufferObject, count: count)
return (array, ptr._rawValue)
Expand Down
9 changes: 7 additions & 2 deletions stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,14 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
self = _ContiguousArrayBuffer<Element>()
}
else {
let storageType: _ContiguousArrayStorage<Element>.Type
#if !$Embedded
storageType = getContiguousArrayStorageType(for: Element.self)
#else
storageType = _ContiguousArrayStorage<Element>.self
#endif
_storage = Builtin.allocWithTailElems_1(
getContiguousArrayStorageType(for: Element.self),
realMinimumCapacity._builtinWordValue, Element.self)
storageType, realMinimumCapacity._builtinWordValue, Element.self)

let storageAddr = UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(_storage))
let allocSize: Int?
Expand Down