Skip to content

Improve Qt object management and performance#14700

Merged
Carreau merged 3 commits intoipython:mainfrom
jdranczewski:main
Feb 2, 2025
Merged

Improve Qt object management and performance#14700
Carreau merged 3 commits intoipython:mainfrom
jdranczewski:main

Conversation

@jdranczewski
Copy link
Copy Markdown
Contributor

This PR tries to address perceived lag in the IPython console when the %gui qt eventloop is enabled (#14581) as well as wider management of Qt objects in said eventloop, initially addressed in #14240.

The lag is resolved by setting the timer for exiting the Qt eventloop to 10ms instead of 50ms. This is consistent with the Tk implementation and removes the observable lag on Windows.

I also took this opportunity to restructure the handling of Qt objects - the old implementation created a new QEventLoop and QTimer each time the inputhook was called. Since context.input_is_ready() pretty much always returns True after 10ms in my tests, this meant creating these objects anew 20 times per second (or 100 with this proposed timer patch). #14240 adds garbage collection for the QEventLoop, but not for the QTimer (which is a feature on Windows only).

I propose creating one instance of QEventLoop and QTimer initially, and then just reusing them. In my testing this doesn't improve performance (Qt is pretty quick at making new objects it seems), but it does avoid garbage collection issues.

Note that a QSocketNotifier is still created every iteration on POSIX platforms. I'm not sure if that is garbage collected or not. The value of context.fileno() could conceivably change, so we may need to recreate it as it is done now instead of reusing. I'm not sure, so I leave it as is.

Let me know if you would rather just change the timer to 10ms to fix #14581 and avoid the larger restructure in this PR. In that case we could only apply this commit: jdranczewski@e65cd11

Make the Qt timer shorter in line with the Tk one.
Reduces perceived lag when the inputhook is engaged.
ipython#14581
Instead or recreating an eventloop and timer 100 times a second, use a single instance throughout.
ipython#14240 introduced garbage collection in the past, but we don't need to garbage collect if we reuse the objects
@jdranczewski
Copy link
Copy Markdown
Contributor Author

jdranczewski commented Jan 30, 2025

(draft as I still need to test this patch on a Linux computer) can confirm my branch works on a Linux computer.

@jdranczewski jdranczewski marked this pull request as ready for review February 2, 2025 13:12
@Carreau Carreau added this to the 8.33 milestone Feb 2, 2025
@Carreau
Copy link
Copy Markdown
Member

Carreau commented Feb 2, 2025

Ok ,let's try it.

@Carreau Carreau merged commit 6022fcf into ipython:main Feb 2, 2025
@Carreau
Copy link
Copy Markdown
Member

Carreau commented Feb 2, 2025

thanks !

meeseeksmachine pushed a commit to meeseeksmachine/ipython that referenced this pull request Feb 2, 2025
Carreau added a commit that referenced this pull request Feb 3, 2025
…rformance) (#14714)

Backport PR #14700: Improve Qt object management and performance
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.

Enabling %matplotlib qt in IPython shell slows it down significantly

2 participants