public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* Tips for improving performance of Python pretty-printer?
@ 2022-01-11 18:00 Antoine Pitrou
  2022-01-11 19:22 ` David Blaikie
  0 siblings, 1 reply; 2+ messages in thread
From: Antoine Pitrou @ 2022-01-11 18:00 UTC (permalink / raw)
  To: gdb


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.



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Tips for improving performance of Python pretty-printer?
  2022-01-11 18:00 Tips for improving performance of Python pretty-printer? Antoine Pitrou
@ 2022-01-11 19:22 ` David Blaikie
  0 siblings, 0 replies; 2+ messages in thread
From: David Blaikie @ 2022-01-11 19:22 UTC (permalink / raw)
  To: Antoine Pitrou; +Cc: gdb

One side note: Generally pretty printers should not call into the code
under test - doing so means the pretty printer can't be used when debugging
a core dump, and risks not being usable when the process is corrupted in
some ways, or risks creating corruption/disturbing a reproduction by
executing code in the test program.

On Tue, Jan 11, 2022 at 10:05 AM Antoine Pitrou via Gdb <gdb@sourceware.org>
wrote:

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-11 19:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-11 18:00 Tips for improving performance of Python pretty-printer? Antoine Pitrou
2022-01-11 19:22 ` David Blaikie

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