Skip to content

Instantly share code, notes, and snippets.

@saagarjha
Last active March 12, 2022 08:28
Show Gist options
  • Save saagarjha/182fc3620daba95cbcd9136bfd6a5d7f to your computer and use it in GitHub Desktop.
Save saagarjha/182fc3620daba95cbcd9136bfd6a5d7f to your computer and use it in GitHub Desktop.
Works around some poor performance in XCBBuildService due to slow reflection machinery (https://twitter.com/_saagarjha/status/1499599539795234816)

To build this:

$ swiftc -emit-object XCBBuildServiceFix.swift -O
$ clang XCBBuildServiceFix.o XCBBuildServiceFix.s -shared -o libXCBBuildServiceFix.dylib -O -L /usr/lib/swift/ -lswiftCore -F /Applications/Xcode.app/Contents/SharedFrameworks/XCBuild.framework/Versions/Current/PlugIns/XCBBuildService.bundle/Contents/Frameworks/ -framework XCBUtil -framework XCBProtocol

Then inject the result into XCBBuildService, which launches from /Applications/Xcode.app/Contents/SharedFrameworks/XCBuild.framework/Versions/Current/PlugIns/XCBBuildService.bundle/Contents/MacOS/XCBBuildService, using your injection tool of choice.

.section __DATA,__interpose
.p2align 3
.quad _String_init_describing
.quad _$sSS10describingSSx_tclufC
import Foundation
protocol OptionalProtocol {
var isNonNil: Bool { get }
}
extension Optional: OptionalProtocol {
var isNonNil: Bool {
self != nil
}
var _description: String {
"\(describe(value: self))"
}
}
extension Bool {
var _description: String {
description
}
}
extension String {
var _description: String {
"\"\(description)\""
}
}
// NOTE: _internal layout probably doesn't really match actual layout.
// This is mostly fine because it's to keep the structs ~appropriately sized.
public struct Path {
var _internal: (String)
@_silgen_name("$s7XCBUtil4PathV3strSSvg")
func str() -> String
}
extension Path {
var _description: String {
"XCBUtil.Path(str: \(str()._description))"
}
}
public struct ArenaInfo {
var _internal: (Path, Path, Path, Path, Path, Path?, Bool)
@_silgen_name("$s11XCBProtocol9ArenaInfoV15derivedDataPath7XCBUtil0F0Vvg")
func derivedDataPath() -> Path
@_silgen_name("$s11XCBProtocol9ArenaInfoV17buildProductsPath7XCBUtil0F0Vvg")
func buildProductsPath() -> Path
@_silgen_name("$s11XCBProtocol9ArenaInfoV22buildIntermediatesPath7XCBUtil0F0Vvg")
func buildIntermediatesPath() -> Path
@_silgen_name("$s11XCBProtocol9ArenaInfoV7pchPath7XCBUtil0E0Vvg")
func pchPath() -> Path
@_silgen_name("$s11XCBProtocol9ArenaInfoV12indexPCHPath7XCBUtil4PathVvg")
func indexPCHPath() -> Path
@_silgen_name("$s11XCBProtocol9ArenaInfoV24indexDataStoreFolderPath7XCBUtil0H0VSgvg")
func indexDataStoreFolderPath() -> Path?
@_silgen_name("$s11XCBProtocol9ArenaInfoV20indexEnableDataStoreSbvg")
func indexEnableDataStore() -> Bool
}
extension ArenaInfo {
var _description: String {
"XCBProtocol.ArenaInfo(derivedDataPath: \(derivedDataPath()._description), buildProductsPath: \(buildProductsPath()._description), buildIntermediatesPath: \(buildIntermediatesPath()._description), pchPath: \(pchPath()._description), indexPCHPath: \(indexPCHPath()._description), indexDataStoreFolderPath: \(indexDataStoreFolderPath()._description), indexEnableDataStore: \(indexEnableDataStore()._description))"
}
}
public struct OrderedSet<T: Hashable> {
var _internal: ([T], Set<T>)
@_silgen_name("$s7XCBUtil10OrderedSetV11descriptionSSvg")
func description() -> String
}
extension OrderedSet {
var _description: String {
description()
}
}
public struct RunDestinationInfo {
var _internal: (String, String, String?, String, OrderedSet<String>, Bool)
@_silgen_name("$s11XCBProtocol18RunDestinationInfoV8platformSSvg")
func platform() -> String
@_silgen_name("$s11XCBProtocol18RunDestinationInfoV3sdkSSvg")
func sdk() -> String
@_silgen_name("$s11XCBProtocol18RunDestinationInfoV10sdkVariantSSSgvg")
func sdkVariant() -> String?
@_silgen_name("$s11XCBProtocol18RunDestinationInfoV18targetArchitectureSSvg")
func targetArchitecture() -> String
@_silgen_name("$s11XCBProtocol18RunDestinationInfoV22supportedArchitectures7XCBUtil10OrderedSetVySSGvg")
func supportedArchitectures() -> OrderedSet<String>
@_silgen_name("$s11XCBProtocol18RunDestinationInfoV21disableOnlyActiveArchSbvg")
func disableOnlyActiveArch() -> Bool
}
extension RunDestinationInfo {
var _description: String {
"XCBProtocol.RunDestinationInfo(platform: \(platform()._description), sdk: \(sdk()._description), sdkVariant: \(sdkVariant()._description), targetArchitecture: \(targetArchitecture()._description), supportedArchitectures: \(supportedArchitectures()._description), disableOnlyActiveArch: \(disableOnlyActiveArch()._description))"
}
}
func describe<T>(value: T) -> String {
// Ideally this would use _getTypeName but it doesn't quite work
if _typeName(T.self) == "Swift.Optional<XCBProtocol.ArenaInfo>", ((value as? OptionalProtocol)?.isNonNil)! {
return withUnsafePointer(to: value) {
"Optional(\(UnsafeRawPointer($0).assumingMemoryBound(to: ArenaInfo.self).pointee._description))"
}
} else if _typeName(T.self) == "Swift.Optional<XCBProtocol.RunDestinationInfo>", ((value as? OptionalProtocol)?.isNonNil)! {
return withUnsafePointer(to: value) {
"Optional(\(UnsafeRawPointer($0).assumingMemoryBound(to: RunDestinationInfo.self).pointee._description))"
}
} else if T.self == Path?.self, ((value as? OptionalProtocol)?.isNonNil)! {
return withUnsafePointer(to: value) {
"Optional(\(UnsafeRawPointer($0).assumingMemoryBound(to: Path.self).pointee._description))"
}
} else {
return String(describing: value)
}
}
@_silgen_name("String_init_describing")
func String_init_describing<T>(describing value: T) -> String {
describe(value: value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment