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

Performance problems when overriding linkCanvasObject #93

Open
jeffmcwhirter opened this issue Jan 17, 2020 · 8 comments
Open

Performance problems when overriding linkCanvasObject #93

jeffmcwhirter opened this issue Jan 17, 2020 · 8 comments

Comments

@jeffmcwhirter
Copy link

Hi,
I am having some really bad performance problems when trying to override the linkCanvasObject method similar to what you have in this example -https://github.com/vasturiano/force-graph/blob/master/example/dash-odd-links/index.html
basically:
ctx.moveTo(link.source.x, link.source.y);
ctx.lineTo(link.target.x, link.target.y);
ctx.stroke();

I am using the Les Miserables data (~250 links) and it drags my browser down to its knees. Without overriding this method the graph renders just fine

@vasturiano
Copy link
Owner

Hi Jeff,

That is most likely because of the canvas stroke instruction that runs at at every link. One thing that you can do is run that instruction only once per frame, on the last link. This will mean all the links will be painted together instead of individually, and should improve the performance dramatically.

Something like:

(link === gData.links[gData.links.length - 1]) && ctx.stroke();

I've applied that fix to the example you mentioned and there is a perceptible performance improvement.
https://github.com/vasturiano/force-graph/blob/master/example/dash-odd-links/index.html#L40

@jeffmcwhirter
Copy link
Author

jeffmcwhirter commented Jan 20, 2020 via email

@happybeing
Copy link

@vasturiano hope it's ok for me to jump in here. I've been learning to do some basic graph visualisation and began using d3js and trying different implementations (SVG, canvas) to compare performance because I hope to handle relatively large networks at some point.

You can see my first test app here which lets you choose which implementation, and also try out different test data, the default data being Les Miserables.

I stumbled on your work and have been impressed by it, but not tried it out yet. Eventually I want to provide many different ways to view a given dataset through a UI based on Svelte components, so I was planning to come back and look at your 3d and globe.gl visualisations.

Anyway, I just tried the link posted above by @jeffmcwhirter and I'm seeing quite slow redraw compared to all my implementations with this same small dataset. Jeff's example is smoother in Chrome than Firefox (Chrome is much faster than Firefox on mobile and desktop), but my Les Mis graph arranges and drags quite smoothly even on my tablet in Firefox, whereas Jeff's is quite jerky (text or simple nodes).

I'm curious about the difference here - is this just due to customisations made by Jeff? I tried your 1d, 2d, 3d example and it seems fine on my desktop, so I'm wondering what is making Jeff's implementation slower?

I'm designing custom styling for the next iteration right now so interested to learn what is the issue here.

@vasturiano
Copy link
Owner

@theWebalyst thanks for reaching out.

Are you observing the same slow down in these built-in examples:
https://vasturiano.github.io/force-graph/example/dash-odd-links/
https://vasturiano.github.io/force-graph/example/load-json/

@happybeing
Copy link

@vasturiano they and the others of yours I've tried have all been fine. It's only Jeff's example that's jittery.

@jeffmcwhirter
Copy link
Author

jeffmcwhirter commented Jan 21, 2020 via email

@happybeing
Copy link

happybeing commented Jan 21, 2020

@jeffmcwhirter The difference is minor on such a small graph but noticable on lower powered devices (mobile/tablet).

I think to see the difference in desktop you would need a larger graph in a single view. See my example for a way to generate these. Source here: https://github.com/theWebalyst/d3-fdg-svelte-perf

If you play with different graph sizes the live example you'll see how much larger graphs can be rendered before jitter becomes noticable than in your implementation.

@FatmaAkram
Copy link

Hi Jeff,

That is most likely because of the canvas stroke instruction that runs at at every link. One thing that you can do is run that instruction only once per frame, on the last link. This will mean all the links will be painted together instead of individually, and should improve the performance dramatically.

Something like:

(link === gData.links[gData.links.length - 1]) && ctx.stroke();

I've applied that fix to the example you mentioned and there is a perceptible performance improvement. https://github.com/vasturiano/force-graph/blob/master/example/dash-odd-links/index.html#L40

I've tried this one, but it only draws the last link. Any thoughts?

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

No branches or pull requests

4 participants