discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

caching code ?

CC
Chris Camacho
Sat, Feb 10, 2018 10:57 AM

Just thinking out loud here I have no idea if it would help or its even
practical....

but suppose I have a small frequently used module like this

module drain() {

    difference() {
        translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32);
        rotate_extrude(convexity = 10,$fn=32)
            translate([4, 0, 0])
                circle(r = 2,$fn=32);
    }
    translate([0,0,-7]) cylinder(h=8,r=2,$fn=32);
}

then suppose I could modify it like this

module drain() {
cache() {
        difference() {
            translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32);
                rotate_extrude(convexity = 10,$fn=32)
                     translate([4, 0, 0])
                          circle(r = 2,$fn=32);
         }
          translate([0,0,-7]) cylinder(h=8,r=2,$fn=32);
}
}

the interpreter would hash just the code between the curly braces (such
the code could change location without disturbing the hash) the hash
would invalidate the cache if there were any changes in the code

The cache would produce ready a made mesh for faster use....

of course this doesn't help with chunks of code that are parameter
driven (except maybe simple translations) but allowing the user to cache
bits like this might make some difference....

Just thinking out loud here I have no idea if it would help or its even practical.... but suppose I have a small frequently used module like this module drain() {     difference() {         translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32);         rotate_extrude(convexity = 10,$fn=32)             translate([4, 0, 0])                 circle(r = 2,$fn=32);     }     translate([0,0,-7]) cylinder(h=8,r=2,$fn=32); } then suppose I could modify it like this module drain() { cache() {         difference() {             translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32);                 rotate_extrude(convexity = 10,$fn=32)                      translate([4, 0, 0])                           circle(r = 2,$fn=32);          }           translate([0,0,-7]) cylinder(h=8,r=2,$fn=32); } } the interpreter would hash just the code between the curly braces (such the code could change location without disturbing the hash) the hash would invalidate the cache if there were any changes in the code The cache would produce ready a made mesh for faster use.... of course this doesn't help with chunks of code that are parameter driven (except maybe simple translations) but allowing the user to cache bits like this might make some difference....
NH
nop head
Sat, Feb 10, 2018 11:30 AM

If you replace cache with render then the mesh will be cached and only
recalculated when parameters change.

On 10 February 2018 at 10:57, Chris Camacho chris@bedroomcoders.co.uk
wrote:

Just thinking out loud here I have no idea if it would help or its even
practical....

but suppose I have a small frequently used module like this

module drain() {

 difference() {
     translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32);
     rotate_extrude(convexity = 10,$fn=32)
         translate([4, 0, 0])
             circle(r = 2,$fn=32);
 }
 translate([0,0,-7]) cylinder(h=8,r=2,$fn=32);

}

then suppose I could modify it like this

module drain() {
cache() {
difference() {
translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32);
rotate_extrude(convexity = 10,$fn=32)
translate([4, 0, 0])
circle(r = 2,$fn=32);
}
translate([0,0,-7]) cylinder(h=8,r=2,$fn=32);
}
}

the interpreter would hash just the code between the curly braces (such
the code could change location without disturbing the hash) the hash would
invalidate the cache if there were any changes in the code

The cache would produce ready a made mesh for faster use....

of course this doesn't help with chunks of code that are parameter driven
(except maybe simple translations) but allowing the user to cache bits like
this might make some difference....


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

If you replace cache with render then the mesh will be cached and only recalculated when parameters change. On 10 February 2018 at 10:57, Chris Camacho <chris@bedroomcoders.co.uk> wrote: > Just thinking out loud here I have no idea if it would help or its even > practical.... > > but suppose I have a small frequently used module like this > > module drain() { > > difference() { > translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32); > rotate_extrude(convexity = 10,$fn=32) > translate([4, 0, 0]) > circle(r = 2,$fn=32); > } > translate([0,0,-7]) cylinder(h=8,r=2,$fn=32); > } > > > then suppose I could modify it like this > > module drain() { > cache() { > difference() { > translate([0,0,1]) cylinder(r=4,h=2,center=true,$fn=32); > rotate_extrude(convexity = 10,$fn=32) > translate([4, 0, 0]) > circle(r = 2,$fn=32); > } > translate([0,0,-7]) cylinder(h=8,r=2,$fn=32); > } > } > > the interpreter would hash just the code between the curly braces (such > the code could change location without disturbing the hash) the hash would > invalidate the cache if there were any changes in the code > > The cache would produce ready a made mesh for faster use.... > > of course this doesn't help with chunks of code that are parameter driven > (except maybe simple translations) but allowing the user to cache bits like > this might make some difference.... > > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
JB
Jordan Brown
Sat, Feb 10, 2018 3:27 PM

On 2/10/2018 3:30 AM, nop head wrote:

If you replace cache with render then the mesh will be cached and only
recalculated when parameters change.

Doesn't it do that even if you don't call render()?  I don't use
render() much, yet it still tells me how much memory is being used by
the cache.

On 2/10/2018 3:30 AM, nop head wrote: > If you replace cache with render then the mesh will be cached and only > recalculated when parameters change. Doesn't it do that even if you don't call render()?  I don't use render() much, yet it still tells me how much memory is being used by the cache.
CC
Chris Camacho
Sat, Feb 10, 2018 4:09 PM

whatever its actually doing it defiantly saves significant time even on
half a dozen relatively simple shapes, so from now on I'll definitely be
keeping in mind where I might be able to use it...

On 10/02/18 15:27, Jordan Brown wrote:

On 2/10/2018 3:30 AM, nop head wrote:

If you replace cache with render then the mesh will be cached and only
recalculated when parameters change.

Doesn't it do that even if you don't call render()?  I don't use
render() much, yet it still tells me how much memory is being used by
the cache.


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

whatever its actually doing it defiantly saves significant time even on half a dozen relatively simple shapes, so from now on I'll definitely be keeping in mind where I might be able to use it... On 10/02/18 15:27, Jordan Brown wrote: > On 2/10/2018 3:30 AM, nop head wrote: >> If you replace cache with render then the mesh will be cached and only >> recalculated when parameters change. > Doesn't it do that even if you don't call render()?  I don't use > render() much, yet it still tells me how much memory is being used by > the cache. > > > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org