Skip to content

Optimize buffering in log_to_sinks #330

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

Merged
merged 5 commits into from
Apr 1, 2025

Conversation

eloff
Copy link
Contributor

@eloff eloff commented Mar 31, 2025

Changelog

Improve logging performance by ~20%

Description

This actually simplifies the code. I had to bring in the SmallBytes crate for a BufMut wrapper around SmallVec.

Copy link

linear bot commented Mar 31, 2025

@eloff eloff requested a review from gasmith March 31, 2025 20:06
Copy link
Contributor

@gasmith gasmith left a comment

Choose a reason for hiding this comment

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

Feels like this might be a nice change to bring in divan, so we can see some before & after numbers.

@@ -32,6 +32,7 @@ serde_repr = "0.1.19"
serde_with = { version = "3.12.0", features = ["macros", "base64"] }
serde.workspace = true
smallvec = "1.14.0"
smallbytes = "0.1.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, this crate appears to be fresh abandonware...

... but it looks like the stars have aligned, and impl BufMut for SmallVec was recently added to smallvec, to be released with v2.0.0!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice. I think let's start with this and then we can remove the smallbytes dependency later.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. Let's file a linear ticket so we don't forget.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added FG-10965

}
Err(_) => {
// Likely the stack buffer was too small, so fall back to a heap buffer.
let mut size = msg.encoded_len().unwrap_or(STACK_BUFFER_SIZE * 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little surprised we don't benefit more from the encoded_len() hint. Which message sizes did you benchmark against?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was testing with 440 kg message sizes.

In practice what happens is everything goes into the stack buffer, then the data field is serialized, it either fits or it's too big and it grows the buffer to fit. Remaining fields won't be enough to cause it to grow again.

I think we could still use encoded_len with reserve or similar, but it doesn't make a difference.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, what if you had several big fields? Couldn't each one theoretically trigger a new allocation as the serializer attempts to write them, one after another?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a call to reserve(). It might be a little slower in some cases as encoded_len() is recursive and not that cheap for prost. Should be minor compared to the memory copy cost.

@eloff eloff merged commit 4bab291 into main Apr 1, 2025
38 checks passed
@eloff eloff deleted the dan/fg-10961-rust-sdk-optimize-buffering-for-logging branch April 1, 2025 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants