Skip to content

Commit 8647951

Browse files
committed
Remove the custom JS Quicksort implementation
We originally added it because Array.prototype.sort was not self-hosted with custom comparison functions, but that's no longer true these days. We also do most of our heavy sorting in wasm now.
1 parent 6ae7233 commit 8647951

File tree

4 files changed

+17
-298
lines changed

4 files changed

+17
-298
lines changed

dist/source-map.js

Lines changed: 15 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
exports["sourceMap"] = factory(require("fs"), require("path"));
88
else
99
root["sourceMap"] = factory(root["fs"], root["path"]);
10-
})(this, function(__WEBPACK_EXTERNAL_MODULE_11__, __WEBPACK_EXTERNAL_MODULE_12__) {
10+
})(this, function(__WEBPACK_EXTERNAL_MODULE_10__, __WEBPACK_EXTERNAL_MODULE_11__) {
1111
return /******/ (function(modules) { // webpackBootstrap
1212
/******/ // The module cache
1313
/******/ var installedModules = {};
@@ -61,7 +61,7 @@ return /******/ (function(modules) { // webpackBootstrap
6161
*/
6262
exports.SourceMapGenerator = __webpack_require__(1).SourceMapGenerator;
6363
exports.SourceMapConsumer = __webpack_require__(7).SourceMapConsumer;
64-
exports.SourceNode = __webpack_require__(14).SourceNode;
64+
exports.SourceNode = __webpack_require__(13).SourceNode;
6565

6666

6767
/***/ }),
@@ -1413,9 +1413,8 @@ return /******/ (function(modules) { // webpackBootstrap
14131413
var binarySearch = __webpack_require__(8);
14141414
var ArraySet = __webpack_require__(5).ArraySet;
14151415
var base64VLQ = __webpack_require__(2);
1416-
var quickSort = __webpack_require__(9).quickSort;
1417-
var readWasm = __webpack_require__(10);
1418-
var wasm = __webpack_require__(13);
1416+
var readWasm = __webpack_require__(9);
1417+
var wasm = __webpack_require__(12);
14191418

14201419
function SourceMapConsumer(aSourceMap, aSourceMapURL) {
14211420
var sourceMap = aSourceMap;
@@ -2314,14 +2313,14 @@ return /******/ (function(modules) { // webpackBootstrap
23142313
IndexedSourceMapConsumer.prototype._sortGeneratedMappings =
23152314
function IndexedSourceMapConsumer_sortGeneratedMappings() {
23162315
const mappings = this._generatedMappingsUnsorted;
2317-
quickSort(mappings, util.compareByGeneratedPositionsDeflated);
2316+
mappings.sort(util.compareByGeneratedPositionsDeflated);
23182317
this.__generatedMappings = mappings;
23192318
};
23202319

23212320
IndexedSourceMapConsumer.prototype._sortOriginalMappings =
23222321
function IndexedSourceMapConsumer_sortOriginalMappings() {
23232322
const mappings = this._originalMappingsUnsorted;
2324-
quickSort(mappings, util.compareByOriginalPositions);
2323+
mappings.sort(util.compareByOriginalPositions);
23252324
this.__originalMappings = mappings;
23262325
};
23272326

@@ -2814,126 +2813,6 @@ return /******/ (function(modules) { // webpackBootstrap
28142813

28152814
/***/ }),
28162815
/* 9 */
2817-
/***/ (function(module, exports) {
2818-
2819-
/* -*- Mode: js; js-indent-level: 2; -*- */
2820-
/*
2821-
* Copyright 2011 Mozilla Foundation and contributors
2822-
* Licensed under the New BSD license. See LICENSE or:
2823-
* http://opensource.org/licenses/BSD-3-Clause
2824-
*/
2825-
2826-
// It turns out that some (most?) JavaScript engines don't self-host
2827-
// `Array.prototype.sort`. This makes sense because C++ will likely remain
2828-
// faster than JS when doing raw CPU-intensive sorting. However, when using a
2829-
// custom comparator function, calling back and forth between the VM's C++ and
2830-
// JIT'd JS is rather slow *and* loses JIT type information, resulting in
2831-
// worse generated code for the comparator function than would be optimal. In
2832-
// fact, when sorting with a comparator, these costs outweigh the benefits of
2833-
// sorting in C++. By using our own JS-implemented Quick Sort (below), we get
2834-
// a ~3500ms mean speed-up in `bench/bench.html`.
2835-
2836-
/**
2837-
* Swap the elements indexed by `x` and `y` in the array `ary`.
2838-
*
2839-
* @param {Array} ary
2840-
* The array.
2841-
* @param {Number} x
2842-
* The index of the first item.
2843-
* @param {Number} y
2844-
* The index of the second item.
2845-
*/
2846-
function swap(ary, x, y) {
2847-
var temp = ary[x];
2848-
ary[x] = ary[y];
2849-
ary[y] = temp;
2850-
}
2851-
2852-
/**
2853-
* Returns a random integer within the range `low .. high` inclusive.
2854-
*
2855-
* @param {Number} low
2856-
* The lower bound on the range.
2857-
* @param {Number} high
2858-
* The upper bound on the range.
2859-
*/
2860-
function randomIntInRange(low, high) {
2861-
return Math.round(low + (Math.random() * (high - low)));
2862-
}
2863-
2864-
/**
2865-
* The Quick Sort algorithm.
2866-
*
2867-
* @param {Array} ary
2868-
* An array to sort.
2869-
* @param {function} comparator
2870-
* Function to use to compare two items.
2871-
* @param {Number} p
2872-
* Start index of the array
2873-
* @param {Number} r
2874-
* End index of the array
2875-
*/
2876-
function doQuickSort(ary, comparator, p, r) {
2877-
// If our lower bound is less than our upper bound, we (1) partition the
2878-
// array into two pieces and (2) recurse on each half. If it is not, this is
2879-
// the empty array and our base case.
2880-
2881-
if (p < r) {
2882-
// (1) Partitioning.
2883-
//
2884-
// The partitioning chooses a pivot between `p` and `r` and moves all
2885-
// elements that are less than or equal to the pivot to the before it, and
2886-
// all the elements that are greater than it after it. The effect is that
2887-
// once partition is done, the pivot is in the exact place it will be when
2888-
// the array is put in sorted order, and it will not need to be moved
2889-
// again. This runs in O(n) time.
2890-
2891-
// Always choose a random pivot so that an input array which is reverse
2892-
// sorted does not cause O(n^2) running time.
2893-
var pivotIndex = randomIntInRange(p, r);
2894-
var i = p - 1;
2895-
2896-
swap(ary, pivotIndex, r);
2897-
var pivot = ary[r];
2898-
2899-
// Immediately after `j` is incremented in this loop, the following hold
2900-
// true:
2901-
//
2902-
// * Every element in `ary[p .. i]` is less than or equal to the pivot.
2903-
//
2904-
// * Every element in `ary[i+1 .. j-1]` is greater than the pivot.
2905-
for (var j = p; j < r; j++) {
2906-
if (comparator(ary[j], pivot) <= 0) {
2907-
i += 1;
2908-
swap(ary, i, j);
2909-
}
2910-
}
2911-
2912-
swap(ary, i + 1, j);
2913-
var q = i + 1;
2914-
2915-
// (2) Recurse on each half.
2916-
2917-
doQuickSort(ary, comparator, p, q - 1);
2918-
doQuickSort(ary, comparator, q + 1, r);
2919-
}
2920-
}
2921-
2922-
/**
2923-
* Sort the given array in-place with the given comparator function.
2924-
*
2925-
* @param {Array} ary
2926-
* An array to sort.
2927-
* @param {function} comparator
2928-
* Function to use to compare two items.
2929-
*/
2930-
exports.quickSort = function (ary, comparator) {
2931-
doQuickSort(ary, comparator, 0, ary.length - 1);
2932-
};
2933-
2934-
2935-
/***/ }),
2936-
/* 10 */
29372816
/***/ (function(module, exports, __webpack_require__) {
29382817

29392818
/* WEBPACK VAR INJECTION */(function(__dirname) {if (typeof fetch === "function") {
@@ -2955,8 +2834,8 @@ return /******/ (function(modules) { // webpackBootstrap
29552834
module.exports.initialize = url => mappingsWasmUrl = url;
29562835
} else {
29572836
// Node version of reading a wasm file into an array buffer.
2958-
const fs = __webpack_require__(11);
2959-
const path = __webpack_require__(12);
2837+
const fs = __webpack_require__(10);
2838+
const path = __webpack_require__(11);
29602839

29612840
module.exports = function readWasm() {
29622841
return new Promise((resolve, reject) => {
@@ -2980,22 +2859,22 @@ return /******/ (function(modules) { // webpackBootstrap
29802859
/* WEBPACK VAR INJECTION */}.call(exports, "/"))
29812860

29822861
/***/ }),
2983-
/* 11 */
2862+
/* 10 */
29842863
/***/ (function(module, exports) {
29852864

2986-
module.exports = __WEBPACK_EXTERNAL_MODULE_11__;
2865+
module.exports = __WEBPACK_EXTERNAL_MODULE_10__;
29872866

29882867
/***/ }),
2989-
/* 12 */
2868+
/* 11 */
29902869
/***/ (function(module, exports) {
29912870

2992-
module.exports = __WEBPACK_EXTERNAL_MODULE_12__;
2871+
module.exports = __WEBPACK_EXTERNAL_MODULE_11__;
29932872

29942873
/***/ }),
2995-
/* 13 */
2874+
/* 12 */
29962875
/***/ (function(module, exports, __webpack_require__) {
29972876

2998-
const readWasm = __webpack_require__(10);
2877+
const readWasm = __webpack_require__(9);
29992878

30002879
/**
30012880
* Provide the JIT with a nice shape / hidden class.
@@ -3105,7 +2984,7 @@ return /******/ (function(modules) { // webpackBootstrap
31052984

31062985

31072986
/***/ }),
3108-
/* 14 */
2987+
/* 13 */
31092988
/***/ (function(module, exports, __webpack_require__) {
31102989

31112990
/* -*- Mode: js; js-indent-level: 2; -*- */

lib/quick-sort.js

Lines changed: 0 additions & 114 deletions
This file was deleted.

lib/source-map-consumer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var util = require('./util');
99
var binarySearch = require('./binary-search');
1010
var ArraySet = require('./array-set').ArraySet;
1111
var base64VLQ = require('./base64-vlq');
12-
var quickSort = require('./quick-sort').quickSort;
1312
var readWasm = require('../lib/read-wasm');
1413
var wasm = require('./wasm');
1514

@@ -910,14 +909,14 @@ Object.defineProperty(IndexedSourceMapConsumer.prototype, '_originalMappingsUnso
910909
IndexedSourceMapConsumer.prototype._sortGeneratedMappings =
911910
function IndexedSourceMapConsumer_sortGeneratedMappings() {
912911
const mappings = this._generatedMappingsUnsorted;
913-
quickSort(mappings, util.compareByGeneratedPositionsDeflated);
912+
mappings.sort(util.compareByGeneratedPositionsDeflated);
914913
this.__generatedMappings = mappings;
915914
};
916915

917916
IndexedSourceMapConsumer.prototype._sortOriginalMappings =
918917
function IndexedSourceMapConsumer_sortOriginalMappings() {
919918
const mappings = this._originalMappingsUnsorted;
920-
quickSort(mappings, util.compareByOriginalPositions);
919+
mappings.sort(util.compareByOriginalPositions);
921920
this.__originalMappings = mappings;
922921
};
923922

0 commit comments

Comments
 (0)