Skip to content

Commit

Permalink
Work around excessive stack space in non optimized builds during oneo…
Browse files Browse the repository at this point in the history
…f enum equality.

Progress on: apple#1034
  • Loading branch information
thomasvl committed Aug 11, 2020
1 parent af44c67 commit fb6ff2d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Sources/protoc-gen-swift/OneofGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,21 @@ class OneofGenerator {
p.print(
"\(visibility)static func ==(lhs: \(swiftFullName), rhs: \(swiftFullName)) -> Bool {\n")
p.indent()
p.print("switch (lhs, rhs) {\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 (lhs, rhs) {\n")
for f in fields {
p.print("case (\(f.dottedSwiftName)(let l), \(f.dottedSwiftName)(let r)): return l == r\n")
p.print(
"case (\(f.dottedSwiftName), \(f.dottedSwiftName)): return {\n")
p.indent()
p.print(
"guard case \(f.dottedSwiftName)(let l) = lhs, case \(f.dottedSwiftName)(let r) = rhs else { preconditionFailure() }\n",
"return l == r\n")
p.outdent()
p.print(
"}()\n")
}
if fields.count > 1 {
// A tricky edge case: If the oneof only has a single case, then
Expand Down

0 comments on commit fb6ff2d

Please sign in to comment.