Skip to content

Commit

Permalink
Merge pull request #300 from cspotcode/feature/performance-improvements
Browse files Browse the repository at this point in the history
Sort mappings on-demand to greatly improve performance
  • Loading branch information
tromey authored Nov 3, 2017
2 parents 2f17c63 + b09169a commit 264fcb4
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappings', {
enumerable: true,
get: function () {
if (!this.__generatedMappings) {
this._parseMappings(this._mappings, this.sourceRoot);
this._sortGeneratedMappings();
}

return this.__generatedMappings;
Expand All @@ -80,13 +80,39 @@ Object.defineProperty(SourceMapConsumer.prototype, '_originalMappings', {
enumerable: true,
get: function () {
if (!this.__originalMappings) {
this._parseMappings(this._mappings, this.sourceRoot);
this._sortOriginalMappings();
}

return this.__originalMappings;
}
});

SourceMapConsumer.prototype.__generatedMappingsUnsorted = null;
Object.defineProperty(SourceMapConsumer.prototype, '_generatedMappingsUnsorted', {
configurable: true,
enumerable: true,
get: function () {
if (!this.__generatedMappingsUnsorted) {
this._parseMappings(this._mappings, this.sourceRoot);
}

return this.__generatedMappingsUnsorted;
}
});

SourceMapConsumer.prototype.__originalMappingsUnsorted = null;
Object.defineProperty(SourceMapConsumer.prototype, '_originalMappingsUnsorted', {
configurable: true,
enumerable: true,
get: function () {
if (!this.__originalMappingsUnsorted) {
this._parseMappings(this._mappings, this.sourceRoot);
}

return this.__originalMappingsUnsorted;
}
});

SourceMapConsumer.prototype._charIsMappingSeparator =
function SourceMapConsumer_charIsMappingSeparator(aStr, index) {
var c = aStr.charAt(index);
Expand All @@ -103,6 +129,20 @@ SourceMapConsumer.prototype._parseMappings =
throw new Error("Subclasses must implement _parseMappings");
};

SourceMapConsumer.prototype._sortGeneratedMappings =
function SourceMapConsumer_sortGeneratedMappings() {
const mappings = this._generatedMappingsUnsorted;
quickSort(mappings, util.compareByGeneratedPositionsDeflated);
this.__generatedMappings = mappings;
};

SourceMapConsumer.prototype._sortOriginalMappings =
function SourceMapConsumer_sortOriginalMappings() {
const mappings = this._originalMappingsUnsorted;
quickSort(mappings, util.compareByOriginalPositions);
this.__originalMappings = mappings;
};

SourceMapConsumer.GENERATED_ORDER = 1;
SourceMapConsumer.ORIGINAL_ORDER = 2;

Expand Down Expand Up @@ -564,11 +604,9 @@ BasicSourceMapConsumer.prototype._parseMappings =
}
}

quickSort(generatedMappings, util.compareByGeneratedPositionsDeflated);
this.__generatedMappings = generatedMappings;
this.__generatedMappingsUnsorted = generatedMappings;

quickSort(originalMappings, util.compareByOriginalPositions);
this.__originalMappings = originalMappings;
this.__originalMappingsUnsorted = originalMappings;
};

/**
Expand Down Expand Up @@ -1097,8 +1135,8 @@ IndexedSourceMapConsumer.prototype.generatedPositionFor =
*/
IndexedSourceMapConsumer.prototype._parseMappings =
function IndexedSourceMapConsumer_parseMappings(aStr, aSourceRoot) {
this.__generatedMappings = [];
this.__originalMappings = [];
const generatedMappings = this.__generatedMappingsUnsorted = [];
const originalMappings = this.__originalMappingsUnsorted = [];
for (var i = 0; i < this._sections.length; i++) {
var section = this._sections[i];
var sectionMappings = section.consumer._generatedMappings;
Expand Down Expand Up @@ -1134,15 +1172,12 @@ IndexedSourceMapConsumer.prototype._parseMappings =
name: name
};

this.__generatedMappings.push(adjustedMapping);
generatedMappings.push(adjustedMapping);
if (typeof adjustedMapping.originalLine === 'number') {
this.__originalMappings.push(adjustedMapping);
originalMappings.push(adjustedMapping);
}
}
}

quickSort(this.__generatedMappings, util.compareByGeneratedPositionsDeflated);
quickSort(this.__originalMappings, util.compareByOriginalPositions);
};

exports.IndexedSourceMapConsumer = IndexedSourceMapConsumer;

0 comments on commit 264fcb4

Please sign in to comment.