Thanks to visit codestin.com
Credit goes to github.com

Skip to content

2 draw optimization -- pre-parse colors, short-circuit path construction code #3393

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

Closed
wants to merge 4 commits into from

Conversation

jbmohler
Copy link
Contributor

Profiling has revealed two small but noticeable hits in drawing performance. These are addressed by this PR. Each commit represents about a 4% improvement at draw-time and they add to 8%. I'm not sure if this passes tests so this is a travis-ci probe as well as a request for comment.

My timings were done like so:

from __future__ import print_function
import timeit
import numpy
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

POINTS = 500

figure = Figure(figsize=(6, 6), dpi=72, facecolor=(1, 1, 1), edgecolor=(0, 0, 0))
ax = figure.add_subplot(1, 1, 1)

scat = ax.scatter(numpy.arange(POINTS), numpy.sin(numpy.arange(POINTS)))

canvas = FigureCanvas(figure)

print(timeit.timeit('canvas.print_to_buffer()', setup='from __main__ import canvas', number=100))

@jbmohler jbmohler force-pushed the jbm_precompute_color branch from 748a62a to 32b932c Compare August 21, 2014 18:30
@mdboom
Copy link
Member

mdboom commented Aug 25, 2014

👍 from me.

@tacaswell
Copy link
Member

My only concern is that we have another set of PRs to remove assert statements.

Other than that, looks good to me. If I am understanding this correctly, this:

  • uses an existing API more smartly
  • unifies all (most?) of the internal color representation to rgba

@WeatherGod
Copy link
Member

I haven't looked at this PR yet, but one concern I have with messing around
in this area of the codebase is that we don't do nearly enough testing on
the ability to modify various properties such as colors at different times.
We should tread carefully here, I think.

On Mon, Aug 25, 2014 at 1:26 PM, Thomas A Caswell [email protected]
wrote:

My only concern is that we have another set of PRs to remove assert
statements.

Other than that, looks good to me. If I am understanding this correctly,
this:

  • uses an existing API more smartly
  • unifies all (most?) of the internal color representation to rgba


Reply to this email directly or view it on GitHub
#3393 (comment)
.

@jbmohler
Copy link
Contributor Author

As far as I know all internal color representation should be rgba tuples, "none" or "auto". I wasn't sure how to know that I got all of them, but I did some verifications that the expensive "to_rgba" wasn't called during the draw phase in various cases. I feel like the latter two are mildly odd special cases, but I think they make sense as specific indirections which aren't encoded by the rgba tuple.

I'm not attached to the asserts although they were useful to me to put my mind at ease that the rest of the code was behaving as I thought. (In general, I think it is distressing that most Pythoners seem to avoid asserts -- but that's a personal opinion. I think you should be compiling to .pyo as Guido intended if you want asserts not there at runtime :). )

@jbmohler
Copy link
Contributor Author

Oh, I should specify that the "none" and "auto" cases only are possible in the markeredgecolor, markerfacecolor, and markerfacecoloralt properties in lines.py.

@tacaswell tacaswell added this to the v1.5.x milestone Aug 26, 2014
self._rgb = colors.colorConverter.to_rgba(fg, self._alpha)
elif isRGBA:
self._rgb = fg
else:
self._rgb = colors.colorConverter.to_rgba(fg)
self._orig_color = self._rgb
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a change in behaviour to me. Previously, _orig_color stored the un-alphaified value, now it is stored after the application of alpha.

That said, I don't see why we need self._orig_color. This file is the only place it occurs, I think we can just drop it and use (the now badly named) self._rgb

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tacaswell , I agree that _orig_color should be deleted, but I say that with some trepidation since the relationships of _alpha, _forced_alpha, _rgb, _orig_color are scary to me in the code before this patch. Whether or not all 4 are geniunely needed, I don't know, but I'm sure they are named very sub-optimally. For instance, "_rgb" should really be "_rgba" as you note.

I'm going to investigate this point further right now.

Copy link
Member

Choose a reason for hiding this comment

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

I would advise writing a lot of unit tests.

Thinking about this on the train this morning what I suspect happened (and it might be worth doing some code forensics to check) is that originally mpl only did RGB and the alpha was added by setting RGB -> RGB * A rather than carrying around RGBA values (I have read a bit of the math behind how alpha blending works, but have not internalized it yet, but both schemes give the same result, but are more/less annoying to work with is different cases, or something...). So the naming _rgb is a hold over from that, as is _orig_color, which you need if you are doing self._rgb = self._rgb * alpha.

@tacaswell
Copy link
Member

Sorry for this languishing.

Please ping me if you push to this branch.

@tacaswell
Copy link
Member

@jbmohler It is starting to look like the color optimization is turning into a major re-write of undetermined scope (at a minimum this is going to involve a lot of unit tests). If you are willing to take this on, awsome and it should probably get it's own PR so we can get the line-related optimizations (or move those to their own PR).

This sort of thing is the rather boring and feels thankless, but we need it.

@jbmohler
Copy link
Contributor Author

See #3681 which is the Path related change in this PR.

@jbmohler jbmohler closed this Nov 18, 2014
@jbmohler jbmohler deleted the jbm_precompute_color branch November 18, 2014 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants