Skip to content

Commit

Permalink
add forceGC
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Oct 6, 2024
1 parent ae6f5e3 commit 4ff78cf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
14 changes: 7 additions & 7 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ const resolveEntrySource = (entry, sourceMapCache, sourceCache) => {
const url = entry.url;

// source from `source-id.json`
const sourceData = sourceCache[url];
const sourceData = sourceCache.get(url);
if (sourceData) {
entry.source = sourceData.source;
return;
Expand Down Expand Up @@ -424,7 +424,7 @@ const readCoverageData = async (dir, filename, entryFilter, sourceCache) => {
};

const readSourceList = async (dir, sourceList) => {
const sourceCache = {};
const sourceCache = new Map();

for (const filename of sourceList) {
const content = await Util.readFile(path.resolve(dir, filename));
Expand All @@ -436,7 +436,7 @@ const readSourceList = async (dir, sourceList) => {
continue;
}
if (json.url) {
sourceCache[json.url] = json;
sourceCache.set(json.url, json);
}
}

Expand Down Expand Up @@ -476,16 +476,16 @@ const readFromDir = async (mcr, dir) => {

const entryFilter = mcr.getEntryFilter();

const results = [];
for (const filename of coverageList) {
const coverageData = await readCoverageData(dir, filename, entryFilter, sourceCache);
if (coverageData) {
const res = await mcr.add(coverageData);
results.push(res);
await mcr.add(coverageData);
}
}

return results;
// GC
sourceCache.clear();

};

module.exports = {
Expand Down
3 changes: 1 addition & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ class CoverageReport {
// add coverage from dir
async addFromDir(dir) {
const time_start = Date.now();
const results = await readFromDir(this, dir);
await readFromDir(this, dir);
Util.logTime(`added from dir: ${dir}`, time_start);
return results;
}

// generate report
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const { writeFile, readFile } = require('fs/promises');
const path = require('path');
const os = require('os');
const crypto = require('crypto');
const v8 = require('node:v8');
const vm = require('node:vm');

const EC = require('eight-colors');
const CG = require('console-grid');
const acornWalk = require('acorn-walk');
Expand Down Expand Up @@ -788,6 +791,15 @@ const Util = {
return major1 - major2;
},

forceGC: () => {
if (!Util.expose_gc) {
v8.setFlagsFromString('--expose_gc');
Util.expose_gc = true;
}
const gc = vm.runInNewContext('gc');
gc();
},

// ==========================================================================================

loggingLevels: {
Expand Down
29 changes: 19 additions & 10 deletions lib/v8/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const mergeCssRanges = (itemList) => {
// ranges: [ {start, end} ]
const ranges = dedupeFlatRanges(concatRanges);

resolve(ranges || []);
resolve(ranges);
});
};

Expand All @@ -174,14 +174,19 @@ const mergeJsFunctions = async (itemList) => {
};

const mergeV8DataList = async (dataList) => {
let allList = [];
dataList.forEach((d) => {
allList = allList.concat(d.data);
const allList = dataList.map((d) => d.data).flat();

// separate coverage and empty items
const coverageList = [];
const allEmptyList = [];
allList.forEach((it) => {
if (it.empty) {
allEmptyList.push(it);
} else {
coverageList.push(it);
}
});

// remove empty items
const coverageList = allList.filter((it) => !it.empty);

// connect all functions and ranges
const itemMap = {};
const sourcePathMap = {};
Expand Down Expand Up @@ -215,7 +220,7 @@ const mergeV8DataList = async (dataList) => {

// first time filter for empty, (not for sources)
// empty and not in item map
const emptyList = allList.filter((it) => it.empty).filter((it) => {
const emptyList = allEmptyList.filter((it) => {
if (itemMap[it.id]) {
return false;
}
Expand All @@ -225,9 +230,10 @@ const mergeV8DataList = async (dataList) => {
return true;
});

// empty list + merged list
const mergedList = emptyList.concat(Object.values(itemMap));
// merged list + empty list
const mergedList = Object.values(itemMap).concat(emptyList);

// Util.forceGC();

return mergedList;
};
Expand All @@ -254,6 +260,9 @@ const mergeV8Coverage = async (dataList, sourceCache, options) => {

}

// GC
sourceCache.clear();

return mergedList;
};

Expand Down

0 comments on commit 4ff78cf

Please sign in to comment.