Debian Codegraph @

lib/dispatcher/fixed-queue.js

raw
1 'use strict'
2
3 // Extracted from node/lib/internal/fixed_queue.js
4
5 // Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
6 const kSize = 2048
7 const kMask = kSize - 1
8
9 // The FixedQueue is implemented as a singly-linked list of fixed-size
10 // circular buffers. It looks something like this:
11 //
12 // head tail
13 // | |
14 // v v
15 // +-----------+ <-----\ +-----------+ <------\ +-----------+
16 // | [null] | \----- | next | \------- | next |
17 // +-----------+ +-----------+ +-----------+
18 // | item | <-- bottom | item | <-- bottom | undefined |
19 // | item | | item | | undefined |
20 // | item | | item | | undefined |
21 // | item | | item | | undefined |
22 // | item | | item | bottom --> | item |
23 // | item | | item | | item |
24 // | ... | | ... | | ... |
25 // | item | | item | | item |
26 // | item | | item | | item |
27 // | undefined | <-- top | item | | item |
28 // | undefined | | item | | item |
29 // | undefined | | undefined | <-- top top --> | undefined |
30 // +-----------+ +-----------+ +-----------+
31 //
32 // Or, if there is only one circular buffer, it looks something
33 // like either of these:
34 //
35 // head tail head tail
36 // | | | |
37 // v v v v
38 // +-----------+ +-----------+
39 // | [null] | | [null] |
40 // +-----------+ +-----------+
41 // | undefined | | item |
42 // | undefined | | item |
43 // | item | <-- bottom top --> | undefined |
44 // | item | | undefined |
45 // | undefined | <-- top bottom --> | item |
46 // | undefined | | item |
47 // +-----------+ +-----------+
48 //
49 // Adding a value means moving `top` forward by one, removing means
50 // moving `bottom` forward by one. After reaching the end, the queue
51 // wraps around.
52 //
53 // When `top === bottom` the current queue is empty and when
54 // `top + 1 === bottom` it's full. This wastes a single space of storage
55 // but allows much quicker checks.
56
57 /**
58 * @type {FixedCircularBuffer}
59 * @template T
60 */
61 class FixedCircularBuffer {
62 /** @type {number} */
63 bottom = 0
64 /** @type {number} */
65 top = 0
66 /** @type {Array<T|undefined>} */
67 list = new Array(kSize).fill(undefined)
68 /** @type {T|null} */
69 next = null
70
71 /** @returns {boolean} */
72 isEmpty () {
73 return this.top === this.bottom
74 }
75
76 /** @returns {boolean} */
77 isFull () {
78 return ((this.top + 1) & kMask) === this.bottom
79 }
80
81 /**
82 * @param {T} data
83 * @returns {void}
84 */
85 push (data) {
86 this.list[this.top] = data
87 this.top = (this.top + 1) & kMask
88 }
89
90 /** @returns {T|null} */
91 shift () {
92 const nextItem = this.list[this.bottom]
93 if (nextItem === undefined) { return null }
94 this.list[this.bottom] = undefined
95 this.bottom = (this.bottom + 1) & kMask
96 return nextItem
97 }
98 }
99
100 /**
101 * @template T
102 */
103 module.exports = class FixedQueue {
104 constructor () {
105 /** @type {FixedCircularBuffer<T>} */
106 this.head = this.tail = new FixedCircularBuffer()
107 }
108
109 /** @returns {boolean} */
110 isEmpty () {
111 return this.head.isEmpty()
112 }
113
114 /** @param {T} data */
115 push (data) {
116 if (this.head.isFull()) {
117 // Head is full: Creates a new queue, sets the old queue's `.next` to it,
118 // and sets it as the new main queue.
119 this.head = this.head.next = new FixedCircularBuffer()
120 }
121 this.head.push(data)
122 }
123
124 /** @returns {T|null} */
125 shift () {
126 const tail = this.tail
127 const next = tail.shift()
128 if (tail.isEmpty() && tail.next !== null) {
129 // If there is another queue, it forms the new tail.
130 this.tail = tail.next
131 tail.next = null
132 }
133 return next
134 }
135 }