splice-buffer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Some of the internal operations of micromark do lots of editing
  3. * operations on very large arrays. This runs into problems with two
  4. * properties of most circa-2020 JavaScript interpreters:
  5. *
  6. * - Array-length modifications at the high end of an array (push/pop) are
  7. * expected to be common and are implemented in (amortized) time
  8. * proportional to the number of elements added or removed, whereas
  9. * other operations (shift/unshift and splice) are much less efficient.
  10. * - Function arguments are passed on the stack, so adding tens of thousands
  11. * of elements to an array with `arr.push(...newElements)` will frequently
  12. * cause stack overflows. (see <https://stackoverflow.com/questions/22123769/rangeerror-maximum-call-stack-size-exceeded-why>)
  13. *
  14. * SpliceBuffers are an implementation of gap buffers, which are a
  15. * generalization of the "queue made of two stacks" idea. The splice buffer
  16. * maintains a cursor, and moving the cursor has cost proportional to the
  17. * distance the cursor moves, but inserting, deleting, or splicing in
  18. * new information at the cursor is as efficient as the push/pop operation.
  19. * This allows for an efficient sequence of splices (or pushes, pops, shifts,
  20. * or unshifts) as long such edits happen at the same part of the array or
  21. * generally sweep through the array from the beginning to the end.
  22. *
  23. * The interface for splice buffers also supports large numbers of inputs by
  24. * passing a single array argument rather passing multiple arguments on the
  25. * function call stack.
  26. *
  27. * @template T
  28. * Item type.
  29. */
  30. export class SpliceBuffer {
  31. /**
  32. * @param {ReadonlyArray<T> | null | undefined} [initial]
  33. * Initial items (optional).
  34. * @returns
  35. * Splice buffer.
  36. */
  37. constructor(initial) {
  38. /** @type {Array<T>} */
  39. this.left = initial ? [...initial] : [];
  40. /** @type {Array<T>} */
  41. this.right = [];
  42. }
  43. /**
  44. * Array access;
  45. * does not move the cursor.
  46. *
  47. * @param {number} index
  48. * Index.
  49. * @return {T}
  50. * Item.
  51. */
  52. get(index) {
  53. if (index < 0 || index >= this.left.length + this.right.length) {
  54. throw new RangeError('Cannot access index `' + index + '` in a splice buffer of size `' + (this.left.length + this.right.length) + '`');
  55. }
  56. if (index < this.left.length) return this.left[index];
  57. return this.right[this.right.length - index + this.left.length - 1];
  58. }
  59. /**
  60. * The length of the splice buffer, one greater than the largest index in the
  61. * array.
  62. */
  63. get length() {
  64. return this.left.length + this.right.length;
  65. }
  66. /**
  67. * Remove and return `list[0]`;
  68. * moves the cursor to `0`.
  69. *
  70. * @returns {T | undefined}
  71. * Item, optional.
  72. */
  73. shift() {
  74. this.setCursor(0);
  75. return this.right.pop();
  76. }
  77. /**
  78. * Slice the buffer to get an array;
  79. * does not move the cursor.
  80. *
  81. * @param {number} start
  82. * Start.
  83. * @param {number | null | undefined} [end]
  84. * End (optional).
  85. * @returns {Array<T>}
  86. * Array of items.
  87. */
  88. slice(start, end) {
  89. /** @type {number} */
  90. const stop = end === null || end === undefined ? Number.POSITIVE_INFINITY : end;
  91. if (stop < this.left.length) {
  92. return this.left.slice(start, stop);
  93. }
  94. if (start > this.left.length) {
  95. return this.right.slice(this.right.length - stop + this.left.length, this.right.length - start + this.left.length).reverse();
  96. }
  97. return this.left.slice(start).concat(this.right.slice(this.right.length - stop + this.left.length).reverse());
  98. }
  99. /**
  100. * Mimics the behavior of Array.prototype.splice() except for the change of
  101. * interface necessary to avoid segfaults when patching in very large arrays.
  102. *
  103. * This operation moves cursor is moved to `start` and results in the cursor
  104. * placed after any inserted items.
  105. *
  106. * @param {number} start
  107. * Start;
  108. * zero-based index at which to start changing the array;
  109. * negative numbers count backwards from the end of the array and values
  110. * that are out-of bounds are clamped to the appropriate end of the array.
  111. * @param {number | null | undefined} [deleteCount=0]
  112. * Delete count (default: `0`);
  113. * maximum number of elements to delete, starting from start.
  114. * @param {Array<T> | null | undefined} [items=[]]
  115. * Items to include in place of the deleted items (default: `[]`).
  116. * @return {Array<T>}
  117. * Any removed items.
  118. */
  119. splice(start, deleteCount, items) {
  120. /** @type {number} */
  121. const count = deleteCount || 0;
  122. this.setCursor(Math.trunc(start));
  123. const removed = this.right.splice(this.right.length - count, Number.POSITIVE_INFINITY);
  124. if (items) chunkedPush(this.left, items);
  125. return removed.reverse();
  126. }
  127. /**
  128. * Remove and return the highest-numbered item in the array, so
  129. * `list[list.length - 1]`;
  130. * Moves the cursor to `length`.
  131. *
  132. * @returns {T | undefined}
  133. * Item, optional.
  134. */
  135. pop() {
  136. this.setCursor(Number.POSITIVE_INFINITY);
  137. return this.left.pop();
  138. }
  139. /**
  140. * Inserts a single item to the high-numbered side of the array;
  141. * moves the cursor to `length`.
  142. *
  143. * @param {T} item
  144. * Item.
  145. * @returns {undefined}
  146. * Nothing.
  147. */
  148. push(item) {
  149. this.setCursor(Number.POSITIVE_INFINITY);
  150. this.left.push(item);
  151. }
  152. /**
  153. * Inserts many items to the high-numbered side of the array.
  154. * Moves the cursor to `length`.
  155. *
  156. * @param {Array<T>} items
  157. * Items.
  158. * @returns {undefined}
  159. * Nothing.
  160. */
  161. pushMany(items) {
  162. this.setCursor(Number.POSITIVE_INFINITY);
  163. chunkedPush(this.left, items);
  164. }
  165. /**
  166. * Inserts a single item to the low-numbered side of the array;
  167. * Moves the cursor to `0`.
  168. *
  169. * @param {T} item
  170. * Item.
  171. * @returns {undefined}
  172. * Nothing.
  173. */
  174. unshift(item) {
  175. this.setCursor(0);
  176. this.right.push(item);
  177. }
  178. /**
  179. * Inserts many items to the low-numbered side of the array;
  180. * moves the cursor to `0`.
  181. *
  182. * @param {Array<T>} items
  183. * Items.
  184. * @returns {undefined}
  185. * Nothing.
  186. */
  187. unshiftMany(items) {
  188. this.setCursor(0);
  189. chunkedPush(this.right, items.reverse());
  190. }
  191. /**
  192. * Move the cursor to a specific position in the array. Requires
  193. * time proportional to the distance moved.
  194. *
  195. * If `n < 0`, the cursor will end up at the beginning.
  196. * If `n > length`, the cursor will end up at the end.
  197. *
  198. * @param {number} n
  199. * Position.
  200. * @return {undefined}
  201. * Nothing.
  202. */
  203. setCursor(n) {
  204. if (n === this.left.length || n > this.left.length && this.right.length === 0 || n < 0 && this.left.length === 0) return;
  205. if (n < this.left.length) {
  206. // Move cursor to the this.left
  207. const removed = this.left.splice(n, Number.POSITIVE_INFINITY);
  208. chunkedPush(this.right, removed.reverse());
  209. } else {
  210. // Move cursor to the this.right
  211. const removed = this.right.splice(this.left.length + this.right.length - n, Number.POSITIVE_INFINITY);
  212. chunkedPush(this.left, removed.reverse());
  213. }
  214. }
  215. }
  216. /**
  217. * Avoid stack overflow by pushing items onto the stack in segments
  218. *
  219. * @template T
  220. * Item type.
  221. * @param {Array<T>} list
  222. * List to inject into.
  223. * @param {ReadonlyArray<T>} right
  224. * Items to inject.
  225. * @return {undefined}
  226. * Nothing.
  227. */
  228. function chunkedPush(list, right) {
  229. /** @type {number} */
  230. let chunkStart = 0;
  231. if (right.length < 10000) {
  232. list.push(...right);
  233. } else {
  234. while (chunkStart < right.length) {
  235. list.push(...right.slice(chunkStart, chunkStart + 10000));
  236. chunkStart += 10000;
  237. }
  238. }
  239. }