Skip to content

Commit 73d1ddc

Browse files
authored
reduce use of byte string (#6497)
1 parent 6bef84c commit 73d1ddc

File tree

42 files changed

+809
-916
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+809
-916
lines changed

Sources/Basics/FileSystem/FileSystem+Extensions.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,44 @@ extension FileSystem {
288288

289289
extension FileSystem {
290290
public func readFileContents(_ path: AbsolutePath) throws -> Data {
291-
return try Data(self.readFileContents(path).contents)
291+
try Data(self.readFileContents(path).contents)
292292
}
293293

294294
public func readFileContents(_ path: AbsolutePath) throws -> String {
295-
return try String(decoding: self.readFileContents(path), as: UTF8.self)
295+
try String(decoding: self.readFileContents(path), as: UTF8.self)
296296
}
297297

298298
public func writeFileContents(_ path: AbsolutePath, data: Data) throws {
299-
return try self.writeFileContents(path, bytes: .init(data))
299+
try self._writeFileContents(path, bytes: .init(data))
300300
}
301301

302302
public func writeFileContents(_ path: AbsolutePath, string: String) throws {
303-
return try self.writeFileContents(path, bytes: .init(encodingAsUTF8: string))
303+
try self._writeFileContents(path, bytes: .init(encodingAsUTF8: string))
304+
}
305+
306+
private func _writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
307+
// using the "body" variant since it creates the directory first
308+
// we should probably fix TSC to be consistent about this behavior
309+
try self.writeFileContents(path, body: { $0.send(bytes) })
310+
}
311+
}
312+
313+
extension FileSystem {
314+
/// Write bytes to the path if the given contents are different.
315+
public func writeIfChanged(path: AbsolutePath, string: String) throws {
316+
try writeIfChanged(path: path, bytes: .init(encodingAsUTF8: string))
317+
}
318+
319+
/// Write bytes to the path if the given contents are different.
320+
public func writeIfChanged(path: AbsolutePath, bytes: ByteString) throws {
321+
try createDirectory(path.parentDirectory, recursive: true)
322+
323+
// Return if the contents are same.
324+
if isFile(path), try readFileContents(path) == bytes {
325+
return
326+
}
327+
328+
try writeFileContents(path, bytes: bytes)
304329
}
305330
}
306331

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ public final class ClangTargetBuildDescription {
309309
// Compute the basename of the bundle.
310310
let bundleBasename = bundlePath.basename
311311

312-
let implFileStream = BufferedOutputByteStream()
313-
implFileStream.send(
312+
let implContent =
314313
"""
315314
#import <Foundation/Foundation.h>
316315
@@ -325,7 +324,6 @@ public final class ClangTargetBuildDescription {
325324
return preferredBundle;
326325
}
327326
"""
328-
)
329327

330328
let implFileSubpath = try RelativePath(validating: "resource_bundle_accessor.m")
331329

@@ -336,11 +334,10 @@ public final class ClangTargetBuildDescription {
336334
// FIXME: We should generate this file during the actual build.
337335
try fileSystem.writeIfChanged(
338336
path: derivedSources.root.appending(implFileSubpath),
339-
bytes: implFileStream.bytes
337+
string: implContent
340338
)
341339

342-
let headerFileStream = BufferedOutputByteStream()
343-
headerFileStream.send(
340+
let headerContent =
344341
"""
345342
#import <Foundation/Foundation.h>
346343
@@ -356,13 +353,13 @@ public final class ClangTargetBuildDescription {
356353
}
357354
#endif
358355
"""
359-
)
356+
360357
let headerFile = derivedSources.root.appending("resource_bundle_accessor.h")
361358
self.resourceAccessorHeaderFile = headerFile
362359

363360
try fileSystem.writeIfChanged(
364361
path: headerFile,
365-
bytes: headerFileStream.bytes
362+
string: headerContent
366363
)
367364
}
368365
}

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,16 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
328328

329329
/// Writes link filelist to the filesystem.
330330
func writeLinkFilelist(_ fs: FileSystem) throws {
331-
let stream = BufferedOutputByteStream()
331+
var content = self.objects
332+
.map { $0.pathString.spm_shellEscaped() }
333+
.joined(separator: "\n")
332334

333-
for object in self.objects {
334-
stream.send("\(object.pathString.spm_shellEscaped())\n")
335+
// not sure this is needed, added here for backward compatibility
336+
if !content.isEmpty {
337+
content.append("\n")
335338
}
336339

337-
try fs.createDirectory(self.linkFileListPath.parentDirectory, recursive: true)
338-
try fs.writeFileContents(self.linkFileListPath, bytes: stream.bytes)
340+
try fs.writeFileContents(self.linkFileListPath, string: content)
339341
}
340342

341343
/// Returns the build flags from the declared build settings.

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -308,29 +308,27 @@ public final class SwiftTargetBuildDescription {
308308
private func generateResourceEmbeddingCode() throws {
309309
guard needsResourceEmbedding else { return }
310310

311-
let stream = BufferedOutputByteStream()
312-
stream.send(
311+
var content =
313312
"""
314313
struct PackageResources {
315314
316315
"""
317-
)
318316

319317
try resources.forEach {
320318
guard $0.rule == .embedInCode else { return }
321319

322320
let variableName = $0.path.basename.spm_mangledToC99ExtendedIdentifier()
323321
let fileContent = try Data(contentsOf: URL(fileURLWithPath: $0.path.pathString)).map { String($0) }.joined(separator: ",")
324322

325-
stream.send("static let \(variableName): [UInt8] = [\(fileContent)]\n")
323+
content += "static let \(variableName): [UInt8] = [\(fileContent)]\n"
326324
}
327325

328-
stream.send("}")
326+
content += "}"
329327

330328
let subpath = try RelativePath(validating: "embedded_resources.swift")
331329
self.derivedSources.relativePaths.append(subpath)
332330
let path = self.derivedSources.root.appending(subpath)
333-
try self.fileSystem.writeIfChanged(path: path, bytes: stream.bytes)
331+
try self.fileSystem.writeIfChanged(path: path, string: content)
334332
}
335333

336334
/// Generate the resource bundle accessor, if appropriate.
@@ -354,8 +352,7 @@ public final class SwiftTargetBuildDescription {
354352
#"Bundle.main.bundleURL.appendingPathComponent("\#(bundlePath.basename.asSwiftStringLiteralConstant)").path"#
355353
}
356354

357-
let stream = BufferedOutputByteStream()
358-
stream.send(
355+
let content =
359356
"""
360357
\(self.toolsVersion < .vNext ? "import" : "@_implementationOnly import") class Foundation.Bundle
361358
@@ -374,7 +371,6 @@ public final class SwiftTargetBuildDescription {
374371
}()
375372
}
376373
"""
377-
)
378374

379375
let subpath = try RelativePath(validating: "resource_bundle_accessor.swift")
380376

@@ -384,7 +380,7 @@ public final class SwiftTargetBuildDescription {
384380
// Write this file out.
385381
// FIXME: We should generate this file during the actual build.
386382
let path = self.derivedSources.root.appending(subpath)
387-
try self.fileSystem.writeIfChanged(path: path, bytes: stream.bytes)
383+
try self.fileSystem.writeIfChanged(path: path, string: content)
388384
}
389385

390386
private func packageNameArgumentIfSupported(with pkg: ResolvedPackage, packageAccess: Bool) -> [String] {
@@ -671,43 +667,42 @@ public final class SwiftTargetBuildDescription {
671667

672668
private func writeOutputFileMap() throws -> AbsolutePath {
673669
let path = self.tempsPath.appending("output-file-map.json")
674-
let stream = BufferedOutputByteStream()
675-
676670
let masterDepsPath = self.tempsPath.appending("master.swiftdeps")
677-
stream.send(
671+
672+
var content =
678673
#"""
679674
{
680675
"": {
681676
682677
"""#
683-
)
678+
684679
if self.buildParameters.useWholeModuleOptimization {
685680
let moduleName = self.target.c99name
686-
stream.send(
681+
content +=
687682
#"""
688683
"dependencies": "\#(
689684
self.tempsPath.appending(component: moduleName + ".d").nativePathString(escaped: true)
690685
)",
691686
692687
"""#
693-
)
688+
694689
// FIXME: Need to record this deps file for processing it later.
695-
stream.send(
690+
content +=
696691
#"""
697692
"object": "\#(
698693
self.tempsPath.appending(component: moduleName + ".o").nativePathString(escaped: true)
699694
)",
700695
701696
"""#
702-
)
697+
703698
}
704-
stream.send(
699+
content +=
705700
#"""
706701
"swift-dependencies": "\#(masterDepsPath.nativePathString(escaped: true))"
707702
},
708703
709704
"""#
710-
)
705+
711706

712707
// Write out the entries for each source file.
713708
let sources = self.target.sources.paths + self.derivedSources.paths + self.pluginDerivedSources.paths
@@ -719,42 +714,38 @@ public final class SwiftTargetBuildDescription {
719714

720715
let swiftDepsPath = objectDir.appending(component: sourceFileName + ".swiftdeps")
721716

722-
stream.send(
717+
content +=
723718
#"""
724719
"\#(source.nativePathString(escaped: true))": {
725720
726721
"""#
727-
)
728722

729723
if !self.buildParameters.useWholeModuleOptimization {
730724
let depsPath = objectDir.appending(component: sourceFileName + ".d")
731-
stream.send(
725+
content +=
732726
#"""
733727
"dependencies": "\#(depsPath.nativePathString(escaped: true))",
734728
735729
"""#
736-
)
737730
// FIXME: Need to record this deps file for processing it later.
738731
}
739732

740733

741734
let partialModulePath = objectDir.appending(component: sourceFileName + "~partial.swiftmodule")
742735

743-
stream.send(
736+
content +=
744737
#"""
745738
"object": "\#(object.nativePathString(escaped: true))",
746739
"swiftmodule": "\#(partialModulePath.nativePathString(escaped: true))",
747740
"swift-dependencies": "\#(swiftDepsPath.nativePathString(escaped: true))"
748741
}\#((idx + 1) < sources.count ? "," : "")
749742

750743
"""#
751-
)
752744
}
753745

754-
stream.send("}\n")
746+
content += "}\n"
755747

756-
try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
757-
try self.fileSystem.writeFileContents(path, bytes: stream.bytes)
748+
try self.fileSystem.writeFileContents(path, string: content)
758749
return path
759750
}
760751

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
6262
private func write(
6363
tests: [IndexStore.TestCaseClass],
6464
forModule module: String,
65-
to path: AbsolutePath
65+
fileSystem: TSCBasic.FileSystem,
66+
path: AbsolutePath
6667
) throws {
67-
let stream = try LocalFileOutputByteStream(path)
6868

6969
let testsByClassNames = Dictionary(grouping: tests, by: { $0.name }).sorted(by: { $0.key < $1.key })
7070

71-
stream.send("import XCTest\n")
72-
stream.send("@testable import \(module)\n")
71+
var content = "import XCTest\n"
72+
content += "@testable import \(module)\n"
7373

7474
for iterator in testsByClassNames {
7575
// 'className' provides uniqueness for derived class.
7676
let className = iterator.key
7777
let testMethods = iterator.value.flatMap(\.testMethods)
78-
stream.send(
78+
content +=
7979
#"""
8080
8181
fileprivate extension \#(className) {
@@ -86,10 +86,9 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
8686
}
8787
8888
"""#
89-
)
9089
}
9190

92-
stream.send(
91+
content +=
9392
#"""
9493
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
9594
func __\#(module)__allTests() -> [XCTestCaseEntry] {
@@ -98,9 +97,9 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
9897
.joined(separator: ",\n "))
9998
]
10099
}
101-
"""#)
100+
"""#
102101

103-
stream.flush()
102+
try fileSystem.writeFileContents(path, string: content)
104103
}
105104

106105
private func execute(fileSystem: TSCBasic.FileSystem, tool: LLBuildManifest.TestDiscoveryTool) throws {
@@ -138,7 +137,12 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
138137
try fileSystem.writeFileContents(file, bytes: "")
139138
continue
140139
}
141-
try write(tests: tests, forModule: module, to: file)
140+
try write(
141+
tests: tests,
142+
forModule: module,
143+
fileSystem: fileSystem,
144+
path: file
145+
)
142146
}
143147

144148
guard let mainFile = maybeMainFile else {

Sources/Build/BuildPlan.swift

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,20 +1052,6 @@ extension BuildParameters {
10521052
}
10531053
}
10541054

1055-
extension FileSystem {
1056-
/// Write bytes to the path if the given contents are different.
1057-
func writeIfChanged(path: AbsolutePath, bytes: ByteString) throws {
1058-
try createDirectory(path.parentDirectory, recursive: true)
1059-
1060-
// Return if the contents are same.
1061-
if isFile(path), try readFileContents(path) == bytes {
1062-
return
1063-
}
1064-
1065-
try writeFileContents(path, bytes: bytes)
1066-
}
1067-
}
1068-
10691055
/// Generate the resource bundle Info.plist.
10701056
func generateResourceInfoPlist(
10711057
fileSystem: FileSystem,
@@ -1076,9 +1062,9 @@ func generateResourceInfoPlist(
10761062
return false
10771063
}
10781064

1079-
let stream = BufferedOutputByteStream()
1080-
stream.send(
1081-
"""
1065+
try fileSystem.writeIfChanged(
1066+
path: path,
1067+
string: """
10821068
<?xml version="1.0" encoding="UTF-8"?>
10831069
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
10841070
<plist version="1.0">
@@ -1089,8 +1075,6 @@ func generateResourceInfoPlist(
10891075
</plist>
10901076
"""
10911077
)
1092-
1093-
try fileSystem.writeIfChanged(path: path, bytes: stream.bytes)
10941078
return true
10951079
}
10961080

0 commit comments

Comments
 (0)