Skip to content

Commit fb2a4bb

Browse files
committed
Remove job from build keys. Saves encoding and decoding the jobs.
The runtime of the following (in release build) went from 2.27 minutes to 2.29 seconds (notice the change in units!!!) // RUN: %{python} -c 'for i in range(500001): print("-DTEST5_" + str(i))' > %t.5.resp // RUN: not %target-swiftc_driver %s @%t.5.resp -Xfrontend -debug-crash-immediately 2>&1
1 parent 20ada01 commit fb2a4bb

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

Sources/SwiftDriver/Execution/JobExecutor.swift

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ public final class JobExecutor {
118118
/// The context required during job execution.
119119
struct Context {
120120

121-
/// This contains mapping from an output to the job that produces that output.
122-
let producerMap: [VirtualPath: Job]
121+
/// This contains mapping from an output to the index(in the jobs array) of the job that produces that output.
122+
let producerMap: [VirtualPath: Int]
123+
124+
/// All the jobs being executed.
125+
let jobs: [Job]
123126

124127
/// The resolver for argument template.
125128
let argsResolver: ArgsResolver
@@ -152,14 +155,16 @@ public final class JobExecutor {
152155
argsResolver: ArgsResolver,
153156
env: [String: String],
154157
fileSystem: FileSystem,
155-
producerMap: [VirtualPath: Job],
158+
producerMap: [VirtualPath: Int],
159+
jobs: [Job],
156160
executorDelegate: JobExecutorDelegate,
157161
jobQueue: OperationQueue,
158162
processSet: ProcessSet?,
159163
forceResponseFiles: Bool,
160164
recordedInputModificationDates: [TypedVirtualPath: Date]
161165
) {
162166
self.producerMap = producerMap
167+
self.jobs = jobs
163168
self.argsResolver = argsResolver
164169
self.env = env
165170
self.fileSystem = fileSystem
@@ -217,7 +222,7 @@ public final class JobExecutor {
217222
let delegate = JobExecutorBuildDelegate(context)
218223
let engine = LLBuildEngine(delegate: delegate)
219224

220-
let result = try engine.build(key: ExecuteAllJobsRule.RuleKey(jobs: jobs))
225+
let result = try engine.build(key: ExecuteAllJobsRule.RuleKey())
221226

222227
// Throw the stub error the build didn't finish successfully.
223228
if !result.success {
@@ -227,11 +232,11 @@ public final class JobExecutor {
227232

228233
/// Create the context required during the execution.
229234
func createContext(_ jobs: [Job], env: [String: String], fileSystem: FileSystem) -> Context {
230-
var producerMap: [VirtualPath: Job] = [:]
231-
for job in jobs {
235+
var producerMap: [VirtualPath: Int] = [:]
236+
for (index, job) in jobs.enumerated() {
232237
for output in job.outputs {
233238
assert(!producerMap.keys.contains(output.file), "multiple producers for output \(output): \(job) \(producerMap[output.file]!)")
234-
producerMap[output.file] = job
239+
producerMap[output.file] = index
235240
}
236241
}
237242

@@ -244,6 +249,7 @@ public final class JobExecutor {
244249
env: env,
245250
fileSystem: fileSystem,
246251
producerMap: producerMap,
252+
jobs: jobs,
247253
executorDelegate: executorDelegate,
248254
jobQueue: jobQueue,
249255
processSet: processSet,
@@ -264,7 +270,7 @@ struct JobExecutorBuildDelegate: LLBuildEngineDelegate {
264270
func lookupRule(rule: String, key: Key) -> Rule {
265271
switch rule {
266272
case ExecuteAllJobsRule.ruleName:
267-
return ExecuteAllJobsRule(key, fileSystem: context.fileSystem)
273+
return ExecuteAllJobsRule(key, jobs: context.jobs, fileSystem: context.fileSystem)
268274
case ExecuteJobRule.ruleName:
269275
return ExecuteJobRule(key, context: context)
270276
default:
@@ -294,26 +300,26 @@ class ExecuteAllJobsRule: LLBuildRule {
294300
struct RuleKey: LLBuildKey {
295301
typealias BuildValue = DriverBuildValue
296302
typealias BuildRule = ExecuteAllJobsRule
297-
298-
let jobs: [Job]
299303
}
300304

301305
override class var ruleName: String { "\(ExecuteAllJobsRule.self)" }
302306

303307
private let key: RuleKey
308+
private let jobs: [Job]
304309

305310
/// True if any of the inputs had any error.
306311
private var allInputsSucceeded: Bool = true
307312

308-
init(_ key: Key, fileSystem: FileSystem) {
313+
init(_ key: Key, jobs: [Job], fileSystem: FileSystem) {
309314
self.key = RuleKey(key)
315+
self.jobs = jobs
310316
super.init(fileSystem: fileSystem)
311317
}
312318

313319
override func start(_ engine: LLTaskBuildEngine) {
314-
for (idx, job) in key.jobs.enumerated() {
315-
let key = ExecuteJobRule.RuleKey(job: job)
316-
engine.taskNeedsInput(key, inputID: idx)
320+
for index in jobs.indices {
321+
let key = ExecuteJobRule.RuleKey(index: index)
322+
engine.taskNeedsInput(key, inputID: index)
317323
}
318324
}
319325

@@ -340,7 +346,7 @@ class ExecuteJobRule: LLBuildRule {
340346
typealias BuildValue = DriverBuildValue
341347
typealias BuildRule = ExecuteJobRule
342348

343-
let job: Job
349+
let index: Int
344350
}
345351

346352
override class var ruleName: String { "\(ExecuteJobRule.self)" }
@@ -358,9 +364,9 @@ class ExecuteJobRule: LLBuildRule {
358364
}
359365

360366
override func start(_ engine: LLTaskBuildEngine) {
361-
for (idx, input) in key.job.inputs.enumerated() {
362-
if let producingJob = context.producerMap[input.file] {
363-
let key = ExecuteJobRule.RuleKey(job: producingJob)
367+
for (idx, input) in context.jobs[key.index].inputs.enumerated() {
368+
if let producingJobIndex = context.producerMap[input.file] {
369+
let key = ExecuteJobRule.RuleKey(index: producingJobIndex)
364370
engine.taskNeedsInput(key, inputID: idx)
365371
}
366372
}
@@ -393,7 +399,7 @@ class ExecuteJobRule: LLBuildRule {
393399
private func executeJob(_ engine: LLTaskBuildEngine) {
394400
let context = self.context
395401
let resolver = context.argsResolver
396-
let job = key.job
402+
let job = context.jobs[key.index]
397403
let env = context.env.merging(job.extraEnvironment, uniquingKeysWith: { $1 })
398404

399405
let value: DriverBuildValue

0 commit comments

Comments
 (0)