ParallelismFactorCalculator.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const binarySearchBounds = require("./binarySearchBounds");
  7. /** @typedef {(value: number) => void} Callback */
  8. class ParallelismFactorCalculator {
  9. constructor() {
  10. /** @type {number[]} */
  11. this._rangePoints = [];
  12. /** @type {Callback[]} */
  13. this._rangeCallbacks = [];
  14. }
  15. /**
  16. * Processes the provided start.
  17. * @param {number} start range start
  18. * @param {number} end range end
  19. * @param {Callback} callback callback
  20. * @returns {void}
  21. */
  22. range(start, end, callback) {
  23. if (start === end) return callback(1);
  24. this._rangePoints.push(start);
  25. this._rangePoints.push(end);
  26. this._rangeCallbacks.push(callback);
  27. }
  28. calculate() {
  29. const segments = [...new Set(this._rangePoints)].sort((a, b) =>
  30. a < b ? -1 : 1
  31. );
  32. const parallelism = segments.map(() => 0);
  33. /** @type {number[]} */
  34. const rangeStartIndices = [];
  35. for (let i = 0; i < this._rangePoints.length; i += 2) {
  36. const start = this._rangePoints[i];
  37. const end = this._rangePoints[i + 1];
  38. let idx = binarySearchBounds.eq(segments, start);
  39. rangeStartIndices.push(idx);
  40. do {
  41. parallelism[idx]++;
  42. idx++;
  43. } while (segments[idx] < end);
  44. }
  45. for (let i = 0; i < this._rangeCallbacks.length; i++) {
  46. const start = this._rangePoints[i * 2];
  47. const end = this._rangePoints[i * 2 + 1];
  48. let idx = rangeStartIndices[i];
  49. let sum = 0;
  50. let totalDuration = 0;
  51. let current = start;
  52. do {
  53. const p = parallelism[idx];
  54. idx++;
  55. const duration = segments[idx] - current;
  56. totalDuration += duration;
  57. current = segments[idx];
  58. sum += p * duration;
  59. } while (current < end);
  60. this._rangeCallbacks[i](sum / totalDuration);
  61. }
  62. }
  63. }
  64. module.exports = ParallelismFactorCalculator;