janl commented on issue #3517:
URL: https://github.com/apache/couchdb/issues/3517#issuecomment-905412556


   I’ve attempted a quick demo that caches the transpilation inside the JS 
process, this would still do one transpile per couchjs process, but should 
still be a lot better.
   
   @konrad-ohms or @nickva would you be able to retry this with the original 
setup?
   
   ```
   diff --git a/share/server/60/rewrite_fun.js b/share/server/60/rewrite_fun.js
   index 1b27a9d14..9b85f953f 100644
   --- a/share/server/60/rewrite_fun.js
   +++ b/share/server/60/rewrite_fun.js
   @@ -15,30 +15,39 @@
    //
    //  
https://github.com/dmunch/couch-chakra/blob/master/js/normalizeFunction.js
    
   +
   +const cache = {}
    function rewriteFunInt(fun) {
   -    const ast = esprima.parse(fun);
   -    let idx = ast.body.length - 1;
   -    let decl = {};
   -
   -    // Search for the first FunctionDeclaration beginning from the end
   -    do {
   -        decl = ast.body[idx--];
   -    } while (idx >= 0 && decl.type !== "FunctionDeclaration");
   -    idx++;
   -
   -    // If we have a function declaration without an Id, wrap it
   -    // in an ExpressionStatement and change it into
   -    // a FuntionExpression
   -    if (decl.type == "FunctionDeclaration" && decl.id == null) {
   -        decl.type = "FunctionExpression";
   -        ast.body[idx] = {
   -            type: "ExpressionStatement",
   -            expression: decl
   -        };
   -    }
   +    const crc = crc32_str(fun)
   +    if (cache[crc]) {
   +        return cache[crc];
   +    } else {
   +        const ast = esprima.parse(fun);
   +        let idx = ast.body.length - 1;
   +        let decl = {};
   +
   +        // Search for the first FunctionDeclaration beginning from the end
   +        do {
   +            decl = ast.body[idx--];
   +        } while (idx >= 0 && decl.type !== "FunctionDeclaration");
   +        idx++;
   +
   +        // If we have a function declaration without an Id, wrap it
   +        // in an ExpressionStatement and change it into
   +        // a FuntionExpression
   +        if (decl.type == "FunctionDeclaration" && decl.id == null) {
   +            decl.type = "FunctionExpression";
   +            ast.body[idx] = {
   +                type: "ExpressionStatement",
   +                expression: decl
   +            };
   +        }
    
   -    // Generate source from the rewritten AST
   -    return escodegen.generate(ast);
   +        // Generate source from the rewritten AST
   +        const gen = escodegen.generate(ast);
   +        cache[crc] = gen;
   +        return gen;
   +    }
    }
    
    
   @@ -53,4 +62,51 @@ function rewriteFuns(funsJSON) {
            return rewriteFunInt(fun);
        });
        return JSON.stringify(results);
   -}
   \ No newline at end of file
   +}
   +
   +// nicked from https://github.com/SheetJS/js-crc32/blob/master/crc32.js
   +
   +function signed_crc_table() {
   +    var c = 0, table = new Array(256);
   +
   +    for(var n =0; n != 256; ++n){
   +            c = n;
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
   +            table[n] = c;
   +    }
   +
   +    return typeof Int32Array !== 'undefined' ? new Int32Array(table) : 
table;
   +}
   +
   +var T = signed_crc_table();
   +
   +function crc32_str(str, seed) {
   +    var C = seed ^ -1;
   +    for(var i = 0, L=str.length, c, d; i < L;) {
   +            c = str.charCodeAt(i++);
   +            if(c < 0x80) {
   +                    C = (C>>>8) ^ T[(C ^ c)&0xFF];
   +            } else if(c < 0x800) {
   +                    C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
   +                    C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
   +            } else if(c >= 0xD800 && c < 0xE000) {
   +                    c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
   +                    C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
   +                    C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
   +                    C = (C>>>8) ^ T[(C ^ 
(128|((d>>6)&15)|((c&3)<<4)))&0xFF];
   +                    C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
   +            } else {
   +                    C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
   +                    C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
   +                    C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
   +            }
   +    }
   +    return C ^ -1;
   +}
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to