Skip to content

[Runtime] Reduce the amount of stack space used in recursive protocol conformance checking.#90558

Open
mikeash wants to merge 1 commit into
swiftlang:mainfrom
mikeash:protocol-conformance-less-stack
Open

[Runtime] Reduce the amount of stack space used in recursive protocol conformance checking.#90558
mikeash wants to merge 1 commit into
swiftlang:mainfrom
mikeash:protocol-conformance-less-stack

Conversation

@mikeash

@mikeash mikeash commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Deeply nested types can run out of stack space in protocol conformance checks. Apply a few strategies to reduce how much space each level of recursion needs, to allow for deeper types.

  • Use llvm::function_ref instead of std::function for some of the callbacks.

  • Reduce the descriptorPath SmallVector's inline size from 8 to 2.

  • Outline a swift_getTypeByMangledName call that requires building std::functions.

  • Change the conformance descriptor scan loop to keep less information live during the ConformanceLookupResult::fromConformance call. This call can recurse, and it's in the middle of a loop with a lot of state. We can instead gather the matching conformance descriptors, then extract the info from them afterwards. This allows us to collect the results in a SmallVector of pointers rather than a SmallDenseMap of larger types. It also simplifies the code a bit, as we can control whether we can use scanSectionsBackwards to decide to pick the first or last match, rather than pulling the scan loop into a lambda and hoping it optimizes well.

rdar://181059210

… conformance checking.

Deeply nested types can run out of stack space in protocol conformance checks. Apply a few strategies to reduce how much space each level of recursion needs, to allow for deeper types.

* Use llvm::function_ref instead of std::function for some of the callbacks.

* Reduce the descriptorPath SmallVector's inline size from 8 to 2.

* Outline a swift_getTypeByMangledName call that requires building std::functions.

* Change the conformance descriptor scan loop to keep less information live during the ConformanceLookupResult::fromConformance call. This call can recurse, and it's in the middle of a loop with a lot of state. We can instead gather the matching conformance descriptors, then extract the info from them afterwards. This allows us to collect the results in a SmallVector of pointers rather than a SmallDenseMap of larger types. It also simplifies the code a bit, as we can control whether we can use scanSectionsBackwards to decide to pick the first or last match, rather than pulling the scan loop into a lambda and hoping it optimizes well.

rdar://181059210
@mikeash mikeash requested a review from al45tair as a code owner July 9, 2026 21:45
@mikeash

mikeash commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@swift-ci please benchmark

@mikeash mikeash requested a review from carlpeto July 9, 2026 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant