Skip to content

Commit

Permalink
first version worker
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Nov 17, 2018
1 parent 424337a commit 86c3b62
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 22 deletions.
13 changes: 0 additions & 13 deletions src/model/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ export const defaultGroup: IGroup = {
color: 'gray'
};

/**
* @internal
*/
export function chooseByLength(length: number) {
if (length <= 255) {
return new Uint8Array(length);
}
if (length <= 65535) {
return new Uint16Array(length);
}
return new Uint32Array(length);
}


/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion src/model/Ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {equalArrays, fixCSS} from '../internal';
import AEventDispatcher, {suffix} from '../internal/AEventDispatcher';
import {isSortingAscByDefault} from './annotations';
import Column, {IColumnParent, IFlatColumn, visibilityChanged, dirtyValues, dirtyHeader, labelChanged, widthChanged, dirty, ICompareValue, ECompareValueType} from './Column';
import {defaultGroup, IOrderedGroup, IndicesArray, chooseByLength} from './Group';
import {defaultGroup, IOrderedGroup, IndicesArray} from './Group';
import {IDataRow, IGroup, IGroupData} from './interfaces';
import {joinGroups} from './internal';
import NumberColumn, {filterChanged} from './NumberColumn';
Expand Down
4 changes: 4 additions & 0 deletions src/provider/ADataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ abstract class ADataProvider extends AEventDispatcher implements IDataProvider {
}
}

destroy() {
// dummy
}

/**
* hook method for cleaning up a ranking
* @param _ranking
Expand Down
10 changes: 7 additions & 3 deletions src/provider/LocalDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import Column, {
defaultGroup, ICategoricalColumn, IColumnDesc, IDataRow, IGroup, INumberColumn,
IOrderedGroup, ICompareValue,
NumberColumn,
chooseByLength,
IndicesArray,
mapIndices
} from '../model';
import Ranking from '../model/Ranking';
import ACommonDataProvider from './ACommonDataProvider';
import {IDataProviderOptions, IStatsBuilder} from './interfaces';
import {local, ISortWorker, sortComplex} from './sort';
import {local, ISortWorker, sortComplex, chooseByLength, WorkerSortWorker} from './sort';
import {range} from 'd3-array';
import ADataProvider from './ADataProvider';

Expand Down Expand Up @@ -50,7 +49,7 @@ export default class LocalDataProvider extends ACommonDataProvider {

private _dataRows: IDataRow[];
private filter: ((row: IDataRow) => boolean) | null = null;
private sortWorker: ISortWorker = local;
private sortWorker: ISortWorker = new WorkerSortWorker();

constructor(private _data: any[], columns: IColumnDesc[] = [], options: Partial<ILocalDataProviderOptions & IDataProviderOptions> = {}) {
super(columns, options);
Expand Down Expand Up @@ -91,6 +90,11 @@ export default class LocalDataProvider extends ACommonDataProvider {
return this._data;
}

destroy() {
super.destroy();
this.sortWorker.terminate();
}

/**
* replaces the dataset rows with a new one
* @param data
Expand Down
99 changes: 98 additions & 1 deletion src/provider/sort.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import {ICompareValue, ECompareValueType} from '../model/Column';
import {FIRST_IS_NAN, FIRST_IS_MISSING} from '../model/missing';
import {chooseByLength, IndicesArray} from '../model';
import {IndicesArray} from '../model';
import {createWorker, IPoorManWorkerScope, toFunctionBody} from './worker';


/**
* @internal
*/
export function chooseByLength(length: number) {
if (length <= 255) {
return new Uint8Array(length);
}
if (length <= 65535) {
return new Uint16Array(length);
}
return new Uint32Array(length);
}


const missingFloat = FIRST_IS_NAN > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
Expand Down Expand Up @@ -118,3 +133,85 @@ export const local: ISortWorker = {
sort,
terminate: () => undefined
};

interface ISortMessageRequest {
uid: number;

rawLength: number;
arr: {i: number, sort: ICompareValue[]}[];
comparators?: {asc: boolean, v: ECompareValueType}[];
}

interface ISortMessageResponse {
uid: number;

order: IndicesArray;
index2pos: IndicesArray;
}


function sortWorkerMain(self: IPoorManWorkerScope) {
self.addEventListener('message', (evt) => {
const r = <ISortMessageRequest>evt.data;
if (typeof r.uid !== 'number') {
return;
}

const arr = r.comparators ? sortComplex(r.arr, r.comparators) : r.arr;
const res = sort2indices(arr, r.rawLength);

self.postMessage(<ISortMessageResponse>{
uid: r.uid,
order: res.order,
index2pos: res.index2pos
}, [res.order.buffer, res.index2pos.buffer]);
});
}


export class WorkerSortWorker implements ISortWorker {
private readonly worker: Worker;

constructor() {
this.worker = createWorker([
chooseByLength.toString(),
`var missingFloat = ${missingFloat};`,
`var missingInt = ${missingInt};`,
`var missingString = '${missingString}';`,
`var compare = new Intl.Collator();`,
floatCompare,
uintCompare,
stringCompare,
floatCompareDesc,
uintCompareDesc,
stringCompareDesc,
toFunction.toString(),
sortComplex.toString(),
sort2indices.toString(),
toFunctionBody(sortWorkerMain)
]);
}

sort(rawLength: number, arr: {i: number, sort: ICompareValue[]}[], comparators?: {asc: boolean, v: ECompareValueType}[]) {
return new Promise<{order: IndicesArray, index2pos: IndicesArray}>((resolve) => {
const uid = Math.random();

const receiver = (msg: MessageEvent) => {
const r = <ISortMessageResponse>msg.data;
if (r.uid !== uid) {
return;
}
this.worker.removeEventListener('message', receiver);
resolve({order: r.order, index2pos: r.index2pos});
};

this.worker.postMessage(<ISortMessageRequest>{
arr, comparators, rawLength, uid
});
});
}

terminate() {
this.worker.terminate();
}
}
8 changes: 4 additions & 4 deletions src/provider/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ function workerMain(self: IPoorManWorkerScope) {
});
}

function toFunctionBody(f: Function) {
export function toFunctionBody(f: Function) {
const source = f.toString();
return source.slice(source.indexOf('{') + 1, source.lastIndexOf('}'));
}

export function createWorker(fs: Function[]) {
const sources: string[] = fs.map(toFunctionBody);
export function createWorker(fs: (string | Function)[]) {
const sources = fs.map((d) => d.toString()).join('\n\n');

const blob = new Blob(sources, {type: 'application/javascript'});
const blob = new Blob([sources], {type: 'application/javascript'});

return new Worker(URL.createObjectURL(blob));
}
1 change: 1 addition & 0 deletions src/ui/ALineUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export abstract class ALineUp extends AEventDispatcher implements ILineUpLike {
destroy() {
// just clear since we hand in the node itself
clear(this.node);
this.data.destroy();
}

dump() {
Expand Down

0 comments on commit 86c3b62

Please sign in to comment.