mirror of
https://github.com/amark/gun.git
synced 2025-06-03 20:56:43 +00:00
Only track msg meta data, renew original request on @, fix faith/raw/rad unique, better GC, GC tracker too.
This commit is contained in:
parent
06099bf881
commit
ecf4fda285
38
gun.js
38
gun.js
@ -619,7 +619,7 @@
|
||||
var Type = USE('./type');
|
||||
function Dup(opt){
|
||||
var dup = {s:{}};
|
||||
opt = opt || {max: 1000, age: /*1000 * 9};//*/ 1000 * 60 * 2};
|
||||
opt = opt || {max: 1000, age: /*1000 * 9};//*/ 1000 * 9 * 3};
|
||||
dup.check = function(id){ var tmp;
|
||||
if(!(tmp = dup.s[id])){ return false }
|
||||
if(tmp.pass){ return tmp.pass = false }
|
||||
@ -629,18 +629,17 @@
|
||||
var it = dup.s[id] || (dup.s[id] = {});
|
||||
it.was = time_is();
|
||||
if(pass){ it.pass = true }
|
||||
if(!dup.to){
|
||||
dup.to = setTimeout(function(){
|
||||
var now = time_is();
|
||||
Type.obj.map(dup.s, function(it, id){
|
||||
if(it && opt.age > (now - it.was)){ return }
|
||||
Type.obj.del(dup.s, id);
|
||||
});
|
||||
dup.to = null;
|
||||
}, opt.age + 9);
|
||||
}
|
||||
if(!dup.to){ dup.to = setTimeout(dup.drop, opt.age + 9) }
|
||||
return it;
|
||||
}
|
||||
dup.drop = function(age){
|
||||
var now = time_is();
|
||||
Type.obj.map(dup.s, function(it, id){
|
||||
if(it && (age || opt.age) > (now - it.was)){ return }
|
||||
Type.obj.del(dup.s, id);
|
||||
});
|
||||
dup.to = null;
|
||||
}
|
||||
return dup;
|
||||
}
|
||||
var time_is = Type.time.is;
|
||||
@ -702,7 +701,8 @@
|
||||
return;
|
||||
}
|
||||
dup.track(tmp);
|
||||
if(!at.ask(msg['@'], msg)){
|
||||
if(tmp = msg['@']){ dup.track(tmp) } // HNPERF: Bump original request's liveliness.
|
||||
if(!at.ask(tmp, msg)){
|
||||
if(msg.get){
|
||||
Gun.on.get(msg, gun); //at.on('get', get(msg));
|
||||
}
|
||||
@ -1995,11 +1995,11 @@
|
||||
if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) }
|
||||
if(msg.DBG_s){ opt.log(+new Date - msg.DBG_s, 'to hear', id) }
|
||||
if(dup.check(id)){ return }
|
||||
dup.track(id, true).it = msg; // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore?
|
||||
dup.track(id, true).it = it(msg); // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore?
|
||||
if(!(hash = msg['##']) && u !== msg.put){ hash = msg['##'] = Type.obj.hash(msg.put) }
|
||||
if(hash && (tmp = msg['@'] || (msg.get && id))){ // Reduces backward daisy in case varying hashes at different daisy depths are the same.
|
||||
if(dup.check(tmp+hash)){ return }
|
||||
dup.track(tmp+hash, true).it = msg; // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore?
|
||||
dup.track(tmp+hash, true).it = it(msg); // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore?
|
||||
}
|
||||
(msg._ = function(){}).via = peer;
|
||||
if(tmp = msg['><']){ (msg._).to = Type.obj.map(tmp.split(','), tomap) }
|
||||
@ -2030,9 +2030,9 @@
|
||||
if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) }
|
||||
if(!(hash = msg['##']) && u !== msg.put){ hash = msg['##'] = Type.obj.hash(msg.put) }
|
||||
if(!(raw = meta.raw)){
|
||||
raw = meta.raw = mesh.raw(msg);
|
||||
raw = mesh.raw(msg);
|
||||
if(hash && (tmp = msg['@'])){
|
||||
dup.track(tmp+hash).it = msg;
|
||||
dup.track(tmp+hash).it = it(msg);
|
||||
if(tmp = (dup.s[tmp]||ok).it){
|
||||
if(hash === tmp['##']){ return false }
|
||||
tmp['##'] = hash;
|
||||
@ -2040,7 +2040,7 @@
|
||||
}
|
||||
}
|
||||
//LOG && opt.log(S, +new Date - S, 'say prep');
|
||||
dup.track(id).it = msg; // track for 9 seconds, default. Earth<->Mars would need more!
|
||||
dup.track(id).it = it(msg); // track for 9 seconds, default. Earth<->Mars would need more!
|
||||
if(!peer){ peer = (tmp = dup.s[msg['@']]) && (tmp = tmp.it) && (tmp = tmp._) && (tmp = tmp.via) }
|
||||
if(!peer && msg['@']){ return false } // TODO: Temporary? If ack via trace has been lost, acks will go to all peers, which trashes browser bandwidth. Not relaying the ack will force sender to ask for ack again. Note, this is technically wrong for mesh behavior.
|
||||
if(!peer && mesh.way){ return mesh.way(msg) }
|
||||
@ -2116,7 +2116,7 @@
|
||||
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
|
||||
}*/
|
||||
if(meta){ meta.raw = raw }
|
||||
if(meta && (raw||'').length < (1000 * 100)){ meta.raw = raw } // HNPERF: If string too big, don't keep in memory.
|
||||
return raw;
|
||||
}
|
||||
var $ = JSON.stringify, _ = ':])([:';
|
||||
@ -2218,6 +2218,8 @@
|
||||
}
|
||||
}());
|
||||
|
||||
function it(msg){ return {_: msg._, '##': msg['##']} } // HNPERF: Only need some meta data, not full reference (took up too much memory).
|
||||
|
||||
var empty = {}, ok = true, u;
|
||||
var LOG = console.LOG;
|
||||
|
||||
|
16
lib/evict.js
16
lib/evict.js
@ -7,17 +7,18 @@
|
||||
if(typeof process == 'undefined'){ return }
|
||||
var util = process.memoryUsage, heap;
|
||||
if(!util){ return }
|
||||
try{ heap = require('v8').getHeapStatistics }catch(e){} heap = heap || function(){};
|
||||
try{ heap = require('v8').getHeapStatistics }catch(e){}
|
||||
if(!heap){ return }
|
||||
|
||||
ev.max = parseFloat(root.opt.memory || (heap().heap_size_limit / 1024 / 1024 / 2) || process.env.WEB_MEMORY || 1399) * 0.8; // max_old_space_size defaults to 1400 MB. Note: old space !== memory space though.
|
||||
ev.max = parseFloat(root.opt.memory || (heap().heap_size_limit / 1024 / 1024) || process.env.WEB_MEMORY || 1399) * 0.8; // max_old_space_size defaults to 1400 MB. Note: old space !== memory space though.
|
||||
|
||||
setInterval(check, 1000);
|
||||
function check(){
|
||||
//var used = ev.used = util().rss / 1024 / 1024;
|
||||
var used = ev.used = heap().used_heap_size / 1024 / 1024;
|
||||
console.LOG && Gun.log('memory:', used, ev.max);
|
||||
if(used < ev.max){ return }
|
||||
setTimeout(GC, 1);
|
||||
var used = util().rss / 1024 / 1024;
|
||||
var hused = heap().used_heap_size / 1024 / 1024;
|
||||
if(hused < ev.max && used < ev.max){ return }
|
||||
console.LOG && Gun.log('evict memory:', hused.toFixed(), used.toFixed(), ev.max.toFixed());
|
||||
GC();//setTimeout(GC, 1);
|
||||
}
|
||||
function GC(){
|
||||
var souls = Object.keys(root.graph||empty);
|
||||
@ -27,6 +28,7 @@
|
||||
if(--toss < 0){ return }
|
||||
root.gun.get(soul).off();
|
||||
});
|
||||
root.dup.drop(1000 * 9); // clean up message tracker
|
||||
}
|
||||
/*
|
||||
root.on('in', function(msg){
|
||||
|
@ -87,7 +87,7 @@ Gun.on('create', function(root){
|
||||
return;
|
||||
}
|
||||
}
|
||||
var S = (+new Date), C = 0; // STATS!
|
||||
var S = (+new Date), C = 0, CC = 0; // STATS!
|
||||
rad(key||'', function(err, data, o){
|
||||
try{opt.store.stats.get.time[statg % 50] = (+new Date) - S; ++statg;
|
||||
opt.store.stats.get.count++;
|
||||
@ -105,15 +105,15 @@ Gun.on('create', function(root){
|
||||
}
|
||||
if(!graph && data){ each(data, '') }
|
||||
}
|
||||
LOG && Gun.log(S, +new Date - S, 'got prep count', C); S = +new Date;
|
||||
var faith = function(){}; faith.faith = true; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
|
||||
LOG && Gun.log(S, +new Date - S, 'got prep count', CC, C); S = +new Date; CC = 0;
|
||||
var faith = function(){}; faith.faith = true; faith.rad = get; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
|
||||
root.on('in', {'@': id, put: graph, '%': o.more? 1 : u, err: err? err : u, _: faith});
|
||||
LOG && Gun.log(S, +new Date - S, 'got sent nodes in graph', Object.keys(graph||{}).length);
|
||||
graph = u; // each is outside our scope, we have to reset graph to nothing!
|
||||
}, o);
|
||||
LOG && Gun.log(S, +new Date - S, 'get call');
|
||||
function each(val, has, a,b){ // TODO: THIS CODE NEEDS TO BE FASTER!!!!
|
||||
C++;
|
||||
C++; ++CC;
|
||||
if(!val){ return }
|
||||
has = (key+has).split(esc);
|
||||
var soul = has.slice(0,1)[0];
|
||||
@ -125,7 +125,6 @@ Gun.on('create', function(root){
|
||||
(graph = graph || {})[soul] = Gun.state.ify(graph[soul], has, state, val, soul);
|
||||
if(o.limit && o.limit <= o.count){ return true }
|
||||
}
|
||||
each.rad = get;
|
||||
LOG = console.LOG;
|
||||
});
|
||||
opt.store.stats = {get:{time:{}, count:0}, put: {time:{}, count:0}}; // STATS!
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gun",
|
||||
"version": "0.2019.1228",
|
||||
"version": "0.2020.115",
|
||||
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
|
||||
"main": "index.js",
|
||||
"browser": "browser.js",
|
||||
|
Loading…
x
Reference in New Issue
Block a user