Skip to content

Various cleanups to backends code. #8711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2017

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jun 3, 2017

Removed some debug statements and old comments; whitespace / line
wrapping (to 79 characters).

(FWIW the debug code in backend_gtkcairo is apparently broken -- it occasionally refers to a function fn_name which doesn't exist (it's _fn_name))

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/whats_new.rst if major new feature
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer anntzer force-pushed the backends-cleanup branch from ed2d37c to e2b6d33 Compare June 3, 2017 22:23
Removed some debug statements and old comments; whitespace / line
wrapping.

In particular, debug comments which simply indicate that
a function has been called can easily be replaced by
tools such as https://pypi.python.org/pypi/hunter or
https://pypi.python.org/pypi/smiley.
@anntzer anntzer force-pushed the backends-cleanup branch from e2b6d33 to 22488ff Compare June 4, 2017 02:50
else: self.canvas._tkcanvas.delete(self.lastrect)
y0 = height - y0
y1 = height - y1
if hasattr(self, "lastrect"):
Copy link
Member

Choose a reason for hiding this comment

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

This is a hot-path for UI, Under normal usage I think we expect this to exist for often that in does not so the try..except pattern is marginally faster.

def tester(obj):
    if hasattr(obj, 'string'):
        pass

def tryer(obj):
    try:
        obj.string
    except:
        pass


class A:
    def __init__(self):
        self.string = 'foo'

class B:
    ...
In [61]: %timeit tester(1)
459 ns ± 3.11 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [62]: %timeit tryer(1)
524 ns ± 0.955 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [63]: 
In [64]: a = A()

In [65]: %timeit tryer(a)
123 ns ± 0.202 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [66]: %timeit tester(a)
194 ns ± 0.281 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [67]: 
In [68]: b = B()

In [69]: %timeit tryer(b)
488 ns ± 1.11 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [70]: %timeit tester(b)
423 ns ± 2.14 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [74]: 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The following patch

diff --git a/lib/matplotlib/backends/backend_tkagg.py b/lib/matplotlib/backends/backend_tkagg.py
index af0ed62f9..4d212efe8 100644
--- a/lib/matplotlib/backends/backend_tkagg.py
+++ b/lib/matplotlib/backends/backend_tkagg.py
@@ -736,7 +736,14 @@ class NavigationToolbar2TkAgg(NavigationToolbar2, Tk.Frame):
         try: self.lastrect
         except AttributeError: pass
         else: self.canvas._tkcanvas.delete(self.lastrect)
+        from time import perf_counter
+        start = perf_counter()
+        elapsed_dummy = perf_counter() - start
+        start = perf_counter()
         self.lastrect = self.canvas._tkcanvas.create_rectangle(x0, y0, x1, y1)
+        elapsed_draw = perf_counter() - start
+        elapsed_corrected = elapsed_draw - elapsed_dummy
+        print(f"create_rectangle took {elapsed_corrected * 1e6}us")

shows that the call to create_rectangle that's just below takes ~100-300us, so I think worrying about 50ns for the sake of clarity is totally overkill.

@tacaswell
Copy link
Member

👍 modulo the hot-path concern.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jun 9, 2017
@tacaswell
Copy link
Member

The check is not the slowest thing in this code path, so 👍

@QuLogic QuLogic merged commit 90ded74 into matplotlib:master Jun 9, 2017
@anntzer anntzer deleted the backends-cleanup branch June 10, 2017 01:29
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.

3 participants