From 3733473115a161bdf2f369e024906e1ef3211e74 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 2 Mar 2020 13:14:04 -0800 Subject: [PATCH] perf notes --- gun.js | 11 +++++++++-- lib/serve.js | 3 ++- lib/server.js | 2 +- lib/stats.js | 1 + lib/wire.js | 2 +- package.json | 4 +++- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/gun.js b/gun.js index 7c934200..cd423dc1 100644 --- a/gun.js +++ b/gun.js @@ -2094,7 +2094,7 @@ root.on('in', msg); //ECHO = msg.put || ECHO; !(msg.ok !== -3740) && mesh.say({ok: -3740, put: ECHO, '@': msg['#']}, peer); DBG && (DBG.hd = +new Date); - console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, 'msg'); + console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, 'msg'); // TODO: PERF: caught one > 1.5s on tgif dup_track(id).via = peer; mesh.leap = null; // warning! mesh.leap could be buggy. } @@ -2138,8 +2138,10 @@ if(!peer || !peer.id){ message = msg; if(!Type.obj.is(peer || opt.peers)){ return false } var S = +new Date; + var wr = meta.raw; meta.raw = raw; // quick perf hack Type.obj.map(peer || opt.peers, each); // in case peer is a peer list. - console.STAT && console.STAT(S, +new Date - S, 'say loop'); + meta.raw = wr; + console.STAT && console.STAT(S, +new Date - S, 'say loop'); // PERF: TODO: 1.4s+ on tgif, NEED PUFF return; } if(!peer.wire && mesh.wire){ mesh.wire(peer) } @@ -2210,6 +2212,10 @@ raw = raw.slice(0, tmp-1) + put + raw.slice(tmp + _.length + 1); //raw = raw.replace('"'+ _ +'"', put); // NEVER USE THIS! ALSO NEVER DELETE IT TO NOT MAKE SAME MISTAKE! https://github.com/amark/gun/wiki/@$$ Heisenbug }*/ + // TODO: PERF: tgif, CPU way too much on re-JSONifying ^ it. + /* + // NOTE TO SELF: Switch NTS to DAM now. + */ if(meta && (raw||'').length < (1000 * 100)){ meta.raw = raw } // HNPERF: If string too big, don't keep in memory. return raw; } @@ -2270,6 +2276,7 @@ setTimeout(function(){ delete gets[tmp] },opt.lack || 9000); }); root.on('hi', function(peer, tmp){ this.to.next(peer); + // TODO: PERF: these things are too slow. Probably `root.next` loop? Huh? is it discovering itself? What going on. if(!(tmp = peer.url) || !gets[tmp]){ return } delete gets[tmp]; Type.obj.map(root.next, function(node, soul){ tmp = {}; tmp[soul] = root.graph[soul]; diff --git a/lib/serve.js b/lib/serve.js index 2bc80c4f..2f689a19 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -34,8 +34,9 @@ function serve(req, res, next){ var tmp; } var S = +new Date; var rs = fs.createReadStream(path); - rs.on('open', function(){ console.STAT && console.STAT(S, +new Date - S, 'serve open file'); rs.pipe(res) }); + rs.on('open', function(){ rs.pipe(res) }); rs.on('error', function(err){ res.end(404+'') }); + rs.on('end', function(){ console.STAT && console.STAT(S, +new Date - S, 'serve file end') }); return true; } if((tmp = req.socket) && (tmp = tmp.server) && (tmp = tmp.route)){ var url; diff --git a/lib/server.js b/lib/server.js index 03b3f8bc..31da4b8a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2,7 +2,7 @@ var Gun = require('../gun'), u; Gun.serve = require('./serve'); //process.env.GUN_ENV = process.env.GUN_ENV || 'debug'; - console.LOG = {}; // only do this for dev. + //console.LOG = {}; // only do this for dev. Gun.on('opt', function(root){ if(u === root.opt.super){ root.opt.super = true } if(u === root.opt.faith){ root.opt.faith = true } // HNPERF: This should probably be off, but we're testing performance improvements, please audit. diff --git a/lib/stats.js b/lib/stats.js index 7af81aba..846d7d06 100644 --- a/lib/stats.js +++ b/lib/stats.js @@ -72,5 +72,6 @@ Gun.log = console.STAT = function(a,b,c,d){ if(max < tmp.push([a,b])){ all[c] = [] } // reset //return; } + if(!console.LOG || log.off){ return } return log.apply(Gun, arguments); } \ No newline at end of file diff --git a/lib/wire.js b/lib/wire.js index 6afdf5e1..c4eff369 100644 --- a/lib/wire.js +++ b/lib/wire.js @@ -78,7 +78,7 @@ Gun.on('opt', function(root){ opt.mesh.bye(peer); }); wire.on('error', function(e){}); - setTimeout(function heart(){ if(!opt.peers[peer.id]){ return } try{ wire.send("[]"); setTimeout(heart, 1000 * 20) }catch(e){} }, 1000 * 20); // Some systems, like Heroku, require heartbeats to not time out. // TODO: Make this configurable? + setTimeout(function heart(){ if(!opt.peers[peer.id]){ return } try{ wire.send("[]") }catch(e){} ;setTimeout(heart, 1000 * 20) }, 1000 * 20); // Some systems, like Heroku, require heartbeats to not time out. // TODO: Make this configurable? // TODO: PERF: Find better approach than try/timeouts? }); } diff --git a/package.json b/package.json index 4154ae90..5bfa5097 100644 --- a/package.json +++ b/package.json @@ -56,11 +56,13 @@ }, "dependencies": { "buffer": "^5.4.3", - "ws": "^7.1.2" + "ws": "^7.2.1" }, "optionalDependencies": { "text-encoding": "^0.7.0", "isomorphic-webcrypto": "^2.3.2", + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2", "emailjs": "^2.2.0" }, "devDependencies": {