Skip to content

Commit 0e1a908

Browse files
authored
Merge pull request #222 from rtfpessoa/fix-oom
feature: Add `maxLineSizeInBlockForComparison` to avoid OOM
2 parents 9eb33e8 + 3c06b40 commit 0e1a908

File tree

8 files changed

+197
-157
lines changed

8 files changed

+197
-157
lines changed

README.md

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"whatwg-fetch": "^3.0.0"
6161
},
6262
"devDependencies": {
63-
"autoprefixer": "^9.5.1",
63+
"autoprefixer": "^9.6.0",
6464
"browserify": "^16.2.3",
6565
"clean-css-cli": "^4.3.0",
6666
"codacy-coverage": "^3.4.0",
@@ -73,7 +73,7 @@
7373
"mocha": "5.2.0",
7474
"nopt": "^4.0.1",
7575
"postcss-cli": "^6.1.2",
76-
"uglify-js": "^3.5.9"
76+
"uglify-js": "^3.6.0"
7777
},
7878
"license": "MIT",
7979
"files": [

src/diff2html.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
matching: 'none',
2020
matchWordsThreshold: 0.25,
2121
matchingMaxComparisons: 2500,
22+
maxLineSizeInBlockForComparison: 200,
2223
maxLineLengthHighlight: 10000,
2324
renderNothingWhenEmpty: false
2425
};

src/line-by-line-printer.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,17 @@
9494
var deleteType;
9595

9696
var comparisons = oldLines.length * newLines.length;
97-
var maxComparisons = that.config.matchingMaxComparisons || 2500;
98-
var doMatching = comparisons < maxComparisons && (that.config.matching === 'lines' ||
99-
that.config.matching === 'words');
97+
98+
var maxLineSizeInBlock = Math.max.apply(null,
99+
[0].concat((oldLines.concat(newLines)).map(
100+
function(elem) {
101+
return elem.content.length;
102+
}
103+
)));
104+
105+
var doMatching = comparisons < that.config.matchingMaxComparisons &&
106+
maxLineSizeInBlock < that.config.maxLineSizeInBlockForComparison &&
107+
(that.config.matching === 'lines' || that.config.matching === 'words');
100108

101109
if (doMatching) {
102110
matches = matcher(oldLines, newLines);

src/side-by-side-printer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@
9696
var deleteType;
9797

9898
var comparisons = oldLines.length * newLines.length;
99-
var maxComparisons = that.config.matchingMaxComparisons || 2500;
100-
var doMatching = comparisons < maxComparisons && (that.config.matching === 'lines' ||
101-
that.config.matching === 'words');
99+
100+
var maxLineSizeInBlock = Math.max.apply(null, (oldLines.concat(newLines)).map(function(elem) {
101+
return elem.length;
102+
}));
103+
104+
var doMatching = comparisons < that.config.matchingMaxComparisons &&
105+
maxLineSizeInBlock < that.config.maxLineSizeInBlockForComparison &&
106+
(that.config.matching === 'lines' || that.config.matching === 'words');
102107

103108
if (doMatching) {
104109
matches = matcher(oldLines, newLines);

test/line-by-line-tests.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,23 +372,23 @@ describe('LineByLinePrinter', function() {
372372
' <div class="d2h-code-line d2h-info">@@ -1 +1 @@</div>\n' +
373373
' </td>\n' +
374374
'</tr><tr>\n' +
375-
' <td class="d2h-code-linenumber d2h-del d2h-change">\n' +
375+
' <td class="d2h-code-linenumber d2h-del">\n' +
376376
' <div class="line-num1">1</div>\n' +
377377
'<div class="line-num2"></div>\n' +
378378
' </td>\n' +
379-
' <td class="d2h-del d2h-change">\n' +
380-
' <div class="d2h-code-line d2h-del d2h-change">\n' +
379+
' <td class="d2h-del">\n' +
380+
' <div class="d2h-code-line d2h-del">\n' +
381381
' <span class="d2h-code-line-prefix">-</span>\n' +
382382
' <span class="d2h-code-line-ctn"><del>test</del></span>\n' +
383383
' </div>\n' +
384384
' </td>\n' +
385385
'</tr><tr>\n' +
386-
' <td class="d2h-code-linenumber d2h-ins d2h-change">\n' +
386+
' <td class="d2h-code-linenumber d2h-ins">\n' +
387387
' <div class="line-num1"></div>\n' +
388388
'<div class="line-num2">1</div>\n' +
389389
' </td>\n' +
390-
' <td class="d2h-ins d2h-change">\n' +
391-
' <div class="d2h-code-line d2h-ins d2h-change">\n' +
390+
' <td class="d2h-ins">\n' +
391+
' <div class="d2h-code-line d2h-ins">\n' +
392392
' <span class="d2h-code-line-prefix">+</span>\n' +
393393
' <span class="d2h-code-line-ctn"><ins>test1r</ins></span>\n' +
394394
' </div>\n' +

test/side-by-side-printer-tests.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ describe('SideBySidePrinter', function() {
253253
' <div class="d2h-code-side-line d2h-info">@@ -1 +1 @@</div>\n' +
254254
' </td>\n' +
255255
'</tr><tr>\n' +
256-
' <td class="d2h-code-side-linenumber d2h-del d2h-change">\n' +
256+
' <td class="d2h-code-side-linenumber d2h-del">\n' +
257257
' 1\n' +
258258
' </td>\n' +
259-
' <td class="d2h-del d2h-change">\n' +
260-
' <div class="d2h-code-side-line d2h-del d2h-change">\n' +
259+
' <td class="d2h-del">\n' +
260+
' <div class="d2h-code-side-line d2h-del">\n' +
261261
' <span class="d2h-code-line-prefix">-</span>\n' +
262262
' <span class="d2h-code-line-ctn"><del>test</del></span>\n' +
263263
' </div>\n' +
@@ -277,11 +277,11 @@ describe('SideBySidePrinter', function() {
277277
' <div class="d2h-code-side-line d2h-info"></div>\n' +
278278
' </td>\n' +
279279
'</tr><tr>\n' +
280-
' <td class="d2h-code-side-linenumber d2h-ins d2h-change">\n' +
280+
' <td class="d2h-code-side-linenumber d2h-ins">\n' +
281281
' 1\n' +
282282
' </td>\n' +
283-
' <td class="d2h-ins d2h-change">\n' +
284-
' <div class="d2h-code-side-line d2h-ins d2h-change">\n' +
283+
' <td class="d2h-ins">\n' +
284+
' <div class="d2h-code-side-line d2h-ins">\n' +
285285
' <span class="d2h-code-line-prefix">+</span>\n' +
286286
' <span class="d2h-code-line-ctn"><ins>test1r</ins></span>\n' +
287287
' </div>\n' +

0 commit comments

Comments
 (0)