npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jscad/vtree

v2.0.30

Published

Experimental Object Caching for JSCAD

Downloads

60

Readme

jscad-tree-experiments

Tree (& caching & more ) experiments to speed up significantly design computation time for JSCAD

NPM version NPM downloads Build Status Stability License

User Group Lerna JavaScript Style Guide

Backers Sponsors

Foreword

The problem

Currently in JSCAD designs, everytime you change a variable's value, an objects position, it recomputes everything !! but why recompute (in a very costly manner) ALL the geometry of a design everytime a small portion of the code changes ?

While I love the simplicity of the current design ie ** code => design **, it does not scale well because of the limiations of our 'geometric core engine' (csg.js), javascript itself, and the complexity of the models.

Possible solutions

My initial idea & exploration of possible optimisations to JSCAD/openjscad to allow for partial re-evaluation was using AST (abstract syntax tree) and the Esprima & co modules ecosystem: due to lack of time, complexity etc things were not really progressing....until !

Eureka ??

I recently had some very interesting discussions after a talk I did about JSCAD at Hannover.js Some people (need to find their names again, sorry folks !) mentioned AST based analysis and partial-re-evaluation not being a good solution, and possible ways of speeding things up by using:

  • an approach similar to virtual-doms :
    • generate a tree structure, and not directly the geometry
    • do diffs of the previous tree structure to determine the changed parts
  • make use of caching when possible:
    • caching of all possible variants of objects is not really feasable (there are infinite possible sizes of even a single cube() for example)
    • but it should be possible to at least cache some 'previous versions' of a design before things change again (since not everything changes at once)
    • perhaps cache each geometry in the csg tree

This repository will contain code that tries to explore these possibilities.

It is also important to mention that some experiments here are also inspired by Maker.js by Dan Marshall, particularly the use of 'pojo' (plain old javascript objects) as a way to define shapes

running the benchmarks

just type

npm run output

The 'user' folder contains all benchmarking code and jscad scripts (in user/examples)

Note: currently what benchmarks are run, and how is changing a lot

1 - first attempt :

  • create a replica of the JSCAD functional API that does NOT return geometry
  • but simple object nodes containing 'metadata' (ie , the dimensions etc of a cube, the list of operands for a boolean operation etc)
  • in order to generate a tree (at this stage, kinda, sorta like a csg tree, but not quite).
  • the tree then gets 'evaluated'/ visited and we use the current JSCAD code and api to create actual geometric data from the nodes

Interestingly this also allows for a very nice decoupling between API and implementation ! So it might be wortwhile regardless of the results (initial benchmarking results put this implimentation a hair width ahead of the SAME designs run in the current JSCAD)

RESULTS

This actually turned out to work really well and in a simple manner !
The 'fake' api can be found at core/index.js

2 - second attempt :

  • building uppon the first attempt, the aim was to make things FASTER by using caching
  • the current implementation works like this:
    • everytime a node gets evaluated into geometry we create a hash of the node (this is simple and fast since nodes are simple json)
    • we check our lookup/cache to see if we already have a geometry for that hash
    • if we do not we generate geometry and add it to the cache
    • if we do , we simply return the previously cached geometry

RESULTS

This worked amazingly well !
The first time a jscad script is evaluated, it has around the same speed as previously
but everytime you re-evaluate it the speedup , even for simpler scripts is in the 3X to 10X range!
and if you have multiple identical shapes (their complexity does not matter) the speedup is even more significant !

no more 'recompute the whole thing'!

This means huge benefits for literally any design, and it makes more complex designs a lot easier to work with !
As code-based designs tend to be created in a very iterative manner, this is a very nice gain!

A few additional notes:
* memory consumption is nearly identical with the 'vanilla' variant (current, non vtree JSCAD)
* cpu consumption as well, as visible in the benchmarks, though in that case I suspect that there might
be an issue with my measurements, as the 'vanilla' versions, made my computer fans go crazy, unlike the cache version
* for now the cache is in memory so there is no gain for cli, but it would be trivial to de/Serialize the cache to also have benefits there

3 - third attempt :

This is nearly identical to the second attempt, with one key difference : cache invalidation
It was implemented in the following manner:
    * increment a counter for a hash if there is a hit
    * keep track of all unused geometry/hash in a given pass
    * AFTER EACH PASS (re-evaluation of a jscad script) decrement the counter for unused hashes
        * if the counter drops to zero, remove the hash/geometry from the cache

RESULTS

Everything worked as expected : testing with a sequence of files with shared primitives , some of which
where removed, and correctly decached