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

perf: generate switch statement for oneof union encode #767

Merged
merged 2 commits into from
Jan 31, 2023

Conversation

antsluts
Copy link
Contributor

Updates the encode method to generate a switch statement for oneof unions. These are currently generating as if statements for every possible $case so not great for performance as the number of cases grows.

In our usage scenario we have a oneof with 160+ cases, which is currently generating 160+ if statements and during encode each is evaluated!!!

From benchmarking against our internal .proto message definition the switch version achieves ~2.8x better performance over the current if version.

Before

export const Value = {
  encode(message: Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.kind?.$case === "null_value") {
      writer.uint32(8).int32(message.kind.null_value);
    }
    if (message.kind?.$case === "number_value") {   // not an 'else if' so evaluate regardless if matched already
      writer.uint32(17).double(message.kind.number_value);
    }
    if (message.kind?.$case === "string_value") {   // not an 'else if' so evaluate regardless if matched already
      writer.uint32(26).string(message.kind.string_value);
    }
    ...

After

export const Value = {
  encode(message: Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    switch (message.kind?.$case) {
      case "null_value":
        writer.uint32(8).int32(message.kind.null_value);
        break;
      case "number_value":
        writer.uint32(17).double(message.kind.number_value);
        break;
      case "string_value":
        writer.uint32(26).string(message.kind.string_value);
        break;
     ...

Previously each oneof union case generated its own `if` statement in the
encode method which becomes very inefficient with more cases as each of
the conditions needed to be evaluated but only one would ever be true.
They were not even `else if (...)` so once a match was found the rest of
the `if` statements would still continue to be evaluated.
@antsluts antsluts changed the title Perf: generate switch statement for oneof union encode perf: generate switch statement for oneof union encode Jan 31, 2023
@stephenh
Copy link
Owner

Looks great, thanks @antsluts !

@stephenh stephenh merged commit c3fd1e3 into stephenh:main Jan 31, 2023
stephenh pushed a commit that referenced this pull request Jan 31, 2023
# [1.139.0](v1.138.0...v1.139.0) (2023-01-31)

### Features

* add support for Struct in NestJS ([#762](#762)) ([e8c6d8b](e8c6d8b))

### Performance Improvements

* generate switch statement for oneof union encode ([#767](#767)) ([c3fd1e3](c3fd1e3))
@stephenh
Copy link
Owner

🎉 This PR is included in version 1.139.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants