public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
From: Antoine Pitrou <antoine@python.org>
To: gdb@sourceware.org
Subject: Tips for improving performance of Python pretty-printer?
Date: Tue, 11 Jan 2022 19:00:20 +0100	[thread overview]
Message-ID: <20220111190020.34ccf144@fsol> (raw)


Hello,

I'm implementing a bunch of pretty-printers (in Python) to improve the
debuggability of C++ types in Apache Arrow C++:
https://github.com/apache/arrow/pull/12092

However, I'm seeing performance issues where inspecting even relatively
simple information is quite slow as soon as I use the "natural" way, by
calling public C++ APIs (e.g. object methods) using
`gdb.parse_and_eval()`.

So for now I'm resorting to inspect private implementation details,
even for types I don't control such as `std::string` or `std::vector`.
As an example, I have the following helper code (simplified below for
clarity):

```
class SharedPtr:

    def __init__(self, val):
        self.val = val
        try:
            # libstdc++ internals
            self._ptr = val['_M_ptr']
        except gdb.error:
            # fallback for other C++ standard libraries
            self._ptr = gdb.parse_and_eval(
                f"{for_evaluation(val)}.get()")

    def get(self):
        return self._ptr


def for_evaluation(val):
    """
    Return a parsable form of gdb.Value `val`
    """
    ty = gdb.types.get_basic_type(val.type)
    if ty.code == gdb.TYPE_CODE_PTR:
        # It's already a pointer, can represent it directly
        return f"(({ty}) ({val}))"
    if val.address is None:
        raise ValueError(f"Cannot further evaluate rvalue: {val}")
    return f"(* ({ty}*) ({val.address}))"
```

This works fine but:

1) I need to maintain a generic fallback for non-GNU libstdc++
implementations of the C++ standard library
2) The generic fallback (obviously) suffers from the original
performance problem

Is it expected that `gdb.parse_and_eval` has such a poor performance?
(I didn't run any timings, but as a rough estimate I estimate that it
takes around 100 ms for a relatively simple expression)

Regards

Antoine.



             reply	other threads:[~2022-01-11 18:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-11 18:00 Antoine Pitrou [this message]
2022-01-11 19:22 ` David Blaikie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220111190020.34ccf144@fsol \
    --to=antoine@python.org \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).