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

Optimization opportunity: encoded_len gets computed twice when encoding a message #1112

Open
ivoanjo opened this issue Jul 30, 2024 · 2 comments

Comments

@ivoanjo
Copy link

ivoanjo commented Jul 30, 2024

Hey 👋! I work at Datadog and we use prost in our libdatadog rust component.

While looking at some performance profiling of our protobuf encoding, I noticed that there's quite a bit of repeated work between encode and encode_raw since both need encoded_len, and it gets computed twice:

image

Specifically, Message::encode computes the encoded_len to check if the buffer has enough space, and then throws this info away, and then encode_raw gets the number again as it needs to record the field size in the pprof.

Computing encoded_len is around 5% of the time of this benchmark, so there would be a nice gain in encoding speed if it was computed only once.

We're happy users of prost so I decided to report this in the spirit of "see something, say something".

Thanks again for your amazing work so far 🙏

@caspermeijn
Copy link
Collaborator

Thanks, that is great that you found this. Do you have a suggestion on how to fix this? Are you willing to create a PR for that?

@ivoanjo
Copy link
Author

ivoanjo commented Jul 31, 2024

Thanks, that is great that you found this. Do you have a suggestion on how to fix this?

I would probably go for either introducing a encode_raw_with_len that receives the len as an argument or modifying the existing encode_raw to always then the len as an argument (I'm not sure where else it gets used).

Are you willing to create a PR for that?

The million dollar question 😅. I'm working on a few other bigger optimizations on our codebase first, and I'm not sure when I'll have enough time to circle back to this one.

So I'll keep it on my radar, but it may take a while -- this is why I decided to report, so it wasn't forgotten/and in case someone wanted to pick it up faster than I can :)

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

No branches or pull requests

2 participants