feat(views): cache function compilation. Closes #3517.
Since supporting SpiderMonkey versions > 1.8.5 we compile design
doc functions of the form `function(args) { /* impl */ }` into a
form that is recognise by newer JS engines.
For reduce views, this means a transpilation happens on each
reduce call over the couchjs protocol, which is once for every
level in the b+tree plus one final rereduce across all shards.
down reduce view indexing/querying.
This patch adds caching to the compilation function. This is
implemented by way of producing a SHA-256 hash of all incoming
JS functions and caching them in a global object in the memory
of a `couchjs` process.
The cache is cleared when a `add_fun` message is received, which
happens before new map functions from a new ddoc are loaded into
`couchjs`. This ensures that only functions from a single view &
security context are ever loaded into the cache.
SHA-256 was chosen because it is producing collisions that are
also valid JS functions is unlikley.
This specific SHA-256 implementation was chosen because:
- it is favourably licensed (MIT)
- taken from the Deno (https://deno.land) project, (h/t Martin
Sonnenholzer for the tip)so we can be reasonably assured this
has been tested thoroughly.
7 files changed