Skip to content

Commit be6e6e5

Browse files
committed
Use callback to avoid repeated allocation when collecting initialized properties
1 parent f4d9c7b commit be6e6e5

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5302,7 +5302,8 @@ class AssignOrInitInst
53025302

53035303
unsigned getNumInitializedProperties() const;
53045304

5305-
ArrayRef<VarDecl *> getInitializedProperties() const;
5305+
void forEachInitializedProperty(
5306+
llvm::function_ref<void(VarDecl *)> callback) const;
53065307
ArrayRef<VarDecl *> getAccessedProperties() const;
53075308

53085309
ArrayRef<Operand> getAllOperands() const { return Operands.asArray(); }

lib/SIL/IR/SILInstructions.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,13 +1328,13 @@ AssignOrInitInst::AssignOrInitInst(SILDebugLocation Loc, VarDecl *P,
13281328
}
13291329

13301330
void AssignOrInitInst::markAsInitialized(VarDecl *property) {
1331-
auto toInitProperties = getInitializedProperties();
1332-
for (unsigned index : indices(toInitProperties)) {
1333-
if (toInitProperties[index] == property) {
1334-
markAsInitialized(index);
1335-
break;
1331+
unsigned idx = 0;
1332+
this->forEachInitializedProperty([&](VarDecl *p) {
1333+
if (p == property) {
1334+
markAsInitialized(idx);
13361335
}
1337-
}
1336+
idx++;
1337+
});
13381338
}
13391339

13401340
void AssignOrInitInst::markAsInitialized(unsigned propertyIdx) {
@@ -1362,20 +1362,20 @@ DeclContext *AssignOrInitInst::getDeclContextOrNull() const {
13621362
}
13631363

13641364
unsigned AssignOrInitInst::getNumInitializedProperties() const {
1365-
return getInitializedProperties().size();
1365+
unsigned count = 0;
1366+
forEachInitializedProperty([&](VarDecl *property) { count++; });
1367+
return count;
13661368
}
13671369

1368-
ArrayRef<VarDecl *> AssignOrInitInst::getInitializedProperties() const {
1369-
if (auto *accessor = getReferencedInitAccessor())
1370-
return accessor->getInitializedProperties();
1371-
else {
1372-
// Dealing wtih an init accessor thunk, only initializes backing property
1373-
// FIXME: Tempory solution below, cannot allocate each time this function is
1374-
// called
1375-
SmallVector<VarDecl *> res;
1370+
void AssignOrInitInst::forEachInitializedProperty(
1371+
llvm::function_ref<void(VarDecl *)> callback) const {
1372+
if (auto *accessor = getReferencedInitAccessor()) {
1373+
for (auto *property : accessor->getInitializedProperties())
1374+
callback(property);
1375+
} else {
1376+
// Only the backing storage property/local variavle
13761377
auto *backingVar = Property->getPropertyWrapperBackingProperty();
1377-
res.push_back(backingVar);
1378-
return Property->getASTContext().AllocateCopy(res);
1378+
callback(backingVar);
13791379
}
13801380
}
13811381

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,8 +1275,7 @@ ElementUseCollector::collectAssignOrInitUses(AssignOrInitInst *Inst,
12751275
addElementUses(fieldIdx, type, Inst, useKind, property);
12761276
};
12771277

1278-
auto initializedElts = Inst->getInitializedProperties();
1279-
if (initializedElts.empty()) {
1278+
if (Inst->getNumInitializedProperties() == 0) {
12801279
auto initAccessorProperties = typeDC->getInitAccessorProperties();
12811280
auto initFieldAt = typeDC->getStoredProperties().size();
12821281

@@ -1294,8 +1293,8 @@ ElementUseCollector::collectAssignOrInitUses(AssignOrInitInst *Inst,
12941293
++initFieldAt;
12951294
}
12961295
} else {
1297-
for (auto *property : initializedElts)
1298-
addUse(property, DIUseKind::InitOrAssign);
1296+
Inst->forEachInitializedProperty(
1297+
[&](VarDecl *property) { addUse(property, DIUseKind::InitOrAssign); });
12991298
}
13001299

13011300
for (auto *property : Inst->getAccessedProperties())

lib/SILOptimizer/Mandatory/RawSILInstLowering.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,13 @@ lowerAssignOrInitInstruction(SILBuilderWithScope &b,
244244
/*fromBuiltin=*/false);
245245
}
246246

247-
auto toInitialize = inst->getInitializedProperties();
248-
for (unsigned index : indices(toInitialize)) {
247+
unsigned index = 0;
248+
inst->forEachInitializedProperty([&](VarDecl *property) {
249249
arguments.push_back(emitFieldReference(
250-
selfRef, toInitialize[index],
250+
selfRef, property,
251251
/*emitDestroy=*/inst->isPropertyAlreadyInitialized(index)));
252-
}
252+
index++;
253+
});
253254
}
254255

255256
// Now emit `initialValue` which is the only argument specified

0 commit comments

Comments
 (0)