@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 4.07 kB
JavaScript
import*as HeapSnapshotModel from"../../models/heap_snapshot_model/heap_snapshot_model.js";export class AllocationProfile{#t;#o;#e;#n;#i;#l;#s;constructor(t,o){this.#t=t.strings,this.#o=1,this.#e=[],this.#n={},this.#i={},this.#l={},this.#s=null,this.#a(t),this.#c(t,o)}#a(t){const o=this.#t,e=t.snapshot.meta.trace_function_info_fields,n=e.indexOf("name"),i=e.indexOf("script_name"),l=e.indexOf("script_id"),s=e.indexOf("line"),a=e.indexOf("column"),c=e.length,r=t.trace_function_infos,d=r.length,h=this.#e=new Array(d/c);let u=0;for(let t=0;t<d;t+=c)h[u++]=new FunctionAllocationInfo(o[r[t+n]],o[r[t+i]],r[t+l],r[t+s],r[t+a])}#c(t,o){const e=t.trace_tree,n=this.#e,i=this.#i,l=t.snapshot.meta.trace_node_fields,s=l.indexOf("id"),a=l.indexOf("function_info_index"),c=l.indexOf("count"),r=l.indexOf("size"),d=l.indexOf("children"),h=l.length;return function t(e,l,u){const p=n[e[l+a]],f=e[l+s],T=o[f],I=T?T.count:0,N=T?T.size:0,z=new TopDownAllocationNode(f,p,e[l+c],e[l+r],I,N,u);i[f]=z,p.addTraceTopNode(z);const m=e[l+d];for(let o=0;o<m.length;o+=h)z.children.push(t(m,o,z));return z}(e,0,null)}serializeTraceTops(){if(this.#s)return this.#s;const t=this.#s=[],o=this.#e;for(let e=0;e<o.length;e++){const n=o[e];if(0===n.totalCount)continue;const i=this.#o++,l=0===e;t.push(this.#r(i,n,n.totalCount,n.totalSize,n.totalLiveCount,n.totalLiveSize,!l)),this.#l[i]=n}return t.sort((function(t,o){return o.size-t.size})),t}serializeCallers(t){let o=this.#d(t);const e=[];for(;1===o.callers().length;)o=o.callers()[0],e.push(this.#h(o));const n=[],i=o.callers();for(let t=0;t<i.length;t++)n.push(this.#h(i[t]));return new HeapSnapshotModel.HeapSnapshotModel.AllocationNodeCallers(e,n)}serializeAllocationStack(t){let o=this.#i[t];const e=[];for(;o;){const t=o.functionInfo;e.push(new HeapSnapshotModel.HeapSnapshotModel.AllocationStackFrame(t.functionName,t.scriptName,t.scriptId,t.line,t.column)),o=o.parent}return e}traceIds(t){return this.#d(t).traceTopIds}#d(t){let o=this.#n[t];if(!o){o=this.#l[t].bottomUpRoot(),delete this.#l[t],this.#n[t]=o}return o}#h(t){const o=this.#o++;return this.#n[o]=t,this.#r(o,t.functionInfo,t.allocationCount,t.allocationSize,t.liveCount,t.liveSize,t.hasCallers())}#r(t,o,e,n,i,l,s){return new HeapSnapshotModel.HeapSnapshotModel.SerializedAllocationNode(t,o.functionName,o.scriptName,o.scriptId,o.line,o.column,e,n,i,l,s)}}export class TopDownAllocationNode{id;functionInfo;allocationCount;allocationSize;liveCount;liveSize;parent;children;constructor(t,o,e,n,i,l,s){this.id=t,this.functionInfo=o,this.allocationCount=e,this.allocationSize=n,this.liveCount=i,this.liveSize=l,this.parent=s,this.children=[]}}export class BottomUpAllocationNode{functionInfo;allocationCount;allocationSize;liveCount;liveSize;traceTopIds;#u;constructor(t){this.functionInfo=t,this.allocationCount=0,this.allocationSize=0,this.liveCount=0,this.liveSize=0,this.traceTopIds=[],this.#u=[]}addCaller(t){const o=t.functionInfo;let e;for(let t=0;t<this.#u.length;t++){const n=this.#u[t];if(n.functionInfo===o){e=n;break}}return e||(e=new BottomUpAllocationNode(o),this.#u.push(e)),e}callers(){return this.#u}hasCallers(){return this.#u.length>0}}export class FunctionAllocationInfo{functionName;scriptName;scriptId;line;column;totalCount;totalSize;totalLiveCount;totalLiveSize;#s;#p;constructor(t,o,e,n,i){this.functionName=t,this.scriptName=o,this.scriptId=e,this.line=n,this.column=i,this.totalCount=0,this.totalSize=0,this.totalLiveCount=0,this.totalLiveSize=0,this.#s=[]}addTraceTopNode(t){0!==t.allocationCount&&(this.#s.push(t),this.totalCount+=t.allocationCount,this.totalSize+=t.allocationSize,this.totalLiveCount+=t.liveCount,this.totalLiveSize+=t.liveSize)}bottomUpRoot(){return this.#s.length?(this.#p||this.#f(),this.#p):null}#f(){this.#p=new BottomUpAllocationNode(this);for(let t=0;t<this.#s.length;t++){let o=this.#s[t],e=this.#p;const n=o.allocationCount,i=o.allocationSize,l=o.liveCount,s=o.liveSize,a=o.id;for(;e.allocationCount+=n,e.allocationSize+=i,e.liveCount+=l,e.liveSize+=s,e.traceTopIds.push(a),o=o.parent,null!==o;)e=e.addCaller(o)}}}