Skip to content

Commit

Permalink
Improve resolveBuildProducts performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu0118 committed Jul 10, 2024
1 parent 49e534d commit 55311c9
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions Sources/ScipioKit/DescriptionPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,50 @@ struct DescriptionPackage {
}

extension DescriptionPackage {
func resolveBuildProducts() throws -> [BuildProduct] {
}
}

struct BuildProduct: Hashable, Sendable {
var package: ResolvedPackage
var target: ScipioResolvedModule

var frameworkName: String {
"\(target.name.packageNamed()).xcframework"
}

var binaryTarget: ScipioBinaryModule? {
target.underlying as? ScipioBinaryModule
}

func hash(into hasher: inout Hasher) {
// Important: Relevant for swift-6.0+ toolchain versions. For the versions below
// this change has no effect as SwiftPM provides its own proper `Hashable`
// implementations for both `ResolvedPackage` and `ResolvedTarget`.
//
// We cannot directly use `ResolvedModule.id` here as `id` also includes `BuildTriple`.
// The reason for this is that `ResolvedModule.buildTriple` is parent-dependent; more
// specifically, the same `ResolvedModule` will have a different build triple depending
// on whether it is in a root or dependency position.
// For more context, see `ResolvedModule.updateBuildTriplesOfDependencies`.
//
// At the same time, build triples remain irrelevant for the `Scipio` use case where the
// build product must be the same regardless of the triple. Meanwhile, the target name and
// package identity remain relevant and unambiguously identify the build product.
hasher.combine(target.name)
hasher.combine(package.identity)
}
}


private final class BuildProductsResolver {
private var visitedTargets: Set<ResolvedTarget> = []
let descriptionPackage: DescriptionPackage

init(descriptionPackage: DescriptionPackage) {
self.descriptionPackage = descriptionPackage
}

func resolveBuildProducts() throws -> [BuildProduct] {
let targetsToBuild = try targetsToBuild()
var products = try targetsToBuild.flatMap(resolveBuildProduct(from:))
Expand Down Expand Up @@ -254,34 +298,3 @@ extension DescriptionPackage {
return Set([rootTargetProduct] + dependencyProducts)
}
}

struct BuildProduct: Hashable, Sendable {
var package: ResolvedPackage
var target: ScipioResolvedModule

var frameworkName: String {
"\(target.name.packageNamed()).xcframework"
}

var binaryTarget: ScipioBinaryModule? {
target.underlying as? ScipioBinaryModule
}

func hash(into hasher: inout Hasher) {
// Important: Relevant for swift-6.0+ toolchain versions. For the versions below
// this change has no effect as SwiftPM provides its own proper `Hashable`
// implementations for both `ResolvedPackage` and `ResolvedTarget`.
//
// We cannot directly use `ResolvedModule.id` here as `id` also includes `BuildTriple`.
// The reason for this is that `ResolvedModule.buildTriple` is parent-dependent; more
// specifically, the same `ResolvedModule` will have a different build triple depending
// on whether it is in a root or dependency position.
// For more context, see `ResolvedModule.updateBuildTriplesOfDependencies`.
//
// At the same time, build triples remain irrelevant for the `Scipio` use case where the
// build product must be the same regardless of the triple. Meanwhile, the target name and
// package identity remain relevant and unambiguously identify the build product.
hasher.combine(target.name)
hasher.combine(package.identity)
}
}

0 comments on commit 55311c9

Please sign in to comment.