Improving Swift code for scalability

Technique 6: only create indexed references through an allocation routine, accessed by a method on a structure that groups all such synchronized tables; template that code through whatever means necessary

By centralizing the production of IndexedReference instances, you can be sure that the relevant arrays are as large as they need to be, and kept in sync. In the ideal case, the lifecycle of allocations is last-out-first-in, such that deallocations can just symmetrically shrink the arrays; in more general situations, more techniques (free list, invalidated element state, etc.) may be necessary.

extension PoolWith2TablesNonGCD
{
    mutating func place(_ elemA: ElementA, _ elemB: ElementB) -> IndexedReferenceNonGCD<Target, WritableNonGCD> {
        guard self[keyPath: Self.aTablePath].g.count == self[keyPath: Self.bTablePath].g.count else {
            fatalError("Mismatch in table lengths within a single pool");
        }
        
        self[keyPath: Self.aTablePath].g.append(elemA);
        self[keyPath: Self.bTablePath].g.append(elemB);
        
        return .init(g: UInt32(self[keyPath: Self.aTablePath].g.count) - 1);
    }
    
    mutating func remove<R: AccessRightsNonGCD>(recovering indexedRef: IndexedReferenceNonGCD<Target, R>) {
        guard self[keyPath: Self.aTablePath].g.count == self[keyPath: Self.bTablePath].g.count else {
            fatalError("Mismatch in table lengths within a single pool");
        }
        
        guard (UInt32(self[keyPath: Self.aTablePath].g.count) - 1) == indexedRef.g else {
            fatalError("Indexed reference deallocated out of order");
        }

        _ = self[keyPath: Self.aTablePath].g.popLast();
        _ = self[keyPath: Self.bTablePath].g.popLast();
    }

You really don't want to spell that out multiple times, so I eventually expect to use property packs (for now only a future direction on parameter packs); for now, a protocol extension will suffice.

But won't these array grow/shrink operations cause dynamic allocations? Well, in most cases you can predict the maximum size it can possibly have, and if you pass that to reserveCapacity(), no actual allocation will occur after that; meanwhile, by having the element not exist until you need to allocate it, you reap some benefits such as definite initialization.

Addendum: and in order to prevent indexes from being forged, you should make their sole property immutable (let) and restrict their general initializer to be fileprivate, so that only this allocation method can create them ex nihilo.

This technique is best seen in action, as of the current version as of this writing, in the context of this patch:

diff --git a/AsyncCountdown/AsyncExplorers.swift b/AsyncCountdown/AsyncExplorers.swift
--- a/AsyncCountdown/AsyncExplorers.swift
+++ b/AsyncCountdown/AsyncExplorers.swift
@@ -38,6 +38,7 @@
                                      _ dispatcher: DispatcherAsync,
                                      kind: Op,
                                      recruitmentLoc: PotentiallyEvenUndefinedIndexedReferenceNonGCD<CountdownArrayCellNonGCD, WritableNonGCD> = .init(CountdownArrayCellNonGCD.self, rights: WritableNonGCD.self)) async rethrows {
+
     resolveCoreCtx.l.push(&resolveCoreCtx.mainTable,
                           resolveCoreCtx.place(.init(.placeholder(op: kind)),
                                                .init(w: .init(sentinelFor: CountdownArrayCellNonGCD.self, rights: ReadOnlyNonGCD.self))