Commit Graph

81 Commits

Author SHA1 Message Date
Dmitrii Galimzianov
091b005a60 Protocol conformance cache for generic types
This change adds a new type of cache (cache by type descriptor) to the protocol conformance lookup system. This optimization is beneficial for generic types, where the
same conformance can be reused across different instantiations of the generic type.

Key changes:
- Add a `GetOrInsertManyScope` class to `ConcurrentReadableHashMap` for performing
  multiple insertions under a single lock
- Add type descriptor-based caching for protocol conformances
- Add environment variables for controlling and debugging the conformance cache
- Add tests to verify the behavior of the conformance cache
- Fix for https://github.com/swiftlang/swift/issues/82889

The implementation is controlled by the `SWIFT_DEBUG_ENABLE_CACHE_PROTOCOL_CONFORMANCES_BY_TYPE_DESCRIPTOR`
environment variable, which is enabled by default.

This reapplies https://github.com/swiftlang/swift/pull/82818 after it's been reverted in https://github.com/swiftlang/swift/pull/83770.
2025-08-30 00:12:52 +02:00
Dmitrii Galimzianov
4509b22415 Revert "Protocol conformance cache for generic types"
This reverts commit 7989dbe24e merged in https://github.com/swiftlang/swift/pull/82818
2025-08-16 01:39:16 +02:00
Dmitrii Galimzianov
7989dbe24e Protocol conformance cache for generic types
This change adds a new type of cache (cache by type descriptor) to the protocol conformance lookup system. This optimization is beneficial for generic types, where the
same conformance can be reused across different instantiations of the generic type.

Key changes:
- Add a `GetOrInsertManyScope` class to `ConcurrentReadableHashMap` for performing
  multiple insertions under a single lock
- Add type descriptor-based caching for protocol conformances
- Add environment variables for controlling and debugging the conformance cache
- Add tests to verify the behavior of the conformance cache
- Fix for https://github.com/swiftlang/swift/issues/82889

The implementation is controlled by the `SWIFT_DEBUG_ENABLE_CACHE_PROTOCOL_CONFORMANCES_BY_TYPE_DESCRIPTOR`
environment variable, which is enabled by default.
2025-07-11 03:54:42 +02:00
Doug Gregor
885f829a63 [Conformance cache] Add allocated extended-storage for entries to the free list
This memory is part of the conformance cache concurrent hash map, so
when we clear the conformance cache, record each of the allocated
pointers within the concurrent map's free list. This way, it'll be
freed with the rest of the concurrent map when it's safe to do so.
2025-04-16 15:11:59 -07:00
Slava Pestov
01acda7730 Runtime: Remove linear order on metadata cache keys 2023-03-02 10:22:11 -05:00
Slava Pestov
affc232b8a Runtime: Remove ConcurrentMap 2023-03-02 09:35:28 -05:00
Slava Pestov
64c796eea2 Runtime: Remove ConcurrentList 2023-03-02 09:33:31 -05:00
Alastair Houghton
0e9318cec5 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
f5bdb858e0 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alex Hoppen
4aa2bbbf06 Revert "Merge pull request #42447 from al45tair/eng/PR-90776105"
This reverts commit 8bcb71140f, reversing
changes made to c4dd271d36.
2022-06-02 18:03:23 +02:00
Alastair Houghton
b5bd267ff1 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-05-24 14:57:41 +01:00
Alastair Houghton
63a09007a1 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-05-24 14:57:39 +01:00
Jonathan Grynspan
845182252c Add exported symbol on non-Apple platforms to enumerate MetadataSection structures at runtime. 2022-03-04 23:44:10 -05:00
Saleem Abdulrasool
bc9b9a5204 Runtime: make padding explicit for ElementStorage
The `ElementStorage` member `Capacity` will be padded up to the next pointer alignment due to the storage of the `ElemTy` by C's struct layout rules.  However, this is implicit and not entirely guaranteed.  Instead, make the storage a pointer sized value, and then truncate to 32-bits to maintain ABI compatibility.  This becomes important as we expand on the runtime metadata handling.
2022-02-04 16:55:52 -08:00
Evan Wilde
d452f68e70 Remove constexpr-if from runtime
constexpr-if is a C++17 feature. Swift is C++14, so this results in a
lot of warnings. Clang is smart enough that the constant propagation
will elide the unused side of the branch anyway. Since we're doing a
template check on a type, we should always have enough information at
compile to elide. We would need a constexpr/std::enable_if-level
approach if the API's were different to tell clang not to look at one
side of the branch. That's not the case here so we can let the optimizer
do it's thing and let clang look at both sides.
2022-01-14 16:21:15 -08:00
Jonathan Grynspan
c9f813604a ConcurrentReadableArray: Use std::uninitialized_copy_n() instead of std::copy() to avoid calling destructors on uninitialized memory 2021-12-11 13:25:51 -05:00
Konrad `ktoso` Malawski
703595e396 [Concurrent] Warnings/errors should use "sendable" not "concurrent"
Resolves rdar://83416205
2021-09-23 12:45:07 +09:00
Kuba (Brecka) Mracek
aaff37ffb8 Add a SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC flag to allow/disallow uses of Darwin libmalloc APIs (malloc_default_zone, malloc_zone_malloc, etc.) (#33812) 2021-08-23 19:37:43 -07:00
David Zarzycki
54e20177c5 [runtime] Fix use-after-free bug 2021-05-01 12:24:02 -04:00
Mike Ash
70354af2b5 [Runtime] Avoid operator new/delete in ConcurrentReadableArray.
The callbacks made in ImageInspectionMachO.cpp are called in a dangerous context, with the dyld and ObjC runtime locks held. C++ allows programs to overload the global operator new/delete, and there's no guarantee that those overloads behave. Ideally, we'd avoid them entirely, but that's a bigger job. For now, avoid the worst trouble by avoiding STL and new/delete in these callbacks. That use came from ConcurrentReadableArray's free list, so we switch that from a std::vector to a linked list.

rdar://75036800
2021-04-09 09:29:20 -04:00
Mike Ash
1557dbcb86 [Runtime] Make Concurrent.h ReaderCount loads seq_cst as well since they need to be ordered with the emplacement of new storage pointers. 2021-01-12 16:46:08 -05:00
Mike Ash
ef8d20a0e6 [Runtime] Fix incorrect memory ordering in ConcurrentReadableArray/HashMap.
When reallocating storage, the storing the pointer to the new storage had insufficiently strong ordering. This could cause writers to check the reader count before storing the new storage pointer. If a reader then came in between that load and store, it would end up using the old storage pointer, while the writer could end up freeing it.

Also adjust the Concurrent.cpp tests to test with a variety of reader and writer counts. Counterintuitively, when freeing garbage is gated on there being zero readers, having a single reader will shake out problems that having lots of readers will not. We were testing with lots of readers in order to stress the code as much as possible, but this resulted in it being extremely rare for writers to ever see zero active readers.

rdar://69798617
2021-01-12 10:41:56 -05:00
Mike Ash
13f84ddd7e [Runtime] Fix premature snapshot destruction in StableAddressConcurrentReadableHashMap::find.
Mark the relevant snapshot methods as ref-qualified (adding a & after the parameter list) so the compiler will enforce this for us and forbid calling them on temporaries.

rdar://72997638
2021-01-11 10:59:08 -05:00
Mike Ash
4ccfdfb72a [Runtime] Fix StableAddressConcurrentReadableHashMap threading crash.
StableAddressConcurrentReadableHashMap::getOrInsert had a race condition in the first lookup, where the snapshot was destroyed before the pointer was extracted from the returned wrapper. Fix this by creating the snapshot outside the if so that it stays alive.

rdar://problem/71932487
2020-12-21 16:52:56 -05:00
Mike Ash
768a085de8 Merge pull request #34598 from mikeash/os-unfair-lock-mutex
[Runtime] Use os_unfair_lock for Mutex and StaticMutex on Darwin.
2020-11-11 11:37:12 -05:00
Mike Ash
1b376cddf1 Merge pull request #34463 from mikeash/concurrentreadablehashmap-inline-indices
[Runtime] Have ConcurrentReadableHashMap store indices inline when the table is sufficiently small.
2020-11-10 14:59:16 -05:00
Mike Ash
e82d9e8c7b Move ConditionMutex to ConditionVariable::Mutex and move various other Mutex.h types to be nested. 2020-11-10 14:44:59 -05:00
Mike Ash
12d114713f [Runtime] Switch MetadataCache to ConcurrentReadableHashMap. (#34307)
* [Runtime] Switch MetadataCache to ConcurrentReadableHashMap.

Use StableAddressConcurrentReadableHashMap. MetadataCacheEntry's methods for awaiting a particular state assume a stable address, where it will repeatedly examine `this` in a loop while waiting on a condition variable, so we give it a stable address to accommodate that. Some of these caches may be able to tolerate unstable addresses if this code is changed to perform the necessary table lookup each time through the loop instead. Some of them store metadata inline and we assume metadata never moves, so they'll have to stay this way.

* Have StableAddressConcurrentReadableHashMap remember the last found entry and check that before doing a more expensive lookup.

* Make a SmallMutex type that stores the mutex data out of line, and use it to get LockingConcurrentMapStorage to fit into the available space on 32-bit.

rdar://problem/70220660
2020-11-04 15:10:50 -05:00
Mike Ash
782fa27206 [Runtime] Move ElementCapacity into the Elements allocation.
This shrinks ConcurrentReadableHashMap a bit, which will be needed for adapting it for metadata caches.
2020-10-28 10:23:21 -04:00
Mike Ash
9f53d4a52e [Runtime] Have ConcurrentReadableHashMap store indices inline when the table is sufficiently small. 2020-10-27 11:55:31 -04:00
Mike Ash
630aff7b19 [Runtime] Change SimpleGlobalCache to use ConcurrentReadableHashMap instead of ConcurrentMap.
This gives us faster lookups and a small advantage in memory usage. Most of these maps need stable addresses for their entries, so we add a level of indirection to ConcurrentReadableHashMap for these cases to accommodate that. This costs some extra memory, but it's still a net win.

A new StableAddressConcurrentReadableHashMap type handles this indirection and adds a convenience getOrInsert to take advantage of it.

ConcurrentReadableHashMap is tweaked to avoid any global constructors or destructors when using it as a global variable.

ForeignWitnessTables does not need stable addresses and it now uses ConcurrentReadableHashMap directly.

rdar://problem/70056398
2020-10-08 14:57:39 -04:00
Pavel Yaskevich
e6b140e32b [Runtime] NFC: Fix swift_runtime_unreachable to swift_unreachable 2020-10-08 11:54:12 -07:00
Mike Ash
ece0399d60 [Runtime] Have ConcurrentReadableHashMap use 1-byte or 2-byte indices when possible. 2020-09-24 15:28:18 -04:00
Mike Ash
0990fa90b3 Merge pull request #33647 from mikeash/shrink-concurrentreadablehashmap
[Runtime] Shrink ConcurrentReadableHashMap a bit.
2020-08-28 14:45:17 -04:00
Mike Ash
4cd1b715bd [Runtime] Shrink ConcurrentReadableHashMap a bit.
We're using a lot of space on the free lists. Each vector is three words, and we have two of them. Switch to a single linked list. We only need one list, as both kinds of pointers just get free()'d. A linked list optimizes for the common case where the list is empty. This takes us from six words to one.

Also make ReaderCount, ElementCount, and ElementCapacity uint32_ts. The size_ts were unnecessarily large and this saves some space on 64-bit systems.

While we're in there, add 0/NULL initialization to all elements. The current use in the runtime is unaffected (it's statically allocated) but the local variables used in the test were tripping over this.
2020-08-27 13:05:40 -04:00
Mike Ash
58604ff1ba [Runtime] Fix memory ordering on a store in ConcurrentReadableArray. 2020-08-26 13:24:44 -04:00
Mike Ash
6467827add [Runtime] Fix memory ordering on two loads in ConcurrentReadableHashMap. 2020-08-24 15:29:37 -04:00
Mike Ash
af35936589 [Runtime] Fix a race condition in ConcurrentReadableHashMap with concurrent reads and clears. 2020-08-22 10:51:04 -04:00
Mike Ash
ecd6d4ddec Add a new ConcurrentReadableHashMap type. Switch the protocol conformance cache
to use it.

ConcurrentReadableHashMap is lock-free for readers, with writers using a lock to
ensure mutual exclusion amongst each other. The intent is to eventually replace
all uses ConcurrentMap with ConcurrentReadableHashMap.

ConcurrentReadableHashMap provides for relatively quick lookups by using a hash
table. Rearders perform an atomic increment/decrement in order to inform writers
that there are active readers. The design attempts to minimize wasted memory by
storing the actual elements out-of-line, and having the table store indices into
a separate array of elements.

The protocol conformance cache now uses ConcurrentReadableHashMap, which
provides faster lookups and less memory use than the previous ConcurrentMap
implementation. The previous implementation caches
ProtocolConformanceDescriptors and extracts the WitnessTable after the cache
lookup. The new implementation directly caches the WitnessTable, removing an
extra step (potentially a quite slow one) from the fast path.

The previous implementation used a generational scheme to detect when negative
cache entries became obsolete due to new dynamic libraries being loaded, and
update them in place. The new implementation just clears the entire cache when
libraries are loaded, greatly simplifying the code and saving the memory needed
to track the current generation in each negative cache entry. This means we need
to re-cache all requested conformances after loading a dynamic library, but
loading libraries at runtime is rare and slow anyway.

rdar://problem/67268325
2020-08-20 13:05:30 -04:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Saleem Abdulrasool
3fa1d1fe3f runtime: ingest LLVMSupport into the runtime
This adds a new copy of LLVMSupport into the runtime.  This is the final
step before changing the inline namespace for the runtime support.  This
will allow us to avoid the ODR violations from the header definitions of
LLVMSupport.

LLVMSupport forked at: 22492eead218ec91d349c8c50439880fbeacf2b7
Changes made to LLVMSupport from that revision:
  process.inc forward declares `_beginthreadex` due to compilation issues due to custom flag handling

API changes required that we alter the `Deallocate` routine to account
for the alignment.

This is a temporary state, meant to simplify the process.  We do not use
the entire LLVMSupport library and there is no value in keeping the
entire library.  Subsequent commits will prune the library to the needs
for the runtime.
2020-05-15 09:55:36 -07:00
Mike Ash
a4863c4dcd [Runtime] Fix ConcurrentReadableArray's handling of FreeList to not double-free items. Also implement a destructor so it can be used for non-globals.
rdar://problem/40484362
2018-05-23 11:29:26 -04:00
Mike Ash
c7eeeb5a68 [Runtime] Change ConcurrentReadableArray's API to provide iterable snapshots rather than using a callback-based read call.
rdar://problem/40230581
2018-05-23 11:19:42 -04:00
Mike Ash
fdf67e80cc [Runtime] Include <vector> in Concurrent.h, since it uses std::vector.
rdar://problem/37173156
2018-05-16 14:44:49 -04:00
Mike Ash
da4fa67d7e [Runtime] Move ConcurrentReadableArray's allocate/deallocate functions into Storage. Mark ConcurrentReadableArray as un-copyable, un-assignable, and un-movable. Use SWIFT_MEMORY_ORDER_CONSUME instead of std::memory_order_consume. Make read() pass a const pointer.
rdar://problem/37173156
2018-05-15 15:27:33 -04:00
Mike Ash
68adaebf52 [Runtime] Properly set the count in newly allocated storage in ConcurrentReadableArray. Remove a redundant load of Count. Fix memory ordering on ReaderCount.
rdar://problem/37173156
2018-05-09 15:31:53 -04:00
Mike Ash
2a0c4bf368 [Runtime] Clean up and lightly document ConcurrentReadableArray. Check for malloc failure. Use placement new when appending values.
rdar://problem/37173156
2018-05-01 12:52:58 -04:00
Mike Ash
8b59295a92 [Runtime] Change protocol conformance scanning to use a concurrent array rather than a locked vector.
rdar://problem/37173156
2018-04-30 12:24:47 -04:00
John McCall
f304c321f3 Rewrite MetadataCache to be a more natural extension of ConcurrentMap.
Change generic witness table instantiation to use a more lightweight
entry scheme that allocates the witness table as part of the entry.

In contrast, change generic metadata instantiation to use a more
straightforward allocation scheme where the metadata is a totally
independent allocation.

This is preparation for proper cyclic-dependency handling.
2018-03-08 02:29:20 -05:00
Calvin Hill
aee81d272f Add Initial platform support for Haiku. (#11583) 2017-09-22 21:06:56 -04:00