-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Performance Hints] Add simple check for returning of values of array and dictionary type #84578
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//===--------------------- PerformanceHints.cpp ---------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
|
||
// | ||
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This files implements the various checks/lints intended to provide | ||
// opt-in guidance for hidden costs in performance-critical Swift code. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "MiscDiagnostics.h" | ||
#include "swift/AST/ASTContext.h" | ||
#include "swift/AST/ASTWalker.h" | ||
#include "swift/AST/Decl.h" | ||
#include "swift/AST/DiagnosticsFrontend.h" | ||
#include "swift/AST/DiagnosticsSema.h" | ||
#include "swift/AST/Evaluator.h" | ||
#include "swift/AST/Expr.h" | ||
#include "swift/AST/TypeCheckRequests.h" | ||
|
||
using namespace swift; | ||
|
||
bool swift::performanceHintDiagnosticsEnabled(ASTContext &ctx) { | ||
return !ctx.Diags.isIgnoredDiagnostic(diag::perf_hint_closure_returns_array.ID) || | ||
!ctx.Diags.isIgnoredDiagnostic(diag::perf_hint_function_returns_array.ID); | ||
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is the best we have right now. At some point (not for this PR!) we should introduce a group-level query for this, which probably means a different underlying data structure. |
||
} | ||
|
||
namespace { | ||
|
||
void checkImplicitCopyReturnType(const FuncDecl *FD, DiagnosticEngine &Diags) { | ||
auto ReturnType = FD->getResultInterfaceType(); | ||
if (ReturnType->isArray() || ReturnType->isDictionary()) { | ||
Diags.diagnose(FD->getLoc(), diag::perf_hint_function_returns_array, FD, | ||
ReturnType->isArray()); | ||
} | ||
} | ||
|
||
void checkImplicitCopyReturnType(const ClosureExpr *Closure, | ||
DiagnosticEngine &Diags) { | ||
auto ReturnType = Closure->getResultType(); | ||
if (ReturnType->isArray() || ReturnType->isDictionary()) { | ||
Diags.diagnose(Closure->getLoc(), diag::perf_hint_closure_returns_array, | ||
ReturnType->isArray()); | ||
} | ||
} | ||
|
||
/// Produce performance hint diagnostics for a SourceFile. | ||
class PerformanceHintDiagnosticWalker final : public ASTWalker { | ||
ASTContext &Ctx; | ||
|
||
public: | ||
PerformanceHintDiagnosticWalker(ASTContext &Ctx) : Ctx(Ctx) {} | ||
|
||
static void check(SourceFile *SF) { | ||
auto Walker = PerformanceHintDiagnosticWalker(SF->getASTContext()); | ||
SF->walk(Walker); | ||
} | ||
|
||
PreWalkResult<Expr *> walkToExprPre(Expr *E) override { | ||
if (auto Closure = dyn_cast<ClosureExpr>(E)) | ||
checkImplicitCopyReturnType(Closure, Ctx.Diags); | ||
|
||
return Action::Continue(E); | ||
} | ||
|
||
PreWalkAction walkToDeclPre(Decl *D) override { | ||
if (auto *FD = dyn_cast<FuncDecl>(D)) | ||
checkImplicitCopyReturnType(FD, Ctx.Diags); | ||
|
||
return Action::Continue(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
evaluator::SideEffect EmitPerformanceHints::evaluate(Evaluator &evaluator, | ||
SourceFile *SF) const { | ||
PerformanceHintDiagnosticWalker::check(SF); | ||
return {}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// RUN: %empty-directory(%t) | ||
|
||
// Ensure ignored by-default | ||
// RUN: %target-swift-frontend -typecheck %s -diagnostic-style llvm -verify | ||
|
||
// Ensure enabled with '-Wwarning' | ||
// RUN: %target-swift-frontend -typecheck %s -diagnostic-style llvm -Wwarning PerformanceHints &> %t/output_warn.txt | ||
// RUN: cat %t/output_warn.txt | %FileCheck %s -check-prefix CHECK-WARN | ||
// RUN: %target-swift-frontend -typecheck %s -diagnostic-style llvm -Wwarning ReturnTypeImplicitCopy &> %t/output_warn.txt | ||
// RUN: cat %t/output_warn.txt | %FileCheck %s -check-prefix CHECK-WARN | ||
|
||
// Ensure enabled with '-Werror' for the broad category and downgraded to warning for the subgroup with '-Wwarning' | ||
// RUN: %target-swift-frontend -typecheck %s -diagnostic-style llvm -Werror PerformanceHints -Wwarning ReturnTypeImplicitCopy &> %t/output_warn.txt | ||
// RUN: cat %t/output_warn.txt | %FileCheck %s -check-prefix CHECK-WARN | ||
|
||
// Ensure enabled with '-Wwarning' for the broad category and downgraded to warning for the subgroup with '-Werror' | ||
// RUN: not %target-swift-frontend -typecheck %s -diagnostic-style llvm -Wwarning PerformanceHints -Werror ReturnTypeImplicitCopy &> %t/output_err.txt | ||
// RUN: cat %t/output_err.txt | %FileCheck %s -check-prefix CHECK-ERR | ||
|
||
// Ensure escalated with '-Werror' | ||
// RUN: not %target-swift-frontend -typecheck %s -diagnostic-style llvm -Werror ReturnTypeImplicitCopy &> %t/output_err.txt | ||
// RUN: cat %t/output_err.txt | %FileCheck %s -check-prefix CHECK-ERR | ||
|
||
// CHECK-ERR: error: Performance: 'foo()' returns an array, leading to implicit copies. Consider using an 'inout' parameter instead. [#ReturnTypeImplicitCopy] | ||
// CHECK-ERR: error: Performance: closure returns an array, leading to implicit copies. Consider using an 'inout' parameter instead. [#ReturnTypeImplicitCopy] | ||
|
||
// CHECK-WARN: warning: Performance: 'foo()' returns an array, leading to implicit copies. Consider using an 'inout' parameter instead. [#ReturnTypeImplicitCopy] | ||
// CHECK-WARN: warning: Performance: closure returns an array, leading to implicit copies. Consider using an 'inout' parameter instead. [#ReturnTypeImplicitCopy] | ||
|
||
func foo() -> [Int] { | ||
return [1,2,3,4,5,6] | ||
} | ||
|
||
func bar() -> Int { | ||
let myCoolArray = { () -> [Int] in | ||
return [1, 2] | ||
} | ||
return myCoolArray().count | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Swift Performance Hint: Function returning Array or Dictionary type | ||
|
||
- TODO: Docs |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.