Skip to content

perf(apple): lend Swift method results to C++ instead of boxing them - #1992

Merged
mrousavy merged 1 commit into
mrousavy/v1-swift-result-consumerfrom
mrousavy/v1-lend-swift-method-results
Nov 2, 2026
Merged

mrousavy merged 1 commit into
mrousavy/v1-swift-result-consumerfrom
mrousavy/v1-lend-swift-method-results

Conversation

@mrousavy

@mrousavy mrousavy commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Stacked on #1991.

Throwing Swift methods returning Swift value types now lend successful results to SwiftResultConsumer. This avoids the outer swift::Optional<T> transport box used by withSwiftResult(). The generated bridges cover strings, arrays, optionals, enums, structs, records, variants and AnyJSON. Primitives, direct class results, imported C++ values, nonthrowing methods and callback adapters retain their existing paths.

The original pre-rebase Release simulator microbenchmark measured string concatenation at 151 -> 97 ns for "hello"/"world" and 303 -> 247 ns for two 32-character strings. These are narrow measurements, not a universal speedup: the new path still copies or retains values, and generic wrapper moves can introduce extra copies and allocations.

swiftlang/swift#92970 would remove common Optional wrapper allocations upstream, but it is still open. Rebenchmark this path when Nitro's minimum Xcode contains it. The separately linked #93321 addresses duplicate generated metadata, not Optional performance.

Rebased onto current v1 and regenerated all affected workspaces, including newer unsigned and shared-callback results. Nitrogen typecheck, lint and formatting passed. The iOS runtime suite passed with 25 tests passed, zero failures and one skipped.

@vercel

vercel Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
nitro-docs Skipped Skipped Nov 2, 2026 11:39am UTC

Request Review

Throwing Swift methods that return Swift values, such as strings, arrays and
structs, now pass the successful value to a SwiftResultConsumer while Swift
still owns it. Before, C++ called tryMakeSwiftValue(), which returned T?, and
C++ allocated a box for that swift::Optional<T> on every successful call.
@mrousavy
mrousavy force-pushed the mrousavy/v1-lend-swift-method-results branch from cb0c6bb to abbb432 Compare November 2, 2026 11:39
@mrousavy
mrousavy added this pull request to stack #2030 November 2, 2026 11:39
@mrousavy
mrousavy merged commit b371d08 into v1 Nov 2, 2026
12 of 18 checks passed

This branch was previously deployed

1 inactive deployment
Preview — abbb4328 Deployed Nov 2, 2026 by vercel[bot]
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