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

Discourage + string concatenation from FileDescriptors #239

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

seanreid-toast
Copy link

@seanreid-toast seanreid-toast commented Mar 21, 2024

Why?

Due to a bug in the Kotlin Compiler, there's a high chance of stack overflow with protos medium+ sized protos (example tested: 1 service, ~20 RPCs, ~45 messages). When concatenating using the + operator in Kotlin, the complier can throw a stack overflow exception when generating the bytecode.

What changed?

This change is simple: it just makes the file descriptors into one long string rather than generating a broken string with + concatenation operators. This allows for larger protos before hitting a stack overflow exception.

Testing

All existing tests pass, including tests that validate descriptors match between ProtoKt and protobuf-java. Manual testing was conducted locally to verify that the proto that originally exposed this issue still caused a stack overflow on a branch without this change, but does not with this change

New large proto test

There's a new large proto in this PR that has 600 fields. It passes all tests. >600 seems to fail to build with the same stack overflow that started this work in the first place. 600 is significantly larger than was previously supported.

.joinToCode(" +\n")
}
.joinToCode(",\n")
private fun descriptorLines(): CodeBlock {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This looks a lot like String.embed in KotlinPoetUtils. Is that usable here?
  • If we're just concatenating all the file descriptor parts, is there any need for it to be a list of strings? Should it just return a CodeBlock?

@@ -207,7 +207,11 @@ private class MessageGenerator(
} else {
buildCodeBlock {
add("return \"%L(\" +\n", msg.className.simpleName)
toStringLines(properties).forEach(::add)
if (properties.size < 100) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this getting around a problem? How does this (or its absence) interact with the ktlint integration we use to clean up generated code?

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.

2 participants