Skip to content

Commit

Permalink
perf(cancellations): use an Array instead of a linked list
Browse files Browse the repository at this point in the history
Arrays have lower memory usage
  • Loading branch information
tusharmath committed May 16, 2019
1 parent 0aa708f commit 782cf80
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Fastest is FIO

```bash
node benchmarks/NestedMap.js
Fluture x 8,518 ops/sec ±1.68% (77 runs sampled)
FIO x 4,810 ops/sec ±1.56% (78 runs sampled)
Fluture x 8,280 ops/sec ±3.43% (73 runs sampled)
FIO x 5,838 ops/sec ±1.03% (80 runs sampled)
Fastest is Fluture
```
26 changes: 16 additions & 10 deletions src/cancellables/CancellationList.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
/**
* Created by tushar on 2019-05-14
*/
import {LinkedList, LinkedListNode} from 'dbl-linked-list-ds'

import {ICancellable} from 'ts-scheduler'

/**
* @ignore
*/
export type CancelId = number

/**
* Collection of cancellables
* @ignore
*/
export class CancellationList {
private readonly q = new LinkedList<ICancellable>()
private readonly q = new Array<ICancellable>()

public cancel(): void {
let node = this.q.shift()
let node = this.q.pop()
while (node !== undefined) {
node.cancel()
node = this.q.shift()
node = this.q.pop()
}
}

public cancelId(node: LinkedListNode<ICancellable>): void {
node.value.cancel()
this.q.remove(node)
public cancelId(id: CancelId): void {
const node = this.q[id]
node.cancel()

this.q[id] = this.q[this.q.length - 1]
this.q.pop()
}

public push(cancel: ICancellable): LinkedListNode<ICancellable> {
return this.q.add(cancel)
public push(cancel: ICancellable): CancelId {
return this.q.push(cancel) - 1
}
}
6 changes: 4 additions & 2 deletions src/operators/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class MapExecutor<R1, E1, A1, A2> implements IExecutable {
private readonly res: (e: A2) => void,
private readonly runtime: Runtime,
private readonly cancellations: CancellationList
) {}
) {
this.onResolve = this.onResolve.bind(this)
}

public execute(): void {
this.cancellations.push(
this.src.fork(this.env, this.rej, this.onResolve, this.runtime)
)
}

private readonly onResolve = (a: A1) => {
private onResolve(a: A1): void {
this.res(this.ab(a))
}
}
Expand Down

0 comments on commit 782cf80

Please sign in to comment.