### Does OpenSCAD fold constants at the syntax parsing stage?

NH
Mon, Feb 5, 2024 11:01 PM

E.g. in a function like this:
yard(x) = x * 25.4 * 12 * 3;

Does each call do one multiplication or three?

E.g. in a function like this: yard(x) = x * 25.4 * 12 * 3; Does each call do one multiplication or three?
BC
Bob Carlson
Tue, Feb 6, 2024 4:42 AM

When I took my compiler writing course in 1968, this was considered a simple and easy optimization. Since OpenSCAD is built on long existing tools, I’d be very surprised if there are 3 multiplications.

-Bob
Tucson AZ

On Feb 5, 2024, at 16:01, nop head via Discuss discuss@lists.openscad.org wrote:

E.g. in a function like this:
yard(x) = x * 25.4 * 12 * 3;

Does each call do one multiplication or three?

To unsubscribe send an email to discuss-leave@lists.openscad.org

When I took my compiler writing course in 1968, this was considered a simple and easy optimization. Since OpenSCAD is built on long existing tools, I’d be very surprised if there are 3 multiplications. -Bob Tucson AZ > On Feb 5, 2024, at 16:01, nop head via Discuss <discuss@lists.openscad.org> wrote: > > E.g. in a function like this: > yard(x) = x * 25.4 * 12 * 3; > > Does each call do one multiplication or three? > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
CM
Curt McDowell
Tue, Feb 6, 2024 5:27 AM

There's no constant folding optimization. Every operator is evaluated
individually during execution, including within functions and loops.
There is no memoization of results (ala Haskell) -- even though there is
caching of computed graphical objects.

I wonder if it's actually worth further optimizing the OpenSCAD
functional language, when migration to Python OpenSCAD is a powerful
solution that's looking pretty promising.

Regards,
Curt

On 2/5/24 15:01, nop head via Discuss wrote:

E.g. in a function like this:
yard(x) = x * 25.4 * 12 * 3;

Does each call do one multiplication or three?

To unsubscribe send an email to discuss-leave@lists.openscad.org

There's no constant folding optimization. Every operator is evaluated individually during execution, including within functions and loops. There is no memoization of results (ala Haskell) -- even though there is caching of computed graphical objects. I wonder if it's actually worth further optimizing the OpenSCAD functional language, when migration to Python OpenSCAD is a powerful solution that's looking pretty promising. Regards, Curt On 2/5/24 15:01, nop head via Discuss wrote: > E.g. in a function like this: >  yard(x) = x * 25.4 * 12 * 3; > > Does each call do one multiplication or three? > > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
CK
Chun Kit LAM
Tue, Feb 6, 2024 1:05 PM

Do note that the slowness in the evaluator is not only caused by the
lack of constant folding, but due to AST walking + passing parameters in
a hash table. I have sscad that tries to rewrite the entire thing from
parser to evaluation, will get back to it when I have more time.

On 6/2/2024 13:27, Curt McDowell via Discuss wrote:

There's no constant folding optimization. Every operator is evaluated
individually during execution, including within functions and loops.
There is no memoization of results (ala Haskell) -- even though there is
caching of computed graphical objects.

I wonder if it's actually worth further optimizing the OpenSCAD
functional language, when migration to Python OpenSCAD is a powerful
solution that's looking pretty promising.

Regards,
Curt

On 2/5/24 15:01, nop head via Discuss wrote:

E.g. in a function like this:
yard(x) = x * 25.4 * 12 * 3;

Does each call do one multiplication or three?

To unsubscribe send an email to discuss-leave@lists.openscad.org

To unsubscribe send an email to discuss-leave@lists.openscad.org

Do note that the slowness in the evaluator is not only caused by the lack of constant folding, but due to AST walking + passing parameters in a hash table. I have sscad that tries to rewrite the entire thing from parser to evaluation, will get back to it when I have more time. On 6/2/2024 13:27, Curt McDowell via Discuss wrote: > There's no constant folding optimization. Every operator is evaluated > individually during execution, including within functions and loops. > There is no memoization of results (ala Haskell) -- even though there is > caching of computed graphical objects. > > I wonder if it's actually worth further optimizing the OpenSCAD > functional language, when migration to Python OpenSCAD is a powerful > solution that's looking pretty promising. > > Regards, > Curt > > On 2/5/24 15:01, nop head via Discuss wrote: >> E.g. in a function like this: >>  yard(x) = x * 25.4 * 12 * 3; >> >> Does each call do one multiplication or three? >> >> >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
TP
Torsten Paul
Tue, Feb 6, 2024 1:36 PM

On 06.02.24 06:27, Curt McDowell via Discuss wrote:

I wonder if it's actually worth further optimizing the OpenSCAD
functional language, when migration to Python OpenSCAD is a powerful
solution that's looking pretty promising.

In my view Python can be a very useful addition, but it's far from
being a full replacement for all use cases.

ciao,
Torsten.

On 06.02.24 06:27, Curt McDowell via Discuss wrote: > I wonder if it's actually worth further optimizing the OpenSCAD > functional language, when migration to Python OpenSCAD is a powerful > solution that's looking pretty promising. In my view Python can be a very useful addition, but it's far from being a full replacement for all use cases. ciao, Torsten.