Skip to content

Explicit source file declaration in Target's sources causes a performance degradation in TargetSourcesBuilder #9304

@abdulowork

Description

@abdulowork

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

  • Confirmed reproduction steps with SwiftPM CLI. The description text must include reproduction steps with either of command-line SwiftPM commands, swift build, swift test, swift package etc.

Description

  1. Create the following Package manifest:
// swift-tools-version: 6.1
import PackageDescription

let package = Package(
    name: "perfRegression",
    platforms: [
        .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "perfRegression",
            targets: ["perfRegression"]),
    ],
    dependencies: [
        .package(url: "[email protected]:apple/swift-protobuf.git", from: "1.33.1"),
    ],
    targets: [
        .target(
            name: "perfRegression",
            dependencies: [
                .product(name: "SwiftProtobuf", package: "swift-protobuf"),
            ]
        ),
    ]
)
  1. Clean build once: swift build --product perfRegression
  2. Remove intermediates rm -rf .build/debug and build again swift build --product perfRegression --verbose

On each incremental build with .build/debug removed, there will be a noticeable lag before swift build starts producing output. An equivalent issue is reproducible using xcodebuild build -scheme perfRegression.

From the sample swift-build output, a particularly hot path is:

16273 AbsolutePath.isDescendantOfOrEqual(to:) (in swift-package-manager) + 104 [0x1008cddc4] AbsolutePath.swift:247

And debugging with print statements here, I see that during TargetSourcesBuilder.run call, SwiftPM checks whether each file in the protobuf target is a subpath of all the other files in the same target.

Downgrading swift-protobuf to 1.32.0 solves the performance regression, and it seems this regression was introduced by explicit specification of all sources in swift-protobuf in this commit / issue

Expected behavior

From Target's documentation, it follows that you are allowed to specify sources at the individual file level, and there should be no performance degradations

Actual behavior

Specifying individual files in Target's sources noticeably impacts the build system's performance

Steps to reproduce

Please see the steps above

Swift Package Manager version/commit hash

swift-6.2-RELEASE / 3cf1cb6

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx15.0
Darwin NVC00511 24.4.0 Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6000 arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions