MakieOrg / Makie.jl

Interactive data visualizations and plotting in Julia
https://docs.makie.org/stable
MIT License
2.42k stars 313 forks source link

2D meshes in CairoMakie are slower than 3D meshes #4112

Open ffreyer opened 3 months ago

ffreyer commented 3 months ago

Running this example on master

using CairoMakie
using GeometryBasics

N = 100
M = 100
m2d = GeometryBasics.Mesh(
    rand(Point2f, N), 
    [GLTriangleFace(i, mod1(i+1, N), mod1(i+2, N)) for i in rand(1:N, M)]
)
m3d = GeometryBasics.Mesh(
    rand(Point3f, N), 
    [GLTriangleFace(i, mod1(i+1, N), mod1(i+2, N)) for i in rand(1:N, M)]
)

@time display(mesh(m2d))
@time display(mesh(m3d))

for some different inputs yields:

N vertices M faces 2D time 3D time
100 100 0.63s 0.16s
1000 100 0.59s 0.13s
100 1000 6.9s 0.9s
1000 10000 57s 8.7s

Running this using a Scene instead of Axis, i.e.

let
    scene = Scene()
    mesh!(scene, m2d)
    cam2d!(scene)
    center!(scene)
    display(scene)
end 

equalizes things a bit, but still leaves 2D at 3x the time of 3D.

asinghvi17 commented 3 months ago

I can replicate this pretty consistently on my machine (mac m1) as well...

asinghvi17 commented 3 months ago

Looking into the code with Cthulhu it looks like that method is just a massive mess of type instability. There are quite a few runtime dispatches but the inner loop (invoked at the last draw_mesh2d call) seems fine...

https://github.com/MakieOrg/Makie.jl/blob/c83dda467a2492036948a94879fa221b0462f678/CairoMakie/src/primitives.jl#L919-L934