Skip to content

Commit

Permalink
Merge pull request #49 from cenfun/performance-issue-47
Browse files Browse the repository at this point in the history
Performance issue 47
  • Loading branch information
cenfun authored Jun 28, 2024
2 parents 78e067b + 04876a3 commit 4e48a8b
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 119 deletions.
43 changes: 23 additions & 20 deletions lib/converter/collect-source-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,6 @@ const resolveSourceMap = (data, url) => {

};

const saveSourceFile = async (filePath, data) => {
await Util.writeFile(filePath, JSON.stringify(data));

// save source and sourcemap file for debug
// https://evanw.github.io/source-map-visualization
// if (data.sourceMap) {
// await Util.writeFile(`${filePath}.js`, data.source);
// await Util.writeFile(`${filePath}.js.map`, JSON.stringify(data.sourceMap));
// }

};

const getInlineSourceMap = (content) => {
let smc;
try {
Expand All @@ -153,7 +141,8 @@ const getInlineSourceMap = (content) => {

const collectSourceMaps = async (v8list, options) => {

const smList = [];
const sourceList = [];
const sourcemapList = [];
const concurrency = new Concurrency();
for (const item of v8list) {

Expand Down Expand Up @@ -192,11 +181,13 @@ const collectSourceMaps = async (v8list, options) => {
const smc = getInlineSourceMap(source);
if (smc) {
sourceData.sourceMap = resolveSourceMap(smc.sourcemap, url);
smList.push({
sourcemapList.push({
url,
inline: true
});
await saveSourceFile(sourcePath, sourceData);
sourceList.push({
sourcePath, sourceData
});
continue;
}
// from url async
Expand All @@ -212,26 +203,38 @@ const collectSourceMaps = async (v8list, options) => {
}

// no need check sourceMap
await saveSourceFile(sourcePath, sourceData);
sourceList.push({
sourcePath, sourceData
});

}

// from url concurrency
await concurrency.start(async (item) => {
const { sourceMapUrl, sourceData } = item;
const {
sourceMapUrl, sourcePath, sourceData
} = item;
const { url } = sourceData;
const data = await loadSourceMap(sourceMapUrl);
if (data) {
sourceData.sourceMap = resolveSourceMap(data, url);
smList.push({
sourcemapList.push({
url,
sourceMapUrl
});
}
await saveSourceFile(item.sourcePath, sourceData);

sourceList.push({
sourcePath,
sourceData
});

});

return smList;
return {
sourceList,
sourcemapList
};

};

Expand Down
15 changes: 11 additions & 4 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ const convertV8List = (v8list, options) => {
delete item.ranges;
}
}
Util.logTime(`- parsed ast: ${sourcePath} (${EC.cyan(Util.BSF(maxContentLength))})`, time_start_ast);
Util.logTime(` ┌ [convert] parsed ast: ${sourcePath} (${EC.cyan(Util.BSF(maxContentLength))})`, time_start_ast);

// console.log(sourcePath, astInfo.statements.length);
// ============================
Expand All @@ -1549,7 +1549,9 @@ const convertV8List = (v8list, options) => {
locator,
maxContentLength,
decodedMappings: [],
cacheMap: new Map(),
rangeCache: new Map(),
diffCache: new Map(),
// cacheHits: 0,
// coverage info
bytes: [],
functions: [],
Expand All @@ -1566,7 +1568,7 @@ const convertV8List = (v8list, options) => {
const time_start_unpack = Date.now();
unpackDistFile(item, state, options);
const unpackedFiles = EC.cyan(`${state.originalList.length} files`);
Util.logTime(`- unpacked dist: ${sourcePath} (${unpackedFiles})`, time_start_unpack);
Util.logTime(` ┌ [convert] unpacked sourcemap: ${sourcePath} (${unpackedFiles})`, time_start_unpack);

stateList.push(state);

Expand All @@ -1575,7 +1577,12 @@ const convertV8List = (v8list, options) => {
const time_start_convert = Date.now();
const dataList = generateV8DataList(stateList, options);
const dataFiles = EC.cyan(`${dataList.v8DataList.length} files`);
Util.logTime(`- generated data list (${dataFiles})`, time_start_convert);

// stateList.forEach((st) => {
// console.log('diffCache', st.diffCache.size, 'cacheHits', st.cacheHits);
// });

Util.logTime(` ┌ [convert] converted data list (${dataFiles})`, time_start_convert);

return dataList;
};
Expand Down
47 changes: 35 additions & 12 deletions lib/converter/find-original-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ const findMapping = (list, offset) => {

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

const alignText = (gt, ot) => {
const alignText = (gt, ot, info) => {

const { diffCache } = info.state;

if (diffCache.has(gt)) {
const subMap = diffCache.get(gt);
if (subMap.has(ot)) {
// info.state.cacheHits += 1;
return subMap.get(ot);
}
}

const toList = (s) => {
return s.split('').map((v, i) => {
Expand Down Expand Up @@ -115,6 +125,15 @@ const alignText = (gt, ot) => {
mergeList(gList, oList);
}

if (diffCache.has(gt)) {
const subMap = diffCache.get(gt);
subMap.set(ot, gl);
} else {
const subMap = new Map();
diffCache.set(gt, subMap);
subMap.set(ot, gl);
}

// console.log(gl);
return gl;
};
Expand All @@ -136,7 +155,11 @@ const getWordEndPosition = (list, gp, direction) => {
}
};

const getAlignPosition = (gt, gp, ot, direction) => {
const getAlignPosition = (info, direction) => {

const gt = info.generatedText;
const gp = info.generatedPos;
const ot = info.originalText;

// there is no need to align for long text
const maxLength = 500;
Expand All @@ -162,7 +185,7 @@ const getAlignPosition = (gt, gp, ot, direction) => {
// only for original first line text

// exclusive
const list = alignText(gt, ot);
const list = alignText(gt, ot, info);
const item = list[gp];
if (item && item.original) {
return {
Expand Down Expand Up @@ -305,7 +328,7 @@ const getComparedPosition = (info, direction) => {

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

return getAlignPosition(gt, gp, ot, direction);
return getAlignPosition(info, direction);

};

Expand All @@ -325,7 +348,7 @@ const getSimilarPosition = (info, direction) => {
}

// no need comparison for fake source
if (info.fake) {
if (info.state.fake) {
return;
}

Expand Down Expand Up @@ -505,7 +528,7 @@ const getFixedOriginalStart = (start, mappings, state, cache) => {
const direction = 'start';

const info = {
fake: state.fake,
state,
generatedText,
generatedPos,
originalText,
Expand Down Expand Up @@ -558,7 +581,7 @@ const getFixedOriginalEnd = (end, mappings, state, cache) => {
const direction = 'end';

const info = {
fake: state.fake,
state,
offset: end,
generatedText,
generatedPos,
Expand Down Expand Up @@ -826,11 +849,11 @@ const getMappingInfo = (start, end, state) => {

const findOriginalRange = (start, end, state, originalMap) => {

const { sourcePath, cacheMap } = state;
const { sourcePath, rangeCache } = state;

const key = `${start}_${end}`;
if (cacheMap.has(key)) {
return cacheMap.get(key);
if (rangeCache.has(key)) {
return rangeCache.get(key);
}

const createMappingError = (errors) => {
Expand All @@ -843,7 +866,7 @@ const findOriginalRange = (start, end, state, originalMap) => {
};

// cache error response
cacheMap.set(key, res);
rangeCache.set(key, res);

return res;
};
Expand Down Expand Up @@ -884,7 +907,7 @@ const findOriginalRange = (start, end, state, originalMap) => {
};

// cache response
cacheMap.set(key, res);
rangeCache.set(key, res);

return res;

Expand Down
Loading

0 comments on commit 4e48a8b

Please sign in to comment.