Skip to content

Commit 9a403a0

Browse files
committed
[Runtime] Only cache completed type metadata for associated type witnesses.
This is the scheme used for metadata caches elsewhere, and it eliminates the need to check the metadata state along the hot path of swift_getAssociatedTypeWitness().
1 parent 4444ff9 commit 9a403a0

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,7 +3968,8 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request,
39683968
auto witness = ((const void* const *)wtable)[witnessIndex];
39693969
if (LLVM_LIKELY((uintptr_t(witness) &
39703970
ProtocolRequirementFlags::AssociatedTypeMangledNameBit) == 0)) {
3971-
return swift_checkMetadataState(request, (const Metadata *)witness);
3971+
// Cached metadata pointers are always complete.
3972+
return MetadataResponse{(const Metadata *)witness, MetadataState::Complete};
39723973
}
39733974

39743975
// Find the mangled name.
@@ -4028,11 +4029,19 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request,
40284029
mangledName.str().c_str());
40294030
}
40304031

4031-
// Update the witness table.
4032+
40324033
assert((uintptr_t(assocTypeMetadata) &
40334034
ProtocolRequirementFlags::AssociatedTypeMangledNameBit) == 0);
4034-
reinterpret_cast<const void**>(wtable)[witnessIndex] = assocTypeMetadata;
4035-
return swift_checkMetadataState(request, assocTypeMetadata);
4035+
4036+
// Check the metadata state.
4037+
auto response = swift_checkMetadataState(request, assocTypeMetadata);
4038+
4039+
// If the metadata was completed, record it in the witness table.
4040+
if (response.State == MetadataState::Complete) {
4041+
reinterpret_cast<const void**>(wtable)[witnessIndex] = assocTypeMetadata;
4042+
}
4043+
4044+
return response;
40364045
}
40374046

40384047
/***************************************************************************/

0 commit comments

Comments
 (0)