From dba676141ed6cf593befe9b82867571661ee4b94 Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Mon, 29 Jul 2024 11:39:08 +0200 Subject: [PATCH] Hide IPython internal frame by default. This should help with #313 In most case it should have no effects as anyway all the root of the tree is removed as there is no time spent in it. Though with some compiled code and threads, some time may be affected to the root. --- pyinstrument/processors.py | 13 +++++++++++++ pyinstrument/renderers/console.py | 1 + pyinstrument/renderers/html.py | 1 + pyinstrument/session.py | 5 +++++ 4 files changed, 20 insertions(+) diff --git a/pyinstrument/processors.py b/pyinstrument/processors.py index 6894b44b..d5320a94 100644 --- a/pyinstrument/processors.py +++ b/pyinstrument/processors.py @@ -294,3 +294,16 @@ def is_runpy_frame(frame: Frame): result.remove_from_parent() return result + + +def group_ipython_frames_processor(frame, options: ProcessorOptions) -> Frame | None: + """ + In some case the IPython's magic show all the internal IPython frames, which makes + traces hard to read. + + This will collapse most of IPython internal frames. + """ + return group_library_frames_processor( + frame, + {"hide_regex": ".+((IPython)|(ipykernel.zmqshell)|(pyinstrument.magic.magic))"}, + ) diff --git a/pyinstrument/renderers/console.py b/pyinstrument/renderers/console.py index 7ed88fde..d47b96b6 100644 --- a/pyinstrument/renderers/console.py +++ b/pyinstrument/renderers/console.py @@ -216,6 +216,7 @@ def default_processors(self) -> ProcessorList: processors.remove_irrelevant_nodes, processors.remove_first_pyinstrument_frames_processor, processors.group_library_frames_processor, + processors.group_ipython_frames_processor, ] class colors_enabled: diff --git a/pyinstrument/renderers/html.py b/pyinstrument/renderers/html.py index 53bf3fa6..41d5577e 100644 --- a/pyinstrument/renderers/html.py +++ b/pyinstrument/renderers/html.py @@ -100,4 +100,5 @@ def default_processors(self) -> ProcessorList: processors.remove_irrelevant_nodes, processors.remove_first_pyinstrument_frames_processor, processors.group_library_frames_processor, + processors.group_ipython_frames_processor, ] diff --git a/pyinstrument/session.py b/pyinstrument/session.py index 45f56ca3..864710cf 100644 --- a/pyinstrument/session.py +++ b/pyinstrument/session.py @@ -127,6 +127,11 @@ def root_frame(self, trim_stem: bool = True) -> Frame | None: if trim_stem: root_frame = self._trim_stem(root_frame) + group_library_frames_processor( + root_frame, + {"hide_regex": ".+((IPython)|(ipykernel.zmqshell)|(pyinstrument.magic.magic))"}, + ) + return root_frame def _trim_stem(self, frame: Frame):