discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

What is Cached .

T
TLC123
Fri, Mar 24, 2017 2:04 PM

Are function calls and expressions cached like geometry?  I am interested in
possibilities to exploit caching for some speed up. So my question is to
what extent is what Cached at different levels.

In my case I call the same function with the same parameters from different
parts of a recursion.

--
View this message in context: http://forum.openscad.org/What-is-Cached-tp21015.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Are function calls and expressions cached like geometry? I am interested in possibilities to exploit caching for some speed up. So my question is to what extent is what Cached at different levels. In my case I call the same function with the same parameters from different parts of a recursion. -- View this message in context: http://forum.openscad.org/What-is-Cached-tp21015.html Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Fri, Mar 24, 2017 2:23 PM

I am pretty sure functions aren't cached at the moment as they run quite
fast compared to the geometry in a normal OpenSCAD script.

On 24 March 2017 at 14:04, TLC123 torleif.ceder@gmail.com wrote:

Are function calls and expressions cached like geometry?  I am interested
in
possibilities to exploit caching for some speed up. So my question is to
what extent is what Cached at different levels.

In my case I call the same function with the same parameters from different
parts of a recursion.

--
View this message in context: http://forum.openscad.org/
What-is-Cached-tp21015.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

I am pretty sure functions aren't cached at the moment as they run quite fast compared to the geometry in a normal OpenSCAD script. On 24 March 2017 at 14:04, TLC123 <torleif.ceder@gmail.com> wrote: > Are function calls and expressions cached like geometry? I am interested > in > possibilities to exploit caching for some speed up. So my question is to > what extent is what Cached at different levels. > > In my case I call the same function with the same parameters from different > parts of a recursion. > > > > -- > View this message in context: http://forum.openscad.org/ > What-is-Cached-tp21015.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
MK
Marius Kintel
Sat, Mar 25, 2017 1:22 PM

On Mar 24, 2017, at 10:04, TLC123 torleif.ceder@gmail.com wrote:

Are function calls and expressions cached like geometry?

At the moment, no, but OpenSCAD is designed to be able to exploit such referential transparency and hence cache expression results.

-Marius

> On Mar 24, 2017, at 10:04, TLC123 <torleif.ceder@gmail.com> wrote: > > Are function calls and expressions cached like geometry? At the moment, no, but OpenSCAD is designed to be able to exploit such referential transparency and hence cache expression results. -Marius
DM
doug moen
Sun, Mar 26, 2017 2:27 PM

A good way to cache expression results is to implement compiler
optimizations such as common subexpression elimination, and lifting of
invariant expressions. The net result is that during runtime, there are
fewer evaluations of an expression that returns the same result each time.
This would be my recommendation.

Another way to cache expression results might be, at runtime, to hash a
function name with its arguments and do a lookup in a hash table to see if
the result has already been computed. But, this doesn't decrease the number
of times we attempt to evaluate the expression, and it adds additional
runtime overhead to the evaluation of the expression. So it is less likely
to be a win.

Haskell and some other functional languages use lazy evaluation. An
expression is compiled into a thunk, which is passed around at run time.
The first time that the thunk is referenced in a value context, it is
executed, and then the thunk object is overwritten with the resulting
value. This approach also introduce a run-time overhead. It can speed up
some programs, if a lot of expensive thunks never get evaluated, but it can
slow down other programs.

OpenSCAD is not 100% referentially transparent. If a function F directly or
indirectly calls rands, then calls to F aren't referentially transparent,
and that inhibits the ability to do these optimizations. rands should be
deprecated and replaced by a referentially transparent random number
facility based on hashing.

On 25 March 2017 at 09:22, Marius Kintel marius@kintel.net wrote:

On Mar 24, 2017, at 10:04, TLC123 torleif.ceder@gmail.com wrote:

Are function calls and expressions cached like geometry?

At the moment, no, but OpenSCAD is designed to be able to exploit such
referential transparency and hence cache expression results.

-Marius


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

A good way to cache expression results is to implement compiler optimizations such as common subexpression elimination, and lifting of invariant expressions. The net result is that during runtime, there are fewer evaluations of an expression that returns the same result each time. This would be my recommendation. Another way to cache expression results might be, at runtime, to hash a function name with its arguments and do a lookup in a hash table to see if the result has already been computed. But, this doesn't decrease the number of times we attempt to evaluate the expression, and it adds additional runtime overhead to the evaluation of the expression. So it is less likely to be a win. Haskell and some other functional languages use lazy evaluation. An expression is compiled into a thunk, which is passed around at run time. The first time that the thunk is referenced in a value context, it is executed, and then the thunk object is overwritten with the resulting value. This approach also introduce a run-time overhead. It can speed up some programs, if a lot of expensive thunks never get evaluated, but it can slow down other programs. OpenSCAD is not 100% referentially transparent. If a function F directly or indirectly calls `rands`, then calls to F aren't referentially transparent, and that inhibits the ability to do these optimizations. `rands` should be deprecated and replaced by a referentially transparent random number facility based on hashing. On 25 March 2017 at 09:22, Marius Kintel <marius@kintel.net> wrote: > > On Mar 24, 2017, at 10:04, TLC123 <torleif.ceder@gmail.com> wrote: > > > > Are function calls and expressions cached like geometry? > > At the moment, no, but OpenSCAD is designed to be able to exploit such > referential transparency and hence cache expression results. > > -Marius > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > >