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
im caching in draw_tex #796
Comments
@mdboom, John's comment is still valid, and applies to macosx as well. What do you think: should the caching be implemented, or should the cache-related code be deleted? The deletion looks trivial, but I don't know what would be required to make the caching work. I suspect that would be obvious to you, though. |
This must have just been accidentally disengaged at some point. The caching is pretty trivial to implement (we use this pattern in a number of other places): diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py
index 3ba1e57..dc9534a 100644
--- a/lib/matplotlib/backends/backend_agg.py
+++ b/lib/matplotlib/backends/backend_agg.py
@@ -234,7 +234,8 @@ class RendererAgg(RendererBase):
im = self.texd.get(key)
if im is None:
Z = texmanager.get_grey(s, size, self.dpi)
- Z = np.array(Z * 255.0, np.uint8)
+ im = np.array(Z * 255.0, np.uint8)
+ self.texd[key] = im
w, h, d = self.get_text_width_height_descent(s, prop, ismath)
xd = d * np.sin(np.deg2rad(angle))
@@ -242,7 +243,7 @@ class RendererAgg(RendererBase):
x = np.round(x + xd)
y = np.round(y + yd)
- self._renderer.draw_text_image(Z, x, y, angle, gc)
+ self._renderer.draw_text_image(im, x, y, angle, gc)
def get_canvas_width_height(self):
'return the canvas width and height in display coords' However, I don't know if it matters a whole lot. I wrote a quick benchmark that renders the same figure 500 times where the text doesn't change (which is often the case in many animations -- obviously the case where the text is changing a lot do not benefit from the cache). I actually couldn't measure any significant change on repeated runs (other things are really dominant). The margin of error was such that some runs with cache were slower than runs without cache. I'm actually very surprised by the result, so please double check my work. (This is on Linux. It might matter more on platforms where creating processes was more expensive.) Here's the benchmark:
So, in favor of simplicity, I guess I'm in favor of removing the cache, unless I've made an error in the benchmarking here. |
The moral of the story is that you should never implement optimisations unless you've measured the problem in the first place. Python often throws up surprising results with regards to performance, and more often than not I've found my own intuition is wrong. I haven't verified your results, but matplotlib doesn't currently have the caching working, and there is no clear benefit to implementing is, so lets take this opportunity to simplify this tiny bit of code, and just delete the if statement. Smiles all round! Thanks for bringing this up again @efiring. |
It's also important to remember that this code goes back 9-10 years. It may have mattered then, and doesn't now. It's quite unlikely that John (who I think wrote this) was using a quad core machine with an SSD as I am today (both of which probably matter quite a bit for spawning large subprocesses). |
The code in question moved to : https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_agg.py#L249 |
This method is really confusing me (https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_agg.py#L199):
Specifically, if "im" is not None after the texd.get call, then "Z" is not defined in the draw_text_image call. That we are not seeing failures suggests the im is never getting cached and perhaps we should just remove this logic.
The text was updated successfully, but these errors were encountered: