Skip to content

Commit 9d7f040

Browse files
committed
Experimentally try caching the last ObjC bridging conformance we looked up
1 parent 2269a35 commit 9d7f040

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

stdlib/public/runtime/DynamicCast.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,28 @@ struct _ObjectiveCBridgeableWitnessTable : WitnessTable {
206206
extern "C" const ProtocolDescriptor
207207
PROTOCOL_DESCR_SYM(s21_ObjectiveCBridgeable);
208208

209+
struct ObjCBridgeWitnessCacheEntry {
210+
const Metadata *metadata;
211+
const _ObjectiveCBridgeableWitnessTable *witness;
212+
};
213+
214+
static std::atomic<ObjCBridgeWitnessCacheEntry> _objcBridgeWitnessCache = {};
215+
209216
static const _ObjectiveCBridgeableWitnessTable *
210217
findBridgeWitness(const Metadata *T) {
218+
auto cached = _objcBridgeWitnessCache.load(std::memory_order_relaxed);
219+
if (cached.metadata == T) {
220+
return cached.witness;
221+
}
211222
auto w = swift_conformsToProtocolCommon(
212223
T, &PROTOCOL_DESCR_SYM(s21_ObjectiveCBridgeable));
213-
return reinterpret_cast<const _ObjectiveCBridgeableWitnessTable *>(w);
224+
auto result = reinterpret_cast<const _ObjectiveCBridgeableWitnessTable *>(w);
225+
cached = {
226+
.metadata = T,
227+
.witness = result
228+
};
229+
_objcBridgeWitnessCache.store(cached, std::memory_order_relaxed);
230+
return result;
214231
}
215232

216233
/// Retrieve the bridged Objective-C type for the given type that

0 commit comments

Comments
 (0)