Increase size of stack

Here's a tiny example program that illustrates the severity of the stack size limitation:

import Dispatch
struct TenOf<T> { let t1, t2, t3, t4, t5, t6, t7, t8, t9, t10: T }
typealias LakhOf<T> = TenOf<TenOf<TenOf<TenOf<TenOf<T>>>>> // 100K
let onmain: LakhOf<Int64>? = nil // works on main thread…
print("size of lakh:", MemoryLayout.size(ofValue: onmain)) // 800_001
DispatchQueue.concurrentPerform(iterations: 2) { i in
    let onq: LakhOf<Int64>? = nil // …but crashes on dispatch queue!
    print("size of lakh (on queue):", MemoryLayout.size(ofValue: onq))
}

The proposed work-around of using ones' own Thread instead of GCD isn't viable in many cases, because you may be working with with built-in frameworks that use GCD, such as Foundation networking. Deserializing JSON into potentially large structs from a network callback is very common, and having to fork a new thread from a perfectly good GCD queue is not a viable work-around for high-performance systems.

And the other work-around of refactoring data models from value types to classes is infeasible, to say the least.

If changing a queue's stack size at runtime presents challenges (and I see why it would), why can't there be some environment variable that controls the default stack size, like Java's -Xss flag?

4 Likes