Haskell-Things / ImplicitCAD

A math-inspired CAD program in haskell. CSG, bevels, and shells; 2D & 3D geometry; 2D gcode generation...
https://implicitcad.org/
GNU Affero General Public License v3.0
1.33k stars 141 forks source link

Performance of `cube` depends heavily on dimensions #417

Closed mokshasoft closed 2 years ago

mokshasoft commented 2 years ago

The following two calls take very different times to finish:

cube False (V3 200 95 45)
cube False (V3 4000 95 45) -- this is 32x slower

Code that triggers:

someFunc :: IO ()
someFunc = do
  putStrLn "Generating model1.stl fast"
  timeIt $ writeSTL 1 "model1.stl" $ cube False (V3 200 95 45)
  putStrLn "Generating model2.stl slow"
  timeIt $ writeSTL 1 "model2.stl" $ cube False (V3 4000 95 45)
  putStrLn "Done"

Trigger using https://github.com/mokshasoft/hus/tree/perf-error-1/Elements

$ stack build
...
$ stack exec Elements
Generating model1.stl fast
CPU time:  15.98s
Generating model2.stl slow
CPU time: 524.81s
Done
sorki commented 2 years ago

The larger cube increases the volume to be evaluated significantly so this is currently expected. The cube results in implicit function which is evaluated along full range of the objects bounds.

mokshasoft commented 2 years ago

Thanks! When I rearranged the code a bit I understand that it is the rendering that takes time (because of the rendering resolution) not the cube call. Changing the resolution to 20 makes the rendering almost instant.

c2 <- timeIt $ pure $ cube False (V3 4000 95 45)
timeIt $ writeSTL 20 "model2.stl" c2