File tree 6 files changed +111
-8
lines changed
6 files changed +111
-8
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,10 @@ let package = Package(
47
47
// See the "Dependencies" section below.
48
48
] ,
49
49
targets: [
50
+ . target(
51
+ name: " _InstructionCounter "
52
+ ) ,
53
+
50
54
. target(
51
55
name: " SwiftFormat " ,
52
56
dependencies: [
@@ -108,6 +112,7 @@ let package = Package(
108
112
. executableTarget(
109
113
name: " swift-format " ,
110
114
dependencies: [
115
+ " _InstructionCounter " ,
111
116
" SwiftFormat " ,
112
117
. product( name: " ArgumentParser " , package : " swift-argument-parser " ) ,
113
118
. product( name: " SwiftSyntax " , package : " swift-syntax " ) ,
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ #include <stdint.h>
14
+
15
+ /// On macOS returns the number of instructions the process has executed since
16
+ /// it was launched, on all other platforms returns 0.
17
+ uint64_t getInstructionsExecuted ();
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ #if __APPLE__
14
+ #include <TargetConditionals.h>
15
+ #if TARGET_OS_MAC && !TARGET_OS_IPHONE
16
+ #define TARGET_IS_MACOS 1
17
+ #endif
18
+ #endif
19
+
20
+ #include "InstructionsExecuted.h"
21
+
22
+ #ifdef TARGET_IS_MACOS
23
+ #include <libproc.h>
24
+ #include <sys/resource.h>
25
+ #include <unistd.h>
26
+
27
+ uint64_t getInstructionsExecuted () {
28
+ struct rusage_info_v4 ru ;
29
+ if (proc_pid_rusage (getpid (), RUSAGE_INFO_V4 , (rusage_info_t * )& ru ) == 0 ) {
30
+ return ru .ri_instructions ;
31
+ }
32
+ return 0 ;
33
+ }
34
+ #else
35
+ uint64_t getInstructionsExecuted () {
36
+ return 0 ;
37
+ }
38
+ #endif
Original file line number Diff line number Diff line change @@ -30,16 +30,21 @@ extension SwiftFormatCommand {
30
30
@OptionGroup ( )
31
31
var formatOptions : LintFormatOptions
32
32
33
+ @OptionGroup ( visibility: . hidden)
34
+ var performanceMeasurementOptions : PerformanceMeasurementsOptions
35
+
33
36
func validate( ) throws {
34
37
if inPlace && formatOptions. paths. isEmpty {
35
38
throw ValidationError ( " '--in-place' is only valid when formatting files " )
36
39
}
37
40
}
38
41
39
42
func run( ) throws {
40
- let frontend = FormatFrontend ( lintFormatOptions: formatOptions, inPlace: inPlace)
41
- frontend. run ( )
42
- if frontend. diagnosticsEngine. hasErrors { throw ExitCode . failure }
43
+ try performanceMeasurementOptions. countingInstructionsIfRequested {
44
+ let frontend = FormatFrontend ( lintFormatOptions: formatOptions, inPlace: inPlace)
45
+ frontend. run ( )
46
+ if frontend. diagnosticsEngine. hasErrors { throw ExitCode . failure }
47
+ }
43
48
}
44
49
}
45
50
}
Original file line number Diff line number Diff line change @@ -28,12 +28,17 @@ extension SwiftFormatCommand {
28
28
)
29
29
var strict : Bool = false
30
30
31
- func run( ) throws {
32
- let frontend = LintFrontend ( lintFormatOptions: lintOptions)
33
- frontend. run ( )
31
+ @OptionGroup ( visibility: . hidden)
32
+ var performanceMeasurementOptions : PerformanceMeasurementsOptions
34
33
35
- if frontend. diagnosticsEngine. hasErrors || strict && frontend. diagnosticsEngine. hasWarnings {
36
- throw ExitCode . failure
34
+ func run( ) throws {
35
+ try performanceMeasurementOptions. countingInstructionsIfRequested {
36
+ let frontend = LintFrontend ( lintFormatOptions: lintOptions)
37
+ frontend. run ( )
38
+
39
+ if frontend. diagnosticsEngine. hasErrors || strict && frontend. diagnosticsEngine. hasWarnings {
40
+ throw ExitCode . failure
41
+ }
37
42
}
38
43
}
39
44
}
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import ArgumentParser
14
+ import _InstructionCounter
15
+
16
+ struct PerformanceMeasurementsOptions : ParsableArguments {
17
+ @Flag ( help: " Measure number of instructions executed by swift-format " )
18
+ var measureInstructions = false
19
+
20
+ /// If `measureInstructions` is set, execute `body` and print the number of instructions
21
+ /// executed by it. Otherwise, just execute `body`
22
+ func printingInstructionCountIfRequested< T> ( _ body: ( ) throws -> T ) rethrows -> T {
23
+ if !measureInstructions {
24
+ return try body ( )
25
+ } else {
26
+ let startInstructions = getInstructionsExecuted ( )
27
+ defer {
28
+ print ( " Instructions executed: \( getInstructionsExecuted ( ) - startInstructions) " )
29
+ }
30
+ return try body ( )
31
+ }
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments