Skip to content

Commit c4ec62b

Browse files
committed
perf: add adaptive aging factor to priority queue
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent e926e5d commit c4ec62b

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

deno.json

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"version": "0.5.12",
44
"exports": "./src/mod.ts",
55
"compilerOptions": {
6-
"lib": [
7-
"deno.worker"
8-
],
6+
"lib": ["deno.worker"],
97
"strict": true
108
},
119
"tasks": {
@@ -27,9 +25,7 @@
2725
"documentation": "deno doc ./src/mod.ts"
2826
},
2927
"test": {
30-
"include": [
31-
"./tests/**/*.test.mjs"
32-
]
28+
"include": ["./tests/**/*.test.mjs"]
3329
},
3430
"fmt": {
3531
"semiColons": false,
@@ -42,18 +38,8 @@
4238
"@std/testing": "jsr:@std/testing@^1.0.15"
4339
},
4440
"publish": {
45-
"include": [
46-
"LICENSE",
47-
"README.md",
48-
"deno.json",
49-
"src/**/*.ts"
50-
]
41+
"include": ["LICENSE", "README.md", "deno.json", "src/**/*.ts"]
5142
},
5243
"lock": false,
53-
"exclude": [
54-
"./coverage",
55-
"./dist/browser",
56-
"./dist/esm",
57-
"./npm"
58-
]
44+
"exclude": ["./coverage", "./dist/browser", "./dist/esm", "./npm"]
5945
}

src/queues/fixed-priority-queue.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@ import type { IFixedQueue } from './queue-types.ts'
1010
export class FixedPriorityQueue<T> extends AbstractFixedQueue<T>
1111
implements IFixedQueue<T> {
1212
private readonly agingFactor: number
13+
private readonly loadExponent: number
1314

1415
/**
1516
* Constructs a FixedPriorityQueue.
1617
* @param size - Fixed queue size. @defaultValue defaultQueueSize
1718
* @param agingFactor - Aging factor to apply to items in priority points per millisecond. A higher value makes items age faster.
1819
* @returns IFixedQueue.
1920
*/
20-
public constructor(size?: number, agingFactor = 0.001) {
21+
public constructor(
22+
size?: number,
23+
agingFactor = 0.001,
24+
loadExponent = 1.0 / 1.5,
25+
) {
2126
super(size)
2227
this.agingFactor = agingFactor
28+
this.loadExponent = loadExponent
2329
}
2430

2531
/** @inheritdoc */
@@ -29,12 +35,14 @@ export class FixedPriorityQueue<T> extends AbstractFixedQueue<T>
2935
}
3036
priority = priority ?? 0
3137
const now = performance.now()
38+
const effectiveAgingFactor = this.agingFactor *
39+
(1 + ((this.size + 1) / this.capacity) ** this.loadExponent)
3240
let insertionPhysicalIndex = -1
3341
let currentPhysicalIndex = this.start
3442
for (let i = 0; i < this.size; i++) {
3543
const node = this.nodeArray[currentPhysicalIndex]!
3644
const nodeEffectivePriority = node.priority -
37-
(now - node.timestamp) * this.agingFactor
45+
(now - node.timestamp) * effectiveAgingFactor
3846
if (nodeEffectivePriority > priority) {
3947
insertionPhysicalIndex = currentPhysicalIndex
4048
break

0 commit comments

Comments
 (0)