Why is __swift_instantiateGenericMetadata showing up here?

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.

  1. generic specialization <[Swift.Double]> of (extension in Fraction):Swift.Collection< where A.Element: Swift.BinaryFloatingPoint>.distribute(Swift.Int64) -> [Swift.Int64]?

  2. Swift._ContiguousArrayBuffer.init(_uninitializedCount: Swift.Int, minimumCapacity: Swift.Int) -> Swift._ContiguousArrayBuffer<A>

  3. __swift_instantiateGenericMetadata

  4. swift_getGenericMetadata

what is going on here?

The specialized Collection.distribute is calling an unspecialized _ContiguousArrayBuffer.init. Unfortunate, perhaps, but not surprising…and not avoidable in non-embedded Swift, because the resulting array buffer object instance stores its type inside itself like any other class instance. (To my knowledge, the difference with embedded Swift is the compiler can make that type up front, knowing that it will never be constructed or linked dynamically.)

how come i don’t see this elsewhere when i write code that allocates an [Int64]?

You do, if the [Int64] escapes. Or rather, you get the equivalent in swift_getTypeByMangledNameInContext2. However, if the question is why it doesn't show up in profiles, we'd probably have to see the implementations of distribute to know why it's picking a different strategy that's maybe less cacheable—as you say, the [Int64] is a concrete type all the time, not dependent on the input.