Skip to content

Commit

Permalink
Work around excessive stack space in non optimized builds during deco…
Browse files Browse the repository at this point in the history
…des.

Progress on: apple#1034
  • Loading branch information
thomasvl committed Aug 11, 2020
1 parent 55d6fde commit 5061489
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/protoc-gen-swift/MessageFieldGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
traitsArg = ""
}

p.print("case \(number): try decoder.\(decoderMethod)(\(traitsArg)value: &\(storedProperty))\n")
p.print("case \(number): try { try decoder.\(decoderMethod)(\(traitsArg)value: &\(storedProperty)) }()\n")
}

func generateTraverse(printer p: inout CodePrinter) {
Expand Down
9 changes: 7 additions & 2 deletions Sources/protoc-gen-swift/MessageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,19 @@ class MessageGenerator {
p.print("while let \(varName) = try decoder.nextFieldNumber() {\n")
p.indent()
if !fields.isEmpty {
p.print("switch fieldNumber {\n")

p.print(
"// The use of inline closures is to circumvents 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 fieldNumber {\n")
for f in fieldsSortedByNumber {
f.generateDecodeFieldCase(printer: &p)
}
if isExtensible {
p.print("case \(descriptor.swiftExtensionRangeExpressions):\n")
p.indent()
p.print("try decoder.decodeExtensionField(values: &_protobuf_extensionFieldValues, messageType: \(swiftFullName).self, fieldNumber: fieldNumber)\n")
p.print("try { try decoder.decodeExtensionField(values: &_protobuf_extensionFieldValues, messageType: \(swiftFullName).self, fieldNumber: fieldNumber) }()\n")
p.outdent()
}
p.print("default: break\n")
Expand Down
3 changes: 2 additions & 1 deletion Sources/protoc-gen-swift/OneofGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class OneofGenerator {
}

func generateDecodeFieldCase(printer p: inout CodePrinter, field: MemberFieldGenerator) {
p.print("case \(field.number):\n")
p.print("case \(field.number): try {\n")
p.indent()

if field.isGroupOrMessage {
Expand All @@ -335,6 +335,7 @@ class OneofGenerator {
"try decoder.decodeSingular\(field.protoGenericType)Field(value: &v)\n",
"if let v = v {\(storedProperty) = \(field.dottedSwiftName)(v)}\n")
p.outdent()
p.print("}()\n")
}

func generateTraverse(printer p: inout CodePrinter, field: MemberFieldGenerator) {
Expand Down

0 comments on commit 5061489

Please sign in to comment.