Skip to content
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

Add Knit directive to disable code gen of performance functions #233

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/KnitCodeGen/KnitDirectives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ public struct KnitDirectives: Codable, Equatable, Sendable {
var moduleName: String?
var spi: String?

/// When true the code gen will not create the additional methods to improve assembler performance
var disablePerformanceGen: Bool

public init(
accessLevel: AccessLevel? = nil,
disablePerformanceGen: Bool = false,
getterConfig: Set<GetterConfig> = [],
moduleName: String? = nil,
spi: String? = nil
) {
self.accessLevel = accessLevel
self.disablePerformanceGen = disablePerformanceGen
self.getterConfig = getterConfig
self.moduleName = moduleName
self.spi = spi
Expand Down Expand Up @@ -56,6 +61,9 @@ public struct KnitDirectives: Codable, Equatable, Sendable {
if let name = parsed.moduleName {
result.moduleName = name
}
if parsed.disablePerformanceGen {
result.disablePerformanceGen = true
}
for getter in parsed.getterConfig {
result.getterConfig.insert(getter)
}
Expand All @@ -77,6 +85,9 @@ public struct KnitDirectives: Codable, Equatable, Sendable {
if token == "getter-callAsFunction" {
return KnitDirectives(getterConfig: [.callAsFunction])
}
if token == "disable-performance-gen" {
return KnitDirectives(disablePerformanceGen: true)
}
if let nameMatch = getterNamedRegex.firstMatch(in: token, range: NSMakeRange(0, token.count)) {
if nameMatch.numberOfRanges >= 2, nameMatch.range(at: 1).location != NSNotFound {
var range = nameMatch.range(at: 1)
Expand Down
4 changes: 3 additions & 1 deletion Sources/KnitCodeGen/TypeSafetySourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public enum TypeSafetySourceFile {
if let defaultOverrides = try makeDefaultOverrideExtensions(config: config) {
defaultOverrides
}
try makePerformanceExtension(config: config)
if !config.directives.disablePerformanceGen {
try makePerformanceExtension(config: config)
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/KnitCodeGenTests/ConfigurationSetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,42 @@ final class ConfigurationSetTests: XCTestCase {
XCTAssertNoThrow(try configSet.validateNoDuplicateRegistrations())
}

func testPerformanceGenDisabled() {
let config = Configuration(
assemblyName: "CustomAssembly",
moduleName: "Custom",
directives: .init(disablePerformanceGen: true),
registrations: [
.init(service: "Service1", accessLevel: .internal)
],
targetResolver: "Resolver"
)
let configSet = ConfigurationSet(
assemblies: [config],
externalTestingAssemblies: [],
moduleDependencies: []
)

XCTAssertEqual(
try configSet.makeTypeSafetySourceFile(),
"""
// Generated using Knit
// Do not edit directly!

import Knit

// The correct resolution of each of these types is enforced by a matching automated unit test
// If a type registration is missing or broken then the automated tests will fail for that PR
/// Generated from ``CustomAssembly``
extension Resolver {
func service1(file: StaticString = #fileID, function: StaticString = #function, line: UInt = #line) -> Service1 {
knitUnwrap(resolve(Service1.self), callsiteFile: file, callsiteFunction: function, callsiteLine: line)
}
}
"""
)
}

}

private enum Factory {
Expand Down
7 changes: 7 additions & 0 deletions Tests/KnitCodeGenTests/KnitDirectivesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ final class KnitDirectivesTests: XCTestCase {
XCTAssertThrowsError(try parse("// @knit module-name()"))
}

func testPerformanceGen() {
XCTAssertEqual(
try parse("// @knit disable-performance-gen"),
.init(disablePerformanceGen: true)
)
}

private func parse(_ comment: String) throws -> KnitDirectives {
let trivia = Trivia(pieces: [.lineComment(comment)])
return try KnitDirectives.parse(leadingTrivia: trivia)
Expand Down