-
Notifications
You must be signed in to change notification settings - Fork 204
[5.5, NFC, Incremental] Create a domain-specific type for the inputDependencySourceMap #710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//===------------- InputDependencySourceMap.swift ---------------- --------===// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import Foundation | ||
import TSCBasic | ||
|
||
@_spi(Testing) public struct InputDependencySourceMap: Equatable { | ||
|
||
/// Maps input files (e.g. .swift) to and from the DependencySource object. | ||
/// | ||
// FIXME: The map between swiftdeps and swift files is absolutely *not* | ||
// a bijection. In particular, more than one swiftdeps file can be encountered | ||
// in the course of deserializing priors *and* reading the output file map | ||
// *and* re-reading swiftdeps files after frontends complete | ||
// that correspond to the same swift file. These cause two problems: | ||
// - overwrites in this data structure that lose data and | ||
// - cache misses in `getInput(for:)` that cause the incremental build to | ||
// turn over when e.g. entries in the output file map change. This should be | ||
// replaced by a multi-map from swift files to dependency sources, | ||
// and a regular map from dependency sources to swift files - | ||
// since that direction really is one-to-one. | ||
|
||
public typealias BiMap = BidirectionalMap<TypedVirtualPath, DependencySource> | ||
@_spi(Testing) public var biMap = BiMap() | ||
|
||
private let simulateGetInputFailure: Bool | ||
|
||
init(simulateGetInputFailure: Bool) { | ||
self.simulateGetInputFailure = simulateGetInputFailure | ||
} | ||
} | ||
|
||
// MARK: - Accessing | ||
extension InputDependencySourceMap { | ||
@_spi(Testing) public func sourceIfKnown(for input: TypedVirtualPath) -> DependencySource? { | ||
biMap[input] | ||
} | ||
|
||
@_spi(Testing) public func inputIfKnown(for source: DependencySource) -> TypedVirtualPath? { | ||
simulateGetInputFailure ? nil : biMap[source] | ||
} | ||
|
||
@_spi(Testing) public func enumerateToSerializePriors( | ||
_ eachFn: (TypedVirtualPath, DependencySource) -> Void | ||
) { | ||
biMap.forEach(eachFn) | ||
} | ||
} | ||
|
||
Comment on lines
+41
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would very much prefer we not conflate the point of use with the names of these APIs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please elaborate? I want to be sure I understand the point here. What would you suggest instead? If that is the source of disagreement, here is a nice quote I found that explains what I am trying to accomplish:
|
||
// MARK: - Populating | ||
extension InputDependencySourceMap { | ||
public enum AdditionPurpose { | ||
/// For testing: | ||
case mocking | ||
/// When building from a `swiftdeps` file: | ||
case buildingFromSwiftDeps | ||
/// When deserializing the map from a prior build: | ||
case readingPriors | ||
/// Adding an entry for an input added to the build since the priors were stored: | ||
case inputsAddedSincePriors | ||
} | ||
@_spi(Testing) public mutating func addEntry(_ input: TypedVirtualPath, | ||
_ dependencySource: DependencySource, | ||
`for` _ : AdditionPurpose) { | ||
assert(input.type == .swift && dependencySource.typedFile.type == .swiftDeps) | ||
biMap[input] = dependencySource | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a format part of the API? We know how to create this failure mode in tests for the current implementation, and the new one should structurally avoid the issue altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simulateGetInputFailure
is gone on the main branch. It remains here in the interest of minimal changes to this branch. If you insist, I'm happy with removing it. Whatever the folks in charge of this branch prefer.