"use" results in 3x more run time than "include"

classic Classic list List threaded Threaded
33 messages Options
12
Reply | Threaded
Open this post in threaded view
|

"use" results in 3x more run time than "include"

adrianv
I was trying to optimize some code and the things that were helping weren't
making a lot of sense.  I finally realized that if I used "include" to use
my library instead of "use" my code ran about 3 times faster.  (I suspect
that "optimization" was achieved by anything that decreased the number of
function calls to the library.)  

Now one reason this might happen, as recently discussed, is top level
variables in my library file.   The included file contains only function
definitions, no variables and no modules.   So what is going on here?  




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

nophead
I suppose local functions are used in preference the used ones, so perhaps it looks up names in the local scope and then each of the used files in turn. Do you have more than one used file, if so does the order make a difference?

I remember coding in interpreted Basic and it ran faster if you put frequently call subroutines at the beginning simply because it did a linear search from the start.

On Sat, 13 Apr 2019 at 12:41, adrianv <[hidden email]> wrote:
I was trying to optimize some code and the things that were helping weren't
making a lot of sense.  I finally realized that if I used "include" to use
my library instead of "use" my code ran about 3 times faster.  (I suspect
that "optimization" was achieved by anything that decreased the number of
function calls to the library.) 

Now one reason this might happen, as recently discussed, is top level
variables in my library file.   The included file contains only function
definitions, no variables and no modules.   So what is going on here?   




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

adrianv
There is a difference depending on the order of the "use" statements.  If I
put the library in question first run and use "use" instead of "include" run
time rises by 100% in one of my tests.  If I put the "use" statement last
run time rises by 125%.  





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

nophead
I think you are treading new ground because most OpenSCAD code spends all its time creating geometry. The time to run the script to is usually negligible. I typically use about 70 files and it starts CSG product generation within a second or two. I do have one project that takes about 10 seconds, so I will look at the order I use things to see if I can reduce that.

On Sat, 13 Apr 2019 at 15:47, adrianv <[hidden email]> wrote:
There is a difference depending on the order of the "use" statements.  If I
put the library in question first run and use "use" instead of "include" run
time rises by 100% in one of my tests.  If I put the "use" statement last
run time rises by 125%. 





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

adrianv
At least at the moment I'm not talking about huge amounts of time, tens of
seconds.  But the time to preview the objects is clearly dominated by the
computations, not by the graphics.  



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

nophead
What sort of computations are you doing? The most intensive thing I do is sweeps along Bezier paths but it only take a few seconds to do several of them.

On Sat, 13 Apr 2019 at 16:57, adrianv <[hidden email]> wrote:
At least at the moment I'm not talking about huge amounts of time, tens of
seconds.  But the time to preview the objects is clearly dominated by the
computations, not by the graphics. 



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

adrianv
I wrote a library for producing semi-regular polyhedra.  The computations are
first using Linde's hull() function to find the convex hull faces of the
polyhedron's vertices---but it produces a list of triangles.  I need to have
the actual faces, which can be octagons or decagons, so I assemble the
triangles into the full faces of the polyhedron.  This last step was taking
3 times as long as doing the convex hull, which seemed really strange, which
was why I started trying to optimize.  

I have a test case where I display ~100 polyhedra which is what I've been
using to assess run time.  




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

RevarBat
In reply to this post by adrianv
I suspect part of the delay isn't just search order, but that it's actually re-defining/recompiling the functions on each use.  The fact that echo()s at top-level are executed any time a function in a used library is called, makes me suspect the function declarations are also being re-executed.

This could be verified by having a used file, with the function you want to call at the beginning, named something like `aaaa()`.  Fill the file with a lot of other function declarations after it.  If the number of function declarations affects runtime, it's probably spending time re-declaring functions.

At this point, it looks like the use of `use` is severely dis-indicated.

- Revar


> On Apr 13, 2019, at 7:46 AM, adrianv <[hidden email]> wrote:
>
> There is a difference depending on the order of the "use" statements.  If I
> put the library in question first run and use "use" instead of "include" run
> time rises by 100% in one of my tests.  If I put the "use" statement last
> run time rises by 125%.  
>
>
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

JordanBrown
On 4/13/2019 5:37 PM, Revar Desmera wrote:
I suspect part of the delay isn't just search order, but that it's actually re-defining/recompiling the functions on each use.  The fact that echo()s at top-level are executed any time a function in a used library is called, makes me suspect the function declarations are also being re-executed.

With 2019.01-RC2 I don't see the behavior that you describe.

I have a main file
use <use.scad>

foo();
translate([10,10]) foo();
echo(f(0));
and use.scad is:
echo("top");

module foo() {
    cube();
}

function f(n) = n+1;

echo("bottom");

and the only echo output is the expected "ECHO: 1".

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

RevarBat
Well THIS is interesting.  I know I reproduced nop head's results with the version of OpenSCAD I'm running (2019-03-03) just a week or so ago.
But this code below does NOT reproduce it.

- Revar


On Apr 13, 2019, at 5:59 PM, Jordan Brown <[hidden email]> wrote:

On 4/13/2019 5:37 PM, Revar Desmera wrote:
I suspect part of the delay isn't just search order, but that it's actually re-defining/recompiling the functions on each use.  The fact that echo()s at top-level are executed any time a function in a used library is called, makes me suspect the function declarations are also being re-executed.

With 2019.01-RC2 I don't see the behavior that you describe.

I have a main file
use <use.scad>

foo();
translate([10,10]) foo();
echo(f(0));
and use.scad is:
echo("top");

module foo() {
    cube();
}

function f(n) = n+1;

echo("bottom");

and the only echo output is the expected "ECHO: 1".


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

RevarBat
Aha!  Minimal code to reproduce:
use.scad:
```
function foo(x) = x;
x=echo("DANGER DANGER! DANGER, Will Robinson!");
```

main.scad:
```
use <use.scad>
y = foo(2);
```

Output:
```
Used file cache size: 2 files
Compiling design (CSG Tree generation)...
ECHO: "DANGER DANGER! DANGER, Will Robinson!"
Compiling design (CSG Products generation)...
Geometries in cache: 1
Geometry cache size in bytes: 728
CGAL Polyhedrons in cache: 0
CGAL cache size in bytes: 0
Compiling design (CSG Products normalization)...
Normalized CSG tree has 0 elements
Compile and preview finished.
Total rendering time: 0 hours, 0 minutes, 0 seconds
```

- Revar



On Apr 13, 2019, at 7:03 PM, Revar Desmera <[hidden email]> wrote:

Well THIS is interesting.  I know I reproduced nop head's results with the version of OpenSCAD I'm running (2019-03-03) just a week or so ago.
But this code below does NOT reproduce it.

- Revar


On Apr 13, 2019, at 5:59 PM, Jordan Brown <[hidden email]> wrote:

On 4/13/2019 5:37 PM, Revar Desmera wrote:
I suspect part of the delay isn't just search order, but that it's actually re-defining/recompiling the functions on each use.  The fact that echo()s at top-level are executed any time a function in a used library is called, makes me suspect the function declarations are also being re-executed.

With 2019.01-RC2 I don't see the behavior that you describe.

I have a main file
use <use.scad>

foo();
translate([10,10]) foo();
echo(f(0));
and use.scad is:
echo("top");

module foo() {
    cube();
}

function f(n) = n+1;

echo("bottom");

and the only echo output is the expected "ECHO: 1".



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

RevarBat
Call foo() a few more times in main.scad, and you'll see the text echoed for every call.

- Revar



On Apr 13, 2019, at 7:11 PM, Revar Desmera <[hidden email]> wrote:

Aha!  Minimal code to reproduce:
use.scad:
```
function foo(x) = x;
x=echo("DANGER DANGER! DANGER, Will Robinson!");
```

main.scad:
```
use <use.scad>
y = foo(2);
```

Output:
```
Used file cache size: 2 files
Compiling design (CSG Tree generation)...
ECHO: "DANGER DANGER! DANGER, Will Robinson!"
Compiling design (CSG Products generation)...
Geometries in cache: 1
Geometry cache size in bytes: 728
CGAL Polyhedrons in cache: 0
CGAL cache size in bytes: 0
Compiling design (CSG Products normalization)...
Normalized CSG tree has 0 elements
Compile and preview finished.
Total rendering time: 0 hours, 0 minutes, 0 seconds
```

- Revar



On Apr 13, 2019, at 7:03 PM, Revar Desmera <[hidden email]> wrote:

Well THIS is interesting.  I know I reproduced nop head's results with the version of OpenSCAD I'm running (2019-03-03) just a week or so ago.
But this code below does NOT reproduce it.

- Revar


On Apr 13, 2019, at 5:59 PM, Jordan Brown <[hidden email]> wrote:

On 4/13/2019 5:37 PM, Revar Desmera wrote:
I suspect part of the delay isn't just search order, but that it's actually re-defining/recompiling the functions on each use.  The fact that echo()s at top-level are executed any time a function in a used library is called, makes me suspect the function declarations are also being re-executed.

With 2019.01-RC2 I don't see the behavior that you describe.

I have a main file
use <use.scad>

foo();
translate([10,10]) foo();
echo(f(0));
and use.scad is:
echo("top");

module foo() {
    cube();
}

function f(n) = n+1;

echo("bottom");

and the only echo output is the expected "ECHO: 1".




_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

nophead
I tried including my math functions into my sweep code instead of using them. It made some of my tests very slightly faster and some slightly slower. The downside of include is it has to re-parse the code each time instead of using the module cache.

I don't really have any high frequency calls across file boundaries, which is why I haven't noticed this effect. I haven't got anything approaching the complexity of calculating a hull in user space. This got me thinking that perhaps OpenSCAD should provide all the CSG ops as functions that take parameters like polyhedron and return the same. That would allow geometry to be computed without breaking anything. Of course if you called a union or minkowski function it would make the compilation crazily slow.

Yes variables seem to re evaluated when you call a function in a used module but I am sure functions and modules are not recompiled because that is done at parse time and the AST is cached in the module cache. So there isn't anything to do for a function unless it is called by a variable initialisation.

On Sun, 14 Apr 2019 at 03:15, Revar Desmera <[hidden email]> wrote:
Call foo() a few more times in main.scad, and you'll see the text echoed for every call.

- Revar



On Apr 13, 2019, at 7:11 PM, Revar Desmera <[hidden email]> wrote:

Aha!  Minimal code to reproduce:
use.scad:
```
function foo(x) = x;
x=echo("DANGER DANGER! DANGER, Will Robinson!");
```

main.scad:
```
use <use.scad>
y = foo(2);
```

Output:
```
Used file cache size: 2 files
Compiling design (CSG Tree generation)...
ECHO: "DANGER DANGER! DANGER, Will Robinson!"
Compiling design (CSG Products generation)...
Geometries in cache: 1
Geometry cache size in bytes: 728
CGAL Polyhedrons in cache: 0
CGAL cache size in bytes: 0
Compiling design (CSG Products normalization)...
Normalized CSG tree has 0 elements
Compile and preview finished.
Total rendering time: 0 hours, 0 minutes, 0 seconds
```

- Revar



On Apr 13, 2019, at 7:03 PM, Revar Desmera <[hidden email]> wrote:

Well THIS is interesting.  I know I reproduced nop head's results with the version of OpenSCAD I'm running (2019-03-03) just a week or so ago.
But this code below does NOT reproduce it.

- Revar


On Apr 13, 2019, at 5:59 PM, Jordan Brown <[hidden email]> wrote:

On 4/13/2019 5:37 PM, Revar Desmera wrote:
I suspect part of the delay isn't just search order, but that it's actually re-defining/recompiling the functions on each use.  The fact that echo()s at top-level are executed any time a function in a used library is called, makes me suspect the function declarations are also being re-executed.

With 2019.01-RC2 I don't see the behavior that you describe.

I have a main file
use <use.scad>

foo();
translate([10,10]) foo();
echo(f(0));
and use.scad is:
echo("top");

module foo() {
    cube();
}

function f(n) = n+1;

echo("bottom");

and the only echo output is the expected "ECHO: 1".



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

kintel
Administrator
> On Apr 14, 2019, at 03:44, nop head <[hidden email]> wrote:
>
> Yes variables seem to re evaluated when you call a function in a used module but I am sure functions and modules are not recompiled because that is done at parse time and the AST is cached in the module cache. So there isn't anything to do for a function unless it is called by a variable initialisation.
>
I  don't think that's necessary, and even if we continue doing it, it should be possible to perform some sort of Common Expression Elimination.
This probably went largely unnoticed since until the recently introduced echo() function, we didn't really have functions with side effects.

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
tp3
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

tp3
Also there's nothing to protect against performance regressions
at this point. While there's >1200 tests running on every code
change, that is only covering functional check.

So if we could collect a couple of not too big code examples
that could be used for performance testing the compilation /
evaluation steps, we could try to get that integrated into
the test suite and see if we can schedule that to run once
a day (or a week).

ciao,
   Torsten.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

nophead
To test my own library I time each test and store the results in a JSON file. At the end of the test I show which got faster or slower by significant amounts by showing the delta and colouring red and green and I rank them by time. Here is the last run. As soon as I added this it prompted me to start investigating how to make it go faster. It started at 972 seconds and now 172.3. It does vary a bit randomly as my PC is doing other things. Perhaps the OpenSCAD test suite should do the same to highlight how performance is changing.

image.png

On Sun, 14 Apr 2019 at 17:40, Torsten Paul <[hidden email]> wrote:
Also there's nothing to protect against performance regressions
at this point. While there's >1200 tests running on every code
change, that is only covering functional check.

So if we could collect a couple of not too big code examples
that could be used for performance testing the compilation /
evaluation steps, we could try to get that integrated into
the test suite and see if we can schedule that to run once
a day (or a week).

ciao,
   Torsten.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
tp3
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

tp3
On 14.04.19 19:29, nop head wrote:
> Perhaps the OpenSCAD test suite should do the same to highlight
> how performance is changing.


Yes, that's pretty much what I'm thinking too. But as we run tests
on various machines it's not that simple as we don't want to have
lots of random errors flagged just because we got a slower worker
VM assigned for a build.

That's why I hope we might be able handle that using a couple of
specific test cases which have a reasonable long run time and
compare those against some base line tests. That way it would
only trigger notifications in case the relative time is changing
a lot and not absolute runtime.

ciao,
   Torsten.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

MichaelAtOz
Administrator
In reply to this post by nophead
nophead, what is that specific measure?
Elapsed run time for Render via cmd-line?



-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

nophead
Yes I measure the time to preview a large PNG using the command line. It is about 1.5 seconds slower than F5 preview but I am exporting a 4096 x 4096 image. I do that because viewall is quite conservative and leaves a large border. I then use image magick to trim the border and downsize it to 1280 width to give some anti-aliasing. The results look like this.

d_connectors.png

On Sun, 14 Apr 2019 at 22:40, MichaelAtOz <[hidden email]> wrote:
nophead, what is that specific measure?
Elapsed run time for Render via cmd-line?



-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "use" results in 3x more run time than "include"

MichaelAtOz
Administrator
Noting, for others, that nophead uses render(), so cmd-line preview includes
the CGAL processing.



-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
12