Skip to content

Commit dd726a0

Browse files
committed
[NFC] Avoid repeatedly instantiating std::function
1 parent 9057123 commit dd726a0

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,13 @@ struct PrintOptions {
380380
/// has no associated doc-comment by itself.
381381
bool CascadeDocComment = false;
382382

383+
static const std::function<bool(const ExtensionDecl *)>
384+
defaultPrintExtensionContentAsMembers;
385+
383386
/// Whether to print the content of an extension decl inside the type decl where it
384387
/// extends from.
385388
std::function<bool(const ExtensionDecl *)> printExtensionContentAsMembers =
386-
[] (const ExtensionDecl *) { return false; };
389+
PrintOptions::defaultPrintExtensionContentAsMembers;
387390

388391
/// How to print the keyword argument and parameter name in functions.
389392
ArgAndParamPrintingMode ArgAndParamPrinting =

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -308,32 +308,30 @@ bool tryCheckedCastBrJumpThreading(
308308
/// A structure containing callbacks that are called when an instruction is
309309
/// removed or added.
310310
struct InstModCallbacks {
311-
std::function<void(SILInstruction *)> deleteInst = [](SILInstruction *inst) {
312-
inst->eraseFromParent();
313-
};
314-
std::function<void(SILInstruction *)> createdNewInst = [](SILInstruction *) {
315-
};
316-
std::function<void(SILValue, SILValue)> replaceValueUsesWith =
317-
[](SILValue oldValue, SILValue newValue) {
318-
oldValue->replaceAllUsesWith(newValue);
319-
};
311+
static const std::function<void(SILInstruction *)> defaultDeleteInst;
312+
static const std::function<void(SILInstruction *)> defaultCreatedNewInst;
313+
static const std::function<void(SILValue, SILValue)> defaultReplaceValueUsesWith;
314+
static const std::function<void(SingleValueInstruction *, SILValue)>
315+
defaultEraseAndRAUWSingleValueInst;
316+
317+
std::function<void(SILInstruction *)> deleteInst =
318+
InstModCallbacks::defaultDeleteInst;
319+
std::function<void(SILInstruction *)> createdNewInst =
320+
InstModCallbacks::defaultCreatedNewInst;
321+
std::function<void(SILValue, SILValue)>
322+
replaceValueUsesWith =
323+
InstModCallbacks::defaultReplaceValueUsesWith;
320324
std::function<void(SingleValueInstruction *, SILValue)>
321325
eraseAndRAUWSingleValueInst =
322-
[](SingleValueInstruction *i, SILValue newValue) {
323-
i->replaceAllUsesWith(newValue);
324-
i->eraseFromParent();
325-
};
326+
InstModCallbacks::defaultEraseAndRAUWSingleValueInst;
326327

327328
InstModCallbacks(decltype(deleteInst) deleteInst,
328329
decltype(createdNewInst) createdNewInst,
329330
decltype(replaceValueUsesWith) replaceValueUsesWith)
330331
: deleteInst(deleteInst), createdNewInst(createdNewInst),
331332
replaceValueUsesWith(replaceValueUsesWith),
332333
eraseAndRAUWSingleValueInst(
333-
[](SingleValueInstruction *i, SILValue newValue) {
334-
i->replaceAllUsesWith(newValue);
335-
i->eraseFromParent();
336-
}) {}
334+
InstModCallbacks::defaultEraseAndRAUWSingleValueInst) {}
337335

338336
InstModCallbacks(
339337
decltype(deleteInst) deleteInst, decltype(createdNewInst) createdNewInst,

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858

5959
using namespace swift;
6060

61+
// Defined here to avoid repeatedly paying the price of template instantiation.
62+
const std::function<bool(const ExtensionDecl *)>
63+
PrintOptions::defaultPrintExtensionContentAsMembers
64+
= [] (const ExtensionDecl *) { return false; };
65+
6166
void PrintOptions::setBaseType(Type T) {
6267
if (T->is<ErrorType>())
6368
return;

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ static llvm::cl::opt<bool> KeepWillThrowCall(
4848
llvm::cl::desc(
4949
"Keep calls to swift_willThrow, even if the throw is optimized away"));
5050

51+
// Defined here to avoid repeatedly paying the price of template instantiation.
52+
const std::function<void(SILInstruction *)>
53+
InstModCallbacks::defaultDeleteInst
54+
= [](SILInstruction *inst) {
55+
inst->eraseFromParent();
56+
};
57+
const std::function<void(SILInstruction *)>
58+
InstModCallbacks::defaultCreatedNewInst
59+
= [](SILInstruction *) {};
60+
const std::function<void(SILValue, SILValue)>
61+
InstModCallbacks::defaultReplaceValueUsesWith
62+
= [](SILValue oldValue, SILValue newValue) {
63+
oldValue->replaceAllUsesWith(newValue);
64+
};
65+
const std::function<void(SingleValueInstruction *, SILValue)>
66+
InstModCallbacks::defaultEraseAndRAUWSingleValueInst
67+
= [](SingleValueInstruction *i, SILValue newValue) {
68+
i->replaceAllUsesWith(newValue);
69+
i->eraseFromParent();
70+
};
71+
5172
/// Creates an increment on \p Ptr before insertion point \p InsertPt that
5273
/// creates a strong_retain if \p Ptr has reference semantics itself or a
5374
/// retain_value if \p Ptr is a non-trivial value without reference-semantics.

0 commit comments

Comments
 (0)