Skip to content
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

Reduce the use of XObjects in pdf backend [backport to 1.4.x] #3485

Merged
merged 7 commits into from Oct 19, 2014

Conversation

jkseppan
Copy link
Member

@jkseppan jkseppan commented Sep 7, 2014

Fixes #3345, fixes #2910. Fixes the pdf part of #2247, and it seems to me that the svg part is already fixed on master.

Add a way for backends to query how many times each
path in a collection can be reused.
@jkseppan
Copy link
Member Author

jkseppan commented Sep 7, 2014

The failing tests are:

mpl_toolkits.tests.test_mplot3d.test_surface3d.test
mpl_toolkits.tests.test_mplot3d.test_mixedsubplots.test

In both cases, the pdf output fails to match the expected output by omitting some contour lines that are not there in the Agg output but are in the old pdf output. I'd say that's an improvement, but it's not yet clear to me why this change would bring it about.

Here's the Agg output for test_surface3d:

surface3d

And the resulting pdf:

surface3d_pdf

Compare to the expected pdf:

surface3d-expected_pdf

And test_mixedsubplots, Agg:

mixedsubplot

Pdf:

mixedsubplot_pdf

Expected pdf:

mixedsubplot-expected_pdf

These look closer to their png counterparts than the old ones.
There probably is a remaining bug in the drawing of edges.
@tacaswell
Copy link
Member

👍 for fixing a bug we didn't know we had.

I would advocate not merging this until we understand why this fixed a bug we didn't know we had.

@mdboom
Copy link
Member

mdboom commented Sep 8, 2014

Thanks for this -- I think this is a really helpful optimization of an optimization.

I think the bug is related to the edge color not getting cleared for every XObject (since every patch in the 3d surface was an XObject before this patch). So I think it means we still technically have the bug if we trigger the case where XObjects are being used. I'll poke around and see if I can trigger that case and fix it.

@mdboom
Copy link
Member

mdboom commented Sep 8, 2014

jkseppan#3 has a fix for the underlying bug.

@jkseppan
Copy link
Member Author

jkseppan commented Sep 8, 2014

Thanks @mdboom! I merged your addition into this pull request.

There's still the question of why draw_path_collection was being called with collections of one path each. That could have consequences for other backends as well.

@jkseppan
Copy link
Member Author

jkseppan commented Sep 8, 2014

The Travis tests are passing, so that didn't uncover any more surprises.

But now I realize this could probably go on the 1.4.x branch. Should I rebase the pull request on top of that?

@tacaswell tacaswell changed the title Reduce the use of XObjects in pdf backend Reduce the use of XObjects in pdf backend [backport to 1.4.x] Sep 8, 2014
@tacaswell
Copy link
Member

Don't worry about it, I think it is easier to cherry-pick it to maintenance branch.

@jkseppan
Copy link
Member Author

I looked at the code again, and it seems my earlier worry about draw_path_collection being called with one-path collections was mistaken. In the example that spurred this pull request (#3345) the method is called just once, with a collection of 5000 paths. Turning each path into a PDF XObject makes no sense in this case, because there is no reuse of each path and the definition and single use of an XObject are more expensive than just inlining the path definition. This is not an indication of a bug in the calling code, but it would make sense to investigate whether the svg and ps backends are doing something similar that could be optimized.

With @mdboom's fix for the alternative code path, I think this could be merged.

The cost calculations are very rough, back-of-the-envelope.
In the example from matplotlib#3345 I get significant file size reductions:

	2.3M	before.svg
	1.8M	after.svg
	916K	before.ps
	672K	after.ps
@jkseppan
Copy link
Member Author

Yes, there are similar effects in svg and ps, but not as dramatic. It seems that the relative cost of XObjects in pdf is higher (and it's exacerbated by the fact that compression works well on the page stream but not so well on the short XObject streams). I added similar changes to the svg and ps backends.

@mdboom
Copy link
Member

mdboom commented Sep 22, 2014

Nice! Ready to merge?

@jkseppan
Copy link
Member Author

I think it's ready to merge, but I don't know if anyone wants to do more review. The Travis build is passing, which of course doesn't say anything about the ps backend but I think the risk there is small, since the change is simply using another known-working code path in some cases.

@tacaswell tacaswell added this to the v1.4.x milestone Oct 17, 2014
@tacaswell
Copy link
Member

gah! this fell through the cracks and did not make it into 1.4.1 and is too big to tack onto the rc 😞 .

@jkseppan
Copy link
Member Author

Oh; I've been busy with other things in the mean time. I agree this is too big to add in a release candidate, so how about we merge this into master and backport to 1.4.x after 1.4.1 is out, so it can get included in 1.4.2?

@tacaswell tacaswell modified the milestones: v1.4.2, v1.4.x Oct 17, 2014
@tacaswell
Copy link
Member

I just created a 1.4.2 milestone for exactly this reason.

I am going to hold of merging it as I plan to tag 1.4.1 tomorrow and it is harder to forget to merge that to forget to backport.

tacaswell added a commit that referenced this pull request Oct 19, 2014
ENH : Reduce the use of XObjects in pdf backend
@tacaswell tacaswell merged commit c600130 into matplotlib:master Oct 19, 2014
tacaswell added a commit that referenced this pull request Oct 19, 2014
ENH : Reduce the use of XObjects in pdf backend
@tacaswell
Copy link
Member

cherry-picked as efa3434

@jkseppan jkseppan deleted the reduce-xobjects branch October 19, 2014 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants