Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around excessive stack space in non optimized builds during oneof isInitialized #1042

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions Reference/unittest_swift_oneof_all_required.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,22 @@ struct ProtobufUnittest_OneOfContainer {
case option4(Int32)

fileprivate var isInitialized: Bool {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every case branch when no optimizations are
// enabled. https://github.com/apple/swift-protobuf/issues/1034
switch self {
case .option1(let v): return v.isInitialized
case .option2(let v): return v.isInitialized
case .option3(let v): return v.isInitialized
case .option1: return {
guard case .option1(let v) = self else { preconditionFailure() }
return v.isInitialized
}()
case .option2: return {
guard case .option2(let v) = self else { preconditionFailure() }
return v.isInitialized
}()
case .option3: return {
guard case .option3(let v) = self else { preconditionFailure() }
return v.isInitialized
}()
default: return true
}
}
Expand Down
13 changes: 11 additions & 2 deletions Sources/protoc-gen-swift/OneofGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,18 @@ class OneofGenerator {
"guard case \(f.dottedSwiftName)(let v) = self else {return true}\n",
"return v.isInitialized\n")
} else if fieldsToCheck.count > 1 {
p.print("switch self {\n")
p.print(
"// The use of inline closures is to circumvent an issue where the compiler\n",
"// allocates stack space for every case branch when no optimizations are\n",
"// enabled. https://github.com/apple/swift-protobuf/issues/1034\n",
"switch self {\n")
for f in fieldsToCheck {
p.print("case \(f.dottedSwiftName)(let v): return v.isInitialized\n")
p.print("case \(f.dottedSwiftName): return {\n")
p.indent()
p.print("guard case \(f.dottedSwiftName)(let v) = self else { preconditionFailure() }\n")
p.print("return v.isInitialized\n")
p.outdent()
p.print("}()\n")
}
// If there were other cases, add a default.
if fieldsToCheck.count != fields.count {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,22 @@ struct ProtobufUnittest_OneOfContainer {
case option4(Int32)

fileprivate var isInitialized: Bool {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every case branch when no optimizations are
// enabled. https://github.com/apple/swift-protobuf/issues/1034
switch self {
case .option1(let v): return v.isInitialized
case .option2(let v): return v.isInitialized
case .option3(let v): return v.isInitialized
case .option1: return {
guard case .option1(let v) = self else { preconditionFailure() }
return v.isInitialized
}()
case .option2: return {
guard case .option2(let v) = self else { preconditionFailure() }
return v.isInitialized
}()
case .option3: return {
guard case .option3(let v) = self else { preconditionFailure() }
return v.isInitialized
}()
default: return true
}
}
Expand Down