splice-buffer.js 7.8 KB

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