Skip to content

Commit 45e0efd

Browse files
committed
Bump version to v1.1.0: Add getSnapshot method and improve documentation/README
1 parent 42db262 commit 45e0efd

10 files changed

+389
-255
lines changed

README.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
The `SlimQueue` class implements an in-memory queue with a basic API, targeting pure FIFO use cases such as task queues, breadth-first search (BFS), and similar scenarios.
44

5+
This versatile data structure is commonly associated with task prioritization, ensuring tasks are processed in order. It also proves valuable in optimizing sliding-window algorithms, where the oldest item (First In) is evicted based on a specific indicator.
6+
57
## Data-Oriented Design :gear:
68

79
This implementation follows the principles of Data-Oriented Design (DOD), optimizing memory layout and access patterns using arrays, particularly to enhance CPU cache efficiency. Unlike Object-Oriented Programming (OOP), where each object may be allocated in disparate locations on the heap, DOD leverages the sequential allocation of arrays, reducing the likelihood of cache misses.
810

911
## Focused API :dart:
1012

11-
This package provides a queue and nothing more. The absence of linear operations like iteration and splicing reflects a deliberate design choice, as resorting to such methods often indicates that a queue may not have been the most appropriate data structure in the first place.
13+
This package provides a queue and nothing more. The absence of linear operations like iteration and splicing reflects a deliberate design choice, as resorting to such methods often indicates that a queue may not have been the most appropriate data structure in the first place.
14+
The sole exception is the `getSnapshot` method, included to support metrics or statistical analysis, particularly in use cases like tracking the K most recent items.
1215

1316
## Table of Contents :bookmark_tabs:
1417

@@ -21,10 +24,10 @@ This package provides a queue and nothing more. The absence of linear operations
2124

2225
## Key Features :sparkles:<a id="key-features"></a>
2326

24-
- __Basic Queue API__: Straightforward API, targeting pure use-cases of queues.
27+
- __Basic Queue API__: Targeting pure use-cases of queues.
2528
- __Efficiency :gear:__: Featuring a Data-Oriented Design with capacity-tuning capability, to reduce or prevent reallocations of the internal cyclic buffer.
2629
- __Comprehensive Documentation :books:__: The class is thoroughly documented, enabling IDEs to provide helpful tooltips that enhance the coding experience.
27-
- __Tests :test_tube:__: **Fully covered** by comprehensive unit tests, including validations to ensure that internal capacity increases as expected.
30+
- __Tests :test_tube:__: **Fully covered** by comprehensive unit tests, including **randomized simulations** of real-life scenarios and validations to ensure proper internal capacity scaling.
2831
- **TypeScript** support.
2932
- No external runtime dependencies: Only development dependencies are used.
3033
- ES2020 Compatibility: The `tsconfig` target is set to ES2020, ensuring compatibility with ES2020 environments.
@@ -33,9 +36,10 @@ This package provides a queue and nothing more. The absence of linear operations
3336

3437
The `SlimQueue` class provides the following methods:
3538

36-
* __push__: Appends the item to the end of the queue as the "Last In" item. As a result, the queue's size increases by one.
37-
* __pop__: Returns the oldest item currently stored in the queue and removes it. As a result, the queue's size decreases by one.
38-
* __clear__: Removes all items from the current queue instance, leaving it empty.
39+
* __push__: Appends an item to the end of the queue (i.e., the Last In), increasing its size by one.
40+
* __pop__: Removes and returns the oldest (First In) item from the queue, decreasing its size by one.
41+
* __clear__: Removes all items from the queue, leaving it empty.
42+
* __getSnapshot__: Returns an array of references to all the currently stored items in the queue, ordered from First-In to Last-In.
3943

4044
If needed, refer to the code documentation for a more comprehensive description.
4145

@@ -47,15 +51,16 @@ The `SlimQueue` class provides the following getter methods to reflect the curre
4751

4852
* __size__: The amount of items currently stored in the queue.
4953
* __isEmpty__: Indicates whether the queue does not contain any item.
54+
* __firstIn__: Returns a reference to the oldest item currently stored in the queue (the First In item), which will be removed during the next pop operation.
5055
* __capacity__: The length of the internal buffer storing items. If the observed capacity remains significantly larger than the queue's size after the initial warm-up period, it may indicate that the initial capacity was overestimated. Conversely, if the capacity has grown excessively due to buffer reallocations, it may suggest that the initial capacity was underestimated.
5156
* __numberOfCapacityIncrements__: The number of internal buffer reallocations due to insufficient capacity that have occurred during the instance's lifespan. A high number of capacity increments suggests that the initial capacity was underestimated.
52-
* __firstIn__: The oldest item currently stored in the queue, i.e., the "First In" item, which will be removed during the next pop operation.
5357

5458
To eliminate any ambiguity, all getter methods have **O(1)** time and space complexity.
5559

5660
## Use Case Example: Rate Limiting :man_technologist:<a id="use-case-example"></a>
5761

58-
Consider a component designed for rate-limiting promises using a sliding-window approach. Suppose a window duration of `windowDurationMs` milliseconds, with a maximum of `tasksPerWindow` tasks allowed within each window. The rate limiter will only trigger the execution of a task if fewer than `tasksPerWindow` tasks have started execution within the time window `[now - windowDurationMs, now]`.
62+
Consider a component designed for rate-limiting promises using a sliding-window approach. Suppose a window duration of `windowDurationMs` milliseconds, with a maximum of `tasksPerWindow` tasks allowed within each window. The rate limiter will only trigger the execution of a task if fewer than `tasksPerWindow` tasks have started execution within the time window `[now - windowDurationMs, now]`.
63+
5964
For simplicity, this example focuses on a single method that initiates task execution only if the current window's limit has not been reached. If the limit has been exceeded, an error is thrown.
6065
In this scenario, we employ the `isEmpty`, `firstIn`, and `size` getters, along with the `push` and `pop` methods.
6166

@@ -82,20 +87,27 @@ class RateLimiter<T> {
8287

8388
public async tryExecutingTask(task: RateLimiterTask<T>): Promise<T> {
8489
// Evict out-of-window past execution timestamps.
85-
const absoluteNow: number = Date.now();
86-
while (
87-
!this._ascWindowTimestamps.isEmpty &&
88-
(absoluteNow - this._ascWindowTimestamps.firstIn) >= this._windowDurationMs
89-
) {
90+
const now: number = Date.now();
91+
while (this._isOutdatedFirstIn(now)) {
9092
this._ascWindowTimestamps.pop();
9193
}
9294

9395
if (this._ascWindowTimestamps.size === this._tasksPerWindow) {
9496
throw new RateLimiterThrottlingError();
9597
}
9698

97-
this._ascWindowTimestamps.push(absoluteNow);
98-
return await task();
99+
this._ascWindowTimestamps.push(now);
100+
return task();
101+
}
102+
103+
// Eviction indicator.
104+
private _isOutdatedFirstIn(now: number): boolean {
105+
if (this._ascWindowTimestamps.isEmpty) {
106+
return false;
107+
}
108+
109+
const elapsedMsSinceOldestTimestamp = now - this._ascWindowTimestamps.firstIn;
110+
return elapsedMsSinceOldestTimestamp >= this._windowDurationMs;
99111
}
100112
}
101113
```

dist/data-oriented-slim-queue.d.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ export declare const DEFAULT_SLIM_QUEUE_CAPACITY_INCREMENT_FACTOR = 1.5;
1818
/**
1919
* SlimQueue
2020
*
21-
* The `SlimQueue` class implements an in-memory queue with a basic API, targeting pure FIFO use cases
22-
* like task queues, breadth-first search (BFS), and similar scenarios.
21+
* The `SlimQueue` class implements an in-memory queue with a basic API, targeting pure FIFO use
22+
* cases like task queues, breadth-first search (BFS), and similar scenarios.
2323
*
2424
* ### Data-Oriented Design
25-
* This implementation follows the principles of Data-Oriented Design (DOD), optimizing memory layout and
26-
* access patterns using arrays, particularly to enhance CPU cache efficiency. Unlike Object-Oriented Programming
27-
* (OOP), where each object may be allocated in disparate locations on the heap, DOD leverages the sequential
28-
* allocation of arrays, reducing the likelihood of cache misses.
25+
* This implementation follows the principles of Data-Oriented Design (DOD), optimizing memory layout
26+
* and access patterns using arrays, particularly to enhance CPU cache efficiency.
27+
* Unlike Object-Oriented Programming (OOP), where each object may be allocated in disparate locations
28+
* on the heap, DOD leverages the sequential allocation of arrays, reducing the likelihood of cache misses.
2929
*
3030
* ### Focused API
31-
* This package provides a queue and nothing more. The absence of linear operations like iteration and splicing
32-
* reflects a deliberate design choice, as resorting to such methods often indicates that a queue may not have
33-
* been the most appropriate data structure in the first place.
31+
* This package provides a queue and nothing more. The absence of linear operations like iteration
32+
* and splicing reflects a deliberate design choice, as resorting to such methods often indicates that
33+
* a queue may not have been the most appropriate data structure in the first place.
3434
*
3535
* ### Terminology
3636
* The 'push' and 'pop' terminology is inspired by std::queue in C++.
37-
* Unlike more complex data structures, a queue only allows pushing in one direction and popping from the other,
38-
* making this straightforward terminology appropriate.
37+
* Unlike more complex data structures, a queue only allows pushing in one direction and popping from the
38+
* other, making this straightforward terminology appropriate.
3939
* The `firstIn` getter provides access to the next item to be removed. This is useful in scenarios where
4040
* items are removed based on a specific condition. For example, in a Rate Limiter that restricts the number
4141
* of requests within a time window, an ascending queue of timestamps might represent request times. To determine
@@ -64,7 +64,7 @@ export declare class SlimQueue<T> {
6464
* will be allocated, and all existing items will be transferred to this new
6565
* buffer. The size of the new buffer will be `oldBufferSize * capacityIncrementFactor`.
6666
* For example, if the initial capacity is 100 and the increment factor is 2,
67-
* the queue will allocate a new buffer of 200 slots before adding the 101st item.
67+
* the queue will allocate a new buffer of 200 slots before adding the 101th item.
6868
*
6969
* ### Considerations
7070
* A small initial capacity may lead to frequent dynamic memory reallocations,
@@ -82,7 +82,7 @@ export declare class SlimQueue<T> {
8282
/**
8383
* size
8484
*
85-
* @returns The amount of items currently stored in the queue.
85+
* @returns The number of items currently stored in the queue.
8686
*/
8787
get size(): number;
8888
/**
@@ -94,11 +94,11 @@ export declare class SlimQueue<T> {
9494
/**
9595
* capacity
9696
*
97-
* The `capacity` getter is useful for metrics and monitoring. If the observed capacity
98-
* remains significantly larger than the queue's size after the initial warm-up period,
99-
* it may indicate that the initial capacity was overestimated. Conversely, if the capacity
100-
* has grown excessively due to buffer reallocations, it may suggest that the initial
101-
* capacity was underestimated.
97+
* The `capacity` getter is useful for metrics and monitoring.
98+
* If the observed capacity remains significantly larger than the queue's size after the
99+
* initial warm-up period, it may indicate that the initial capacity was overestimated.
100+
* Conversely, if the capacity has grown excessively due to buffer reallocations, it may
101+
* suggest that the initial capacity was underestimated.
102102
*
103103
* @returns The length of the internal buffer storing items.
104104
*/
@@ -115,37 +115,62 @@ export declare class SlimQueue<T> {
115115
/**
116116
* firstIn
117117
*
118-
* @returns The oldest item currently stored in the queue, i.e., the "First In" item,
119-
* which will be removed during the next pop operation.
118+
* Returns a reference to the oldest item currently stored in the queue.
119+
*
120+
* Commonly used in scenarios like sliding-window algorithms, where items
121+
* are conditionally removed based on an out-of-window indicator.
122+
*
123+
* @returns The "First In" item, i.e., the oldest item in the queue,
124+
* which will be removed during the next `pop` operation.
120125
*/
121126
get firstIn(): T;
122127
/**
123128
* push
124129
*
125-
* This method appends the item to the end of the queue as the "Last In" item.
126-
* As a result, the queue's size increases by one.
130+
* Appends an item to the end of the queue (i.e., the Last In), increasing its size by one.
127131
*
128-
* @param item The item to be added as the Last In, i.e., the newest item in the queue.
129-
* It will be removed by the pop method only after all the existing items
132+
* @param item The item to add as the newest entry in the queue (i.e., the Last In).
133+
* It will be removed by the `pop` method only after all existing items
130134
* have been removed.
131135
*/
132136
push(item: T): void;
133137
/**
134138
* pop
135139
*
136-
* This method returns the oldest item currently stored in the queue and removes it.
137-
* As a result, the queue's size decreases by one.
140+
* Removes and returns the oldest (First In) item from the queue, decreasing its size by one.
138141
*
139-
* @returns The oldest item currently stored in the queue, i.e., the "First In" item.
142+
* @returns The item that was removed from the queue (the First In item).
140143
*/
141144
pop(): T;
142145
/**
143146
* clear
144147
*
145-
* This method removes all items from the current queue instance, leaving it empty.
148+
* Removes all items from the queue, leaving it empty.
146149
*/
147150
clear(): void;
151+
/**
152+
* getSnapshot
153+
*
154+
* Returns an array of references to all the currently stored items in the queue,
155+
* ordered from First-In to Last-In.
156+
*
157+
* This method can be used, for example, to periodically log the K most recent metrics,
158+
* such as CPU or memory usage.
159+
*
160+
* @returns An array of references to the queue's items, ordered from First-In to Last-In.
161+
*/
162+
getSnapshot(): T[];
148163
private _increaseCapacityIfNecessary;
149164
private _calculateExclusiveTailIndex;
150165
private _validateCapacityIncrementFactor;
166+
/**
167+
* _traverseInOrder
168+
*
169+
* Facilitates traversing the items in the queue in order, from first-in to last-in.
170+
* The method accepts a callback, which is invoked with the current item index in
171+
* the internal cyclic buffer.
172+
*
173+
* @param callback The callback to execute, provided with the current item index.
174+
*/
175+
private _traverseInOrder;
151176
}

0 commit comments

Comments
 (0)