Skip to content

Commit dfd313c

Browse files
committed
[Runtime] Fast lookups of Concurrency types with standard manglings.
Have the Concurrency runtime register a struct containing pointers to all of its type descriptors with standard manglings. Then have the runtime use that struct to quickly look up those types, instead of using the standard, slower type lookup code. rdar://109783861
1 parent 90fd254 commit dfd313c

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

include/swift/Runtime/Metadata.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,21 @@ void swift_enableDynamicReplacementScope(const DynamicReplacementScope *scope);
992992
SWIFT_RUNTIME_EXPORT
993993
void swift_disableDynamicReplacementScope(const DynamicReplacementScope *scope);
994994

995+
/// A struct containing pointers to all of the type descriptors in the
996+
/// Concurrency runtime which have standard manglings.
997+
struct ConcurrencyStandardTypeDescriptors {
998+
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME)
999+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
1000+
const ContextDescriptor *TYPENAME;
1001+
#include "swift/Demangling/StandardTypesMangling.def"
1002+
};
1003+
1004+
/// Register the type descriptors with standard manglings from the Concurrency
1005+
/// runtime. The passed-in struct must be immortal.
1006+
SWIFT_RUNTIME_STDLIB_SPI
1007+
void _swift_registerConcurrencyStandardTypeDescriptors(
1008+
const ConcurrencyStandardTypeDescriptors *descriptors);
1009+
9951010
#pragma clang diagnostic pop
9961011

9971012
} // end namespace swift

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
107107
GlobalActor.swift
108108
MainActor.swift
109109
PartialAsyncTask.swift
110+
Setup.cpp
110111
SourceCompatibilityShims.swift
111112
Task.cpp
112113
Task.swift

stdlib/public/Concurrency/Setup.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- Setup.cpp - Load-time setup code ---------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Runtime/Metadata.h"
14+
15+
// Helper macros for figuring out the mangled name of a context descriptor.
16+
#define DESCRIPTOR_MANGLING_SUFFIX_Structure Mn
17+
#define DESCRIPTOR_MANGLING_SUFFIX_Class Mn
18+
#define DESCRIPTOR_MANGLING_SUFFIX_Enum Mn
19+
#define DESCRIPTOR_MANGLING_SUFFIX_Protocol Mp
20+
21+
#define DESCRIPTOR_MANGLING_SUFFIX_(X) X
22+
#define DESCRIPTOR_MANGLING_SUFFIX(KIND) \
23+
DESCRIPTOR_MANGLING_SUFFIX_(DESCRIPTOR_MANGLING_SUFFIX_##KIND)
24+
25+
#define DESCRIPTOR_MANGLING_(CHAR, SUFFIX) $sSc##CHAR##SUFFIX
26+
#define DESCRIPTOR_MANGLING(CHAR, SUFFIX) DESCRIPTOR_MANGLING_(CHAR, SUFFIX)
27+
28+
// Declare context descriptors for all of the concurrency descriptors with
29+
// standard manglings.
30+
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME)
31+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
32+
extern "C" const swift::ContextDescriptor DESCRIPTOR_MANGLING( \
33+
MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND));
34+
#include "swift/Demangling/StandardTypesMangling.def"
35+
36+
// Register our type descriptors with standard manglings when the concurrency
37+
// runtime is loaded. This allows the runtime to quickly resolve those standard
38+
// manglings.
39+
__attribute__((constructor)) static void setupStandardConcurrencyDescriptors() {
40+
static const swift::ConcurrencyStandardTypeDescriptors descriptors = {
41+
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME)
42+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
43+
&DESCRIPTOR_MANGLING(MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND)),
44+
#include "swift/Demangling/StandardTypesMangling.def"
45+
};
46+
_swift_registerConcurrencyStandardTypeDescriptors(&descriptors);
47+
}

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,8 @@ _searchTypeMetadataRecords(TypeMetadataPrivateState &T,
770770

771771
#include "swift/Demangling/StandardTypesMangling.def"
772772

773+
static const ConcurrencyStandardTypeDescriptors *concurrencyDescriptors;
774+
773775
static const ContextDescriptor *
774776
_findContextDescriptor(Demangle::NodePointer node,
775777
Demangle::Demangler &Dem) {
@@ -797,7 +799,10 @@ _findContextDescriptor(Demangle::NodePointer node,
797799
}
798800
// FIXME: When the _Concurrency library gets merged into the Standard Library,
799801
// we will be able to reference those symbols directly as well.
800-
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME)
802+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
803+
if (concurrencyDescriptors && name.equals(#TYPENAME)) { \
804+
return concurrencyDescriptors->TYPENAME; \
805+
}
801806
#if !SWIFT_OBJC_INTEROP
802807
# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
803808
#endif
@@ -851,6 +856,11 @@ _findContextDescriptor(Demangle::NodePointer node,
851856
return foundContext;
852857
}
853858

859+
void swift::_swift_registerConcurrencyStandardTypeDescriptors(
860+
const ConcurrencyStandardTypeDescriptors *descriptors) {
861+
concurrencyDescriptors = descriptors;
862+
}
863+
854864
#pragma mark Protocol descriptor cache
855865
namespace {
856866
struct ProtocolSection {

0 commit comments

Comments
 (0)