i have a function, let’s call it costs, which lives in CModule, and is not inlinable.
import Fraction
import RealModule
// `C` is non-generic
extension C {
public mutating func costs(
funds available: Int64,
segmented: Int64,
tradeable: Int64,
) {
let totalCost: Int64 = tradeable + segmented
let items: [Int64]? = [
Double.sqrt(Double.init(tradeable)),
Double.sqrt(Double.init(segmented))
].distribute(min(totalCost, available))
if let items: [Int64] {
self.tradeable += items[0]
self.segmented += items[1]
}
}
}
it calls a generic function on Collection, called distribute(_:), which lives in another module, Fraction,
extension Collection where Element: BinaryFloatingPoint {
@inlinable public func distribute(_ funds: Int64) -> [Int64]? {
self.distribute(funds) { $0 }
}
}
the entire call stack of distribute(_:) is inlinable.
extension Collection {
// Collection.distribute(_:) calls this:
@inlinable public func distribute(
_ funds: Int64,
share: (Element) -> some BinaryFloatingPoint
) -> [Int64]?
// ... which calls this:
@inlinable func distribute<Share>(
share: (Element) -> Share,
funds: (Share) -> Int64,
) -> [Int64]? where Share: BinaryFloatingPoint
// ...which finally calls this:
@inlinable func distribute<Share>(
_ funds: Int64,
shares: Share,
share: (Element) -> Share
) -> [Int64] where Share: BinaryFloatingPoint
}
the C.costs method is being called from code external to both CModule and Fraction. when i compile (for WebAssembly) with optimizations enabled, i see __swift_instantiateGenericMetadata popping up in the sampled call stacks.
strangely, these calls are inside generic specializations, and i don’t understand why a specialization is still interacting with runtime type metadata.
-
generic specialization <[Swift.Double]> of (extension in Fraction):Swift.Collection< where A.Element: Swift.BinaryFloatingPoint>.distribute(Swift.Int64) -> [Swift.Int64]? -
Swift._ContiguousArrayBuffer.init(_uninitializedCount: Swift.Int, minimumCapacity: Swift.Int) -> Swift._ContiguousArrayBuffer<A> -
__swift_instantiateGenericMetadata -
swift_getGenericMetadata
what is going on here?