Skip to content

Sanchda/rust demangling#53

Merged
sanchda merged 3 commits into
DataDog:mainfrom
sanchda:sanchda/rust_demangling
Dec 29, 2023
Merged

Sanchda/rust demangling#53
sanchda merged 3 commits into
DataDog:mainfrom
sanchda:sanchda/rust_demangling

Conversation

@sanchda
Copy link
Copy Markdown
Contributor

@sanchda sanchda commented Dec 13, 2023

What does this PR do?:
Adds legacy Rust demangling to native code

Motivation:
I saw a flamegraph with tokio in the native parts and thought this might be useful.

Additional Notes:
Rust demangling isn't free, but I haven't really spent much time trying to speed it up. This was taken from a similar implementation in the native profiler, but adapted to C++11

How to test the change?:
I added tests. I believe this is fairly comprehensive, as it pulls from both llvm and libiberty.

For Datadog employees:

  • 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.

#pragma once
#include <string>

namespace RustDemangler {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh, namespace :)
First time I see it here. It's been a long time, I almost forgot you could do this in C++ 🤣

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Heh, mostly here I was afraid of conflicting with something else in the project (I'm not familiar with the codebase). Happy to remove if it would align with the design of the repo. 🙏

Copy link
Copy Markdown
Collaborator

@jbachorik jbachorik Dec 27, 2023

Choose a reason for hiding this comment

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

I don't think it is necessary to remove it - I assume it is a purely syntactic construct and won't result in linking more of libstdc++ to the final library ...

Comment thread ddprof-lib/src/main/cpp/rustDemangler.cpp
Comment thread ddprof-lib/src/main/cpp/rustDemangler.cpp
const char *end = ptr + str.size() - hash_pre.size() - hash_eg.size();
for (; ptr <= end; ++ptr) {
if (*ptr == '$') {
if (ptr[1] == '$') {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess this is safe because the string has the tailing hash and there never will be $ as the last char here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed, and also because end excludes the hash part, so we're guaranteed to have several characters of "string" left after any ptr.

Comment thread ddprof-lib/src/main/cpp/rustDemangler.cpp
} else {
// We didn't have valid unicode values, but we should still skip
// the $u??$ sequence
ret += str.substr(i, k_nb_read_chars);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this skipping the $u??$ sequence or copying it to the ret string?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question. It "skips" the $u???$ pattern in the sense that it doesn't perform additional processing on it, but it actually inserts the unprocessed $u???$ sequence into the destination string. In other words, tokens are only consumed when they are successfully matched, otherwise they are inserted literally into the result.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you extend the comment with 'inserted, not processed' part? Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 645fe69

Copy link
Copy Markdown
Collaborator

@jbachorik jbachorik left a comment

Choose a reason for hiding this comment

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

Hi, I have a bunch of noob questions. Please, be gentle :)

@sanchda sanchda merged commit e167e52 into DataDog:main Dec 29, 2023
@sanchda
Copy link
Copy Markdown
Contributor Author

sanchda commented Dec 29, 2023

Looks like none of the code quality nags were related to this PR. Merged

@jbachorik jbachorik added this to the 0.93.0 milestone Dec 29, 2023
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