feature: Add maxLineSizeInBlockForComparison to avoid OOM

Fixes #220
This commit is contained in:
Rodrigo Fernandes 2019-06-14 16:18:09 +01:00
parent 3ad45ff86e
commit 3c06b401b9
No known key found for this signature in database
GPG key ID: C5DD1B4104382F6B
6 changed files with 33 additions and 18 deletions

View file

@ -196,6 +196,7 @@ The HTML output accepts a Javascript object with configuration. Possible options
- `matching`: matching level: `'lines'` for matching lines, `'words'` for matching lines and words or `'none'`, default is `none`
- `matchWordsThreshold`: similarity threshold for word matching, default is 0.25
- `matchingMaxComparisons`: perform at most this much comparisons for line matching a block of changes, default is `2500`
- `maxLineSizeInBlockForComparison`: maximum number os characters of the bigger line in a block to apply comparison, default is `200`
- `maxLineLengthHighlight`: only perform diff changes highlight if lines are smaller than this, default is `10000`
- `templates`: object with previously compiled templates to replace parts of the html
- `rawTemplates`: object with raw not compiled templates to replace parts of the html

View file

@ -19,6 +19,7 @@
matching: 'none',
matchWordsThreshold: 0.25,
matchingMaxComparisons: 2500,
maxLineSizeInBlockForComparison: 200,
maxLineLengthHighlight: 10000,
renderNothingWhenEmpty: false
};

View file

@ -94,9 +94,17 @@
var deleteType;
var comparisons = oldLines.length * newLines.length;
var maxComparisons = that.config.matchingMaxComparisons || 2500;
var doMatching = comparisons < maxComparisons && (that.config.matching === 'lines' ||
that.config.matching === 'words');
var maxLineSizeInBlock = Math.max.apply(null,
[0].concat((oldLines.concat(newLines)).map(
function(elem) {
return elem.content.length;
}
)));
var doMatching = comparisons < that.config.matchingMaxComparisons &&
maxLineSizeInBlock < that.config.maxLineSizeInBlockForComparison &&
(that.config.matching === 'lines' || that.config.matching === 'words');
if (doMatching) {
matches = matcher(oldLines, newLines);

View file

@ -96,9 +96,14 @@
var deleteType;
var comparisons = oldLines.length * newLines.length;
var maxComparisons = that.config.matchingMaxComparisons || 2500;
var doMatching = comparisons < maxComparisons && (that.config.matching === 'lines' ||
that.config.matching === 'words');
var maxLineSizeInBlock = Math.max.apply(null, (oldLines.concat(newLines)).map(function(elem) {
return elem.length;
}));
var doMatching = comparisons < that.config.matchingMaxComparisons &&
maxLineSizeInBlock < that.config.maxLineSizeInBlockForComparison &&
(that.config.matching === 'lines' || that.config.matching === 'words');
if (doMatching) {
matches = matcher(oldLines, newLines);

View file

@ -372,23 +372,23 @@ describe('LineByLinePrinter', function() {
' <div class="d2h-code-line d2h-info">@@ -1 +1 @@</div>\n' +
' </td>\n' +
'</tr><tr>\n' +
' <td class="d2h-code-linenumber d2h-del d2h-change">\n' +
' <td class="d2h-code-linenumber d2h-del">\n' +
' <div class="line-num1">1</div>\n' +
'<div class="line-num2"></div>\n' +
' </td>\n' +
' <td class="d2h-del d2h-change">\n' +
' <div class="d2h-code-line d2h-del d2h-change">\n' +
' <td class="d2h-del">\n' +
' <div class="d2h-code-line d2h-del">\n' +
' <span class="d2h-code-line-prefix">-</span>\n' +
' <span class="d2h-code-line-ctn"><del>test</del></span>\n' +
' </div>\n' +
' </td>\n' +
'</tr><tr>\n' +
' <td class="d2h-code-linenumber d2h-ins d2h-change">\n' +
' <td class="d2h-code-linenumber d2h-ins">\n' +
' <div class="line-num1"></div>\n' +
'<div class="line-num2">1</div>\n' +
' </td>\n' +
' <td class="d2h-ins d2h-change">\n' +
' <div class="d2h-code-line d2h-ins d2h-change">\n' +
' <td class="d2h-ins">\n' +
' <div class="d2h-code-line d2h-ins">\n' +
' <span class="d2h-code-line-prefix">+</span>\n' +
' <span class="d2h-code-line-ctn"><ins>test1r</ins></span>\n' +
' </div>\n' +

View file

@ -253,11 +253,11 @@ describe('SideBySidePrinter', function() {
' <div class="d2h-code-side-line d2h-info">@@ -1 +1 @@</div>\n' +
' </td>\n' +
'</tr><tr>\n' +
' <td class="d2h-code-side-linenumber d2h-del d2h-change">\n' +
' <td class="d2h-code-side-linenumber d2h-del">\n' +
' 1\n' +
' </td>\n' +
' <td class="d2h-del d2h-change">\n' +
' <div class="d2h-code-side-line d2h-del d2h-change">\n' +
' <td class="d2h-del">\n' +
' <div class="d2h-code-side-line d2h-del">\n' +
' <span class="d2h-code-line-prefix">-</span>\n' +
' <span class="d2h-code-line-ctn"><del>test</del></span>\n' +
' </div>\n' +
@ -277,11 +277,11 @@ describe('SideBySidePrinter', function() {
' <div class="d2h-code-side-line d2h-info"></div>\n' +
' </td>\n' +
'</tr><tr>\n' +
' <td class="d2h-code-side-linenumber d2h-ins d2h-change">\n' +
' <td class="d2h-code-side-linenumber d2h-ins">\n' +
' 1\n' +
' </td>\n' +
' <td class="d2h-ins d2h-change">\n' +
' <div class="d2h-code-side-line d2h-ins d2h-change">\n' +
' <td class="d2h-ins">\n' +
' <div class="d2h-code-side-line d2h-ins">\n' +
' <span class="d2h-code-line-prefix">+</span>\n' +
' <span class="d2h-code-line-ctn"><ins>test1r</ins></span>\n' +
' </div>\n' +