-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (50 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const zlib = require('zlib');
const {promisify} = require('util');
const gzipPromise = promisify(zlib.gzip);
const gunzipPromise = promisify(zlib.gunzip);
const StringArray = require('./stringArray');
const inferArrayType = require('./inferArrayType');
const decode = require('./decode');
const encode = require('./encode');
const vm = require('vm');
const DATA_TYPE_LOOKUP = [ Int32Array, Int16Array, Int8Array, Uint32Array, Uint16Array, Uint8Array, Float32Array, Float64Array, StringArray ];
const ENCODE_OPTIMIZATION_FN = [];
const DECODE_OPTIMIZATION_FN = [];
const DATA_VERSION = 7;
function typeOptimizedFunction(cache, index, originalFn) {
// v8 optimization does quite poorly if different array types
// are used for the same execution of the same function.
// this code forces v8 to treat each array type as a separately
// optimized function
let fn = cache[index];
if (!fn) {
fn = cache[index] = vm.runInThisContext(`(${originalFn.toString()})()`);
}
return fn;
}
module.exports = {
encode: (arr, options, debugInfo) => {
let ResultType = inferArrayType(arr),
resultTypeIndex = DATA_TYPE_LOOKUP.indexOf(ResultType);
return typeOptimizedFunction(ENCODE_OPTIMIZATION_FN, resultTypeIndex, encode)(DATA_VERSION, arr, ResultType, resultTypeIndex, gzipPromise, options || {}, debugInfo);
},
decode: (buffer) => {
var dataVersion = buffer.readUInt8(0),
arrayTypeIndex = buffer.readUInt8(1),
hasLUT = false,
isGzipped = false;
if (DATA_VERSION !== dataVersion) {
throw 'INVALID';
}
if (arrayTypeIndex & 128) {
hasLUT = true;
arrayTypeIndex &= 127;
}
if (arrayTypeIndex & 64) {
isGzipped = true;
arrayTypeIndex &= 63;
}
var ArrayType = DATA_TYPE_LOOKUP[arrayTypeIndex];
return typeOptimizedFunction(DECODE_OPTIMIZATION_FN, buffer.readUInt16LE(1), decode)(buffer, hasLUT, ArrayType, isGzipped, gunzipPromise);
}
};