I tried to used some @inlinable and @usableFromInline in a SwiftUI library but found that things like @Observable and @State are not friendly for using inline attributes:
@Observable
final class MyModel {
@usableFromInline var myState = 0
// Expanded to something like `@usableFromInline private var`
}
Since I used generics in Views and there might be some heavy computations in models, I do want to keep the call stack inlinable. Is there a better approach for this?
@inlineable isn’t always preferred. It can allow for performance improvements, specifically around Library functions with Generics or closures. But annotating every function with @inlineable will increase binary size without necessarily improving performance.
This post does a good job explaining @inlineable and @usableFromInline.
Hi Michael, I was developing a UI library, and the view model contains generics and heavy computations, which is very slow if I don't keep things @inlinable. Say, I have a view for rendering graph:
@Observable
class MyGraphModel<NodeID: Hashable, F> where ... {
// this could be very slow if not inlinable
let simulationContext: Simulation<SIMD2<Double>, F, NodeID>
}
Since it seems like using inline attributes basically breaks almost all state management magics in SwiftUI, I was wondering if this is discouraged for ui libraries. Or do you have any suggestions for handling such situations? Thanks!