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: less mem per entry in the string table #303

Merged
merged 6 commits into from
Feb 13, 2024
Merged

Conversation

morrisonlevi
Copy link
Contributor

@morrisonlevi morrisonlevi commented Feb 5, 2024

What does this PR do?

This uses a Box<str> instead of String, saving 1 usize per entry. When it's serialized to pprof proto, it gets encoded directly without allocating a temporary String.

Motivation

I'm working on reducing memory and CPU overhead of profiles and noticed this pretty cheap reduction in memory.

Additional Notes

I've opened a PR upstream to put a generic str encoder helper there, as I think others could benefit from it as well. This part enables future optimizations like using a memory arena for string table data.

PROF-9125

How to test the change?

Test the same as usual, public API should be the same.

For Reviewers

  • If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.

@github-actions github-actions bot added the profiling Relates to the profiling* modules. label Feb 5, 2024
This uses a `Box<str>` instead of `String`, saving 1 `usize` per
entry. When it's serialized to pprof proto, it gets encoded directly
without allocating a temporary `String`. This currently is waiting
on a PR in prost to expose the needed function to directly encode
`AsRef<str>` instead of `&String`. This part enables future
optimizations like using a memory arena for string table data.

PROF-9125
@ivoanjo
Copy link
Member

ivoanjo commented Feb 6, 2024

Nice! :D

The PR didn't receive immediate attention from a maintainer, so let's
not stay blocked when it's such a small thing.
@morrisonlevi morrisonlevi marked this pull request as ready for review February 9, 2024 17:41
@morrisonlevi morrisonlevi requested a review from a team as a code owner February 9, 2024 17:41
Comment on lines -88 to -93
#[derive(Eq, Hash, PartialEq, ::prost::Message)]
pub struct ProfileStringTableEntry {
#[prost(string, required, tag = "6")]
pub string_table_entry: String,
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Why the change to do this differently from all the other field serializers?

Copy link
Contributor Author

@morrisonlevi morrisonlevi Feb 9, 2024

Choose a reason for hiding this comment

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

Because prost::Message requires a String to encode strings. You can't encode any other String-like representations such as &str, Box<str>, etc with a prost::Message. Doing it this way avoids an unnecessary conversion to String. A Box<str> into String is cheap, but in the next iteration the string data will be arena-allocated so it doesn't make sense to invest too much into Box<str> when we don't need anything more than this PR for the longer-term goal, and we'd throw away the Box<str> specific stuff.

@morrisonlevi morrisonlevi changed the title feat: less mem per entry in the string table perf: less mem per entry in the string table Feb 9, 2024
@morrisonlevi morrisonlevi merged commit b232f14 into main Feb 13, 2024
20 checks passed
@morrisonlevi morrisonlevi deleted the levi/box-str branch February 13, 2024 15:20
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request Mar 19, 2024
**What does this PR do?**

This PR upgrades dd-trace-rb to use libdatadog 7.

There was a small breaking API change -- the rename of `ddog_Endpoint`
APIs to `ddog_prof_Endpoint`.

**Motivation:**

Make sure Ruby is up-to-date on libdatadog.

**Additional Notes:**

As far as Ruby is impacted, the main changes in libdatadog 7 are
a number of fixes to the crash tracker (getting us closer to merging
 #3384) as well as some potential memory improvements from
(DataDog/libdatadog#303).

I'm opening this as a draft PR as libdatadog 7 is not yet available on
rubygems.org, and I'll come back to re-trigger CI and mark this as
non-draft once it is.

**How to test the change?**

Our existing test coverage includes libdatadog testing, so a green CI
is good here :)
ivoanjo added a commit to DataDog/dd-trace-rb that referenced this pull request Mar 27, 2024
**What does this PR do?**

This PR upgrades dd-trace-rb to use libdatadog 7.

There was a small breaking API change -- the rename of `ddog_Endpoint`
APIs to `ddog_prof_Endpoint`.

**Motivation:**

Make sure Ruby is up-to-date on libdatadog.

**Additional Notes:**

As far as Ruby is impacted, the main changes in libdatadog 7 are
a number of fixes to the crash tracker (getting us closer to merging
 #3384) as well as some potential memory improvements from
(DataDog/libdatadog#303).

I'm opening this as a draft PR as libdatadog 7 is not yet available on
rubygems.org, and I'll come back to re-trigger CI and mark this as
non-draft once it is.

**How to test the change?**

Our existing test coverage includes libdatadog testing, so a green CI
is good here :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
profiling Relates to the profiling* modules.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants