@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
87 lines • 3.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSizeOfDfGraph = getSizeOfDfGraph;
const environment_1 = require("../../dataflow/environments/environment");
const vertex_1 = require("../../dataflow/graph/vertex");
const identifier_1 = require("../../dataflow/environments/identifier");
const object_sizeof_1 = __importDefault(require("object-sizeof"));
const objects_1 = require("../../util/objects");
/* we have to kill all processors linked in the default environment as they cannot be serialized and they are shared anyway */
function killBuiltInEnv(env) {
if (env === undefined) {
return undefined;
}
else if (env.id === environment_1.BuiltInEnvironment.id) {
/* in this case, the reference would be shared for sure */
return {
id: env.id,
parent: killBuiltInEnv(env.parent),
memory: new Map()
};
}
const memory = new Map();
for (const [k, v] of env.memory) {
memory.set(k, v.filter(v => v.type !== identifier_1.ReferenceType.BuiltInFunction && v.type !== identifier_1.ReferenceType.BuiltInConstant && !('processor' in v)));
}
return {
id: env.id,
parent: killBuiltInEnv(env.parent),
memory
};
}
/** Returns the size of the given df graph in bytes (without sharing in-memory) */
function getSizeOfDfGraph(df) {
const verts = [];
for (const [, v] of df.vertices(true)) {
let vertex = v;
if (vertex.environment) {
vertex = {
...vertex,
environment: {
...vertex.environment,
current: killBuiltInEnv(v.environment?.current)
}
};
}
if (vertex.tag === vertex_1.VertexType.FunctionDefinition) {
vertex = {
...vertex,
subflow: {
...vertex.subflow,
environment: {
...vertex.subflow.environment,
current: killBuiltInEnv(vertex.subflow.environment.current)
}
}
};
}
vertex = (0, objects_1.compactRecord)({
...vertex,
/* shared anyway by using constants */
tag: undefined
});
verts.push(vertex);
}
return safeSizeOf([...verts, ...df.edges()]);
}
/**
* Calculates the size of an array in bytes.
*
* @param array - The array to calculate the size of.
* @returns The size of the array in bytes.
*/
function safeSizeOf(array) {
const size = (0, object_sizeof_1.default)(array);
if (typeof size === 'number') {
return size;
}
// the sizeOf method returns an error object, when the size could not be calculated
// in this case, we split the array in half and calculate the size of each half recursively
const chunkSize = Math.ceil(array.length / 2);
// subtract 1, because of the separate stringification of the array
return safeSizeOf(array.slice(0, chunkSize)) + safeSizeOf(array.slice(chunkSize)) - 1;
}
//# sourceMappingURL=size-of.js.map