Skip to content

Commit

Permalink
Changes to enable the compiler to make better optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch committed Oct 31, 2023
1 parent d73e64a commit 705d9e0
Show file tree
Hide file tree
Showing 30 changed files with 193 additions and 145 deletions.
3 changes: 1 addition & 2 deletions Sources/Frontend/Commands/ScanBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ final class ScanBehavior {
results = try block(project)
let interval = logger.beginInterval("result:output")
let filteredResults = OutputDeclarationFilter().filter(results)
let sortedResults = filteredResults.sorted { $0.declaration < $1.declaration }
let output = try configuration.outputFormat.formatter.init().format(sortedResults)
let output = try configuration.outputFormat.formatter.init().format(filteredResults)
logger.info("", canQuiet: true)
logger.info(output, canQuiet: false)
logger.endInterval(interval)
Expand Down
41 changes: 0 additions & 41 deletions Sources/Frontend/Formatters/OutputDeclarationFilter.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import Shared
import PeripheryKit

final class CheckstyleFormatter: OutputFormatter {
func format(_ results: [ScanResult]) -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import PeripheryKit

final class CodeClimateFormatter: OutputFormatter {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import Shared
import PeripheryKit

final class CsvFormatter: OutputFormatter {
func format(_ results: [ScanResult]) -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import Shared
import PeripheryKit

final class JsonFormatter: OutputFormatter {
func format(_ results: [ScanResult]) throws -> String {
Expand Down
42 changes: 42 additions & 0 deletions Sources/PeripheryKit/Formatters/OutputDeclarationFilter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Foundation
import SystemPackage
import Shared
import FilenameMatcher

public final class OutputDeclarationFilter {
private let configuration: Configuration
private let logger: ContextualLogger

public required init(configuration: Configuration = .shared, logger: Logger = .init()) {
self.configuration = configuration
self.logger = logger.contextualized(with: "report:filter")
}

public func filter(_ declarations: [ScanResult]) -> [ScanResult] {
if configuration.reportInclude.isEmpty && configuration.reportExclude.isEmpty {
return declarations
}

return declarations
.filter {
let path = $0.declaration.location.file.path

if configuration.reportIncludeMatchers.isEmpty {
if configuration.reportExcludeMatchers.anyMatch(filename: path.string) {
self.logger.debug("Excluding \(path.string)")
return false
}

return true
}

if configuration.reportIncludeMatchers.anyMatch(filename: path.string) {
self.logger.debug("Including \(path.string)")
return true
}

return false
}
.sorted { $0.declaration < $1.declaration }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Foundation
import Shared
import PeripheryKit

protocol OutputFormatter: AnyObject {
public protocol OutputFormatter: AnyObject {
init()
func format(_ results: [ScanResult]) throws -> String
}
Expand Down Expand Up @@ -58,7 +57,7 @@ extension OutputFormatter {
}
}

extension OutputFormat {
public extension OutputFormat {
var formatter: OutputFormatter.Type {
switch self {
case .xcode:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import Shared
import PeripheryKit

final class XcodeFormatter: OutputFormatter {
func format(_ results: [ScanResult]) throws -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/PeripheryKit/Indexer/Accessibility.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum Accessibility: String {
enum Accessibility: String {
case `public` = "public"
case `internal` = "internal"
case `private` = "private"
Expand Down
59 changes: 29 additions & 30 deletions Sources/PeripheryKit/Indexer/Declaration.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public final class Declaration {
public enum Kind: String, RawRepresentable, CaseIterable {
final class Declaration {
enum Kind: String, RawRepresentable, CaseIterable {
case `associatedtype` = "associatedtype"
case `class` = "class"
case `enum` = "enum"
Expand Down Expand Up @@ -41,7 +41,7 @@ public final class Declaration {
case varParameter = "var.parameter"
case varStatic = "var.static"

public static var functionKinds: Set<Kind> {
static var functionKinds: Set<Kind> {
Set(Kind.allCases.filter { $0.isFunctionKind })
}

Expand Down Expand Up @@ -137,15 +137,15 @@ public final class Declaration {
functionKinds.union(variableKinds).union(globalKinds)
}

public var isAccessorKind: Bool {
var isAccessorKind: Bool {
rawValue.hasPrefix("function.accessor")
}

static var toplevelAttributableKind: Set<Kind> {
[.class, .struct, .enum]
}

public var displayName: String? {
var displayName: String? {
switch self {
case .class:
return "class"
Expand Down Expand Up @@ -179,22 +179,21 @@ public final class Declaration {
}
}

public let location: SourceLocation
public var attributes: Set<String> = []
public var modifiers: Set<String> = []
public var accessibility: DeclarationAccessibility = .init(value: .internal, isExplicit: false)
public let kind: Kind
public var name: String?
public let usrs: Set<String>
public var unusedParameters: Set<Declaration> = []
public var declarations: Set<Declaration> = []
public var commentCommands: Set<CommentCommand> = []
public var references: Set<Reference> = []
public var declaredType: String?
public var hasCapitalSelfFunctionCall: Bool = false
public var hasGenericFunctionReturnedMetatypeParameters: Bool = false
public var parent: Declaration?

let location: SourceLocation
var attributes: Set<String> = []
var modifiers: Set<String> = []
var accessibility: DeclarationAccessibility = .init(value: .internal, isExplicit: false)
let kind: Kind
var name: String?
let usrs: Set<String>
var unusedParameters: Set<Declaration> = []
var declarations: Set<Declaration> = []
var commentCommands: Set<CommentCommand> = []
var references: Set<Reference> = []
var declaredType: String?
var hasCapitalSelfFunctionCall: Bool = false
var hasGenericFunctionReturnedMetatypeParameters: Bool = false
var parent: Declaration?
var related: Set<Reference> = []
var isImplicit: Bool = false
var isObjcAccessible: Bool = false
Expand All @@ -213,7 +212,7 @@ public final class Declaration {
return declarations
}

public var descendentDeclarations: Set<Declaration> {
var descendentDeclarations: Set<Declaration> {
Set(declarations.flatMap { $0.descendentDeclarations }).union(declarations).union(unusedParameters)
}

Expand Down Expand Up @@ -262,19 +261,19 @@ public final class Declaration {
}

extension Declaration: Hashable {
public func hash(into hasher: inout Hasher) {
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}

extension Declaration: Equatable {
public static func == (lhs: Declaration, rhs: Declaration) -> Bool {
static func == (lhs: Declaration, rhs: Declaration) -> Bool {
lhs.identifier == rhs.identifier
}
}

extension Declaration: CustomStringConvertible {
public var description: String {
var description: String {
"Declaration(\(descriptionParts.joined(separator: ", ")))"
}

Expand All @@ -298,7 +297,7 @@ extension Declaration: CustomStringConvertible {
}

extension Declaration: Comparable {
public static func < (lhs: Declaration, rhs: Declaration) -> Bool {
static func < (lhs: Declaration, rhs: Declaration) -> Bool {
if lhs.location == rhs.location {
return lhs.usrs.sorted().joined() < rhs.usrs.sorted().joined()
}
Expand All @@ -307,11 +306,11 @@ extension Declaration: Comparable {
}
}

public struct DeclarationAccessibility {
public let value: Accessibility
public let isExplicit: Bool
struct DeclarationAccessibility {
let value: Accessibility
let isExplicit: Bool

public func isExplicitly(_ testValue: Accessibility) -> Bool {
func isExplicitly(_ testValue: Accessibility) -> Bool {
isExplicit && value == testValue
}

Expand Down
28 changes: 14 additions & 14 deletions Sources/PeripheryKit/Indexer/Reference.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public final class Reference {
public enum Role {
final class Reference {
enum Role {
case varType
case returnType
case parameterType
Expand All @@ -21,14 +21,14 @@ public final class Reference {
}
}

public let location: SourceLocation
public let kind: Declaration.Kind
public let isRelated: Bool
public var name: String?
public var parent: Declaration?
public var references: Set<Reference> = []
public let usr: String
public var role: Role = .unknown
let location: SourceLocation
let kind: Declaration.Kind
let isRelated: Bool
var name: String?
var parent: Declaration?
var references: Set<Reference> = []
let usr: String
var role: Role = .unknown

private let identifier: Int

Expand All @@ -46,19 +46,19 @@ public final class Reference {
}

extension Reference: Hashable {
public func hash(into hasher: inout Hasher) {
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}

extension Reference: Equatable {
public static func == (lhs: Reference, rhs: Reference) -> Bool {
static func == (lhs: Reference, rhs: Reference) -> Bool {
lhs.identifier == rhs.identifier
}
}

extension Reference: CustomStringConvertible {
public var description: String {
var description: String {
let referenceType = isRelated ? "Related" : "Reference"

return "\(referenceType)(\(descriptionParts.joined(separator: ", ")))"
Expand All @@ -72,7 +72,7 @@ extension Reference: CustomStringConvertible {
}

extension Reference: Comparable {
public static func < (lhs: Reference, rhs: Reference) -> Bool {
static func < (lhs: Reference, rhs: Reference) -> Bool {
lhs.location < rhs.location
}
}
16 changes: 8 additions & 8 deletions Sources/PeripheryKit/Indexer/SourceFile.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Foundation
import SystemPackage

public class SourceFile {
public typealias ImportStatement = (parts: [String], isTestable: Bool)
class SourceFile {
typealias ImportStatement = (parts: [String], isTestable: Bool)

public let path: FilePath
public let modules: Set<String>
public var importStatements: [ImportStatement] = []
let path: FilePath
let modules: Set<String>
var importStatements: [ImportStatement] = []

init(path: FilePath, modules: Set<String>) {
self.path = path
Expand All @@ -15,19 +15,19 @@ public class SourceFile {
}

extension SourceFile: Hashable {
public func hash(into hasher: inout Hasher) {
func hash(into hasher: inout Hasher) {
hasher.combine(path)
}
}

extension SourceFile: Equatable {
public static func == (lhs: SourceFile, rhs: SourceFile) -> Bool {
static func == (lhs: SourceFile, rhs: SourceFile) -> Bool {
lhs.path == rhs.path
}
}

extension SourceFile: Comparable {
public static func < (lhs: SourceFile, rhs: SourceFile) -> Bool {
static func < (lhs: SourceFile, rhs: SourceFile) -> Bool {
lhs.path.string < rhs.path.string
}
}
Loading

0 comments on commit 705d9e0

Please sign in to comment.