typedarray-pool
Advanced tools
Comparing version 0.0.1 to 0.1.0
{ | ||
"name": "typedarray-pool", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Reuse typed arrays", | ||
@@ -5,0 +5,0 @@ "main": "pool.js", |
152
pool.js
@@ -29,3 +29,3 @@ "use strict" | ||
function free(array) { | ||
exports.free = function free(array) { | ||
if(array instanceof ArrayBuffer) { | ||
@@ -63,5 +63,40 @@ var n = array.byteLength|0 | ||
} | ||
exports.free = free | ||
function malloc(n, dtype) { | ||
exports.freeUint8 = function freeUint8(array) { | ||
UINT8[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeUint16 = function freeUint16(array) { | ||
UINT16[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeUint32 = function freeUint32(array) { | ||
UINT32[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeInt8 = function freeInt8(array) { | ||
INT8[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeInt16 = function freeInt16(array) { | ||
INT16[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeInt32 = function freeInt32(array) { | ||
INT32[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeFloat32 = exports.freeFloat = function freeFloat(array) { | ||
FLOAT[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeFloat64 = exports.freeDouble = function freeDouble(array) { | ||
DOUBLE[bits.log2(array.length)].push(array) | ||
} | ||
exports.freeArrayBuffer = function freeArrayBuffer(array) { | ||
DATA[bits.log2(array.length)].push(array) | ||
} | ||
exports.malloc = function malloc(n, dtype) { | ||
n = Math.max(bits.nextPow2(n), 32) | ||
@@ -167,6 +202,112 @@ var log_n = bits.log2(n) | ||
} | ||
exports.malloc = malloc | ||
exports.mallocUint8 = function mallocUint8(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = UINT8[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Uint8Array(n) | ||
} | ||
function clearCache() { | ||
exports.mallocUint16 = function mallocUint16(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = UINT16[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Uint16Array(n) | ||
} | ||
exports.mallocUint32 = function mallocUint32(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = UINT32[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Uint32Array(n) | ||
} | ||
exports.mallocInt8 = function mallocInt8(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = INT8[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Int8Array(n) | ||
} | ||
exports.mallocInt16 = function mallocInt16(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = INT16[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Int16Array(n) | ||
} | ||
exports.mallocInt32 = function mallocInt32(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = INT32[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Int32Array(n) | ||
} | ||
exports.mallocFloat32 = exports.mallocFloat = function mallocFloat(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = FLOAT[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Float32Array(n) | ||
} | ||
exports.mallocFloat64 = exports.mallocDouble = function mallocDouble(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = DOUBLE[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new Float64Array(n) | ||
} | ||
exports.mallocArrayBuffer = function mallocArrayBuffer(n) { | ||
n = Math.max(bits.nextPow2(n), 32)|0 | ||
var log_n = bits.log2(n) | ||
var cache = DATA[log_n] | ||
if(cache.length > 0) { | ||
var r = cache[cache.length-1] | ||
cache.pop() | ||
return r | ||
} | ||
return new ArrayBuffer(n) | ||
} | ||
exports.clearCache = function clearCache() { | ||
for(var i=0; i<32; ++i) { | ||
@@ -184,2 +325,1 @@ UINT8[i].length = 0 | ||
} | ||
exports.clearCache = clearCache |
@@ -48,2 +48,15 @@ typedarray-pool | ||
**Note** You can avoid the dispatch by directly calling one of the following methods: | ||
* `pool.mallocUint8` | ||
* `pool.mallocUint16` | ||
* `pool.mallocUint32` | ||
* `pool.mallocInt8` | ||
* `pool.mallocInt16` | ||
* `pool.mallocInt32` | ||
* `pool.mallocFloat` | ||
* `pool.mallocDouble` | ||
* `pool.mallocArrayBuffer` | ||
### `pool.free(array)` | ||
@@ -54,2 +67,14 @@ Returns the array back to the pool. | ||
**Note** You can speed up the method if you know the type of array before hand by calling one of the following: | ||
* `pool.freeUint8` | ||
* `pool.freeUint16` | ||
* `pool.freeUint32` | ||
* `pool.freeInt8` | ||
* `pool.freeInt16` | ||
* `pool.freeInt32` | ||
* `pool.freeFloat` | ||
* `pool.freeDouble` | ||
* `pool.freeArrayBuffer` | ||
### `pool.clearCache()` | ||
@@ -56,0 +81,0 @@ Removes all references to cached arrays. Use this when you are done with the pool to return all the cached memory to the garbage collector. |
@@ -57,2 +57,53 @@ "use strict" | ||
for(var i=1; i<100; ++i) { | ||
var a | ||
a = pool.mallocInt8(i) | ||
t.assert(a instanceof Int8Array) | ||
t.assert(a.length >= i) | ||
pool.freeInt8(a) | ||
a = pool.mallocInt16(i) | ||
t.assert(a instanceof Int16Array) | ||
t.assert(a.length >= i) | ||
pool.freeInt16(a) | ||
a = pool.mallocInt32(i) | ||
t.assert(a instanceof Int32Array) | ||
t.assert(a.length >= i) | ||
pool.freeInt32(a) | ||
a = pool.mallocUint8(i) | ||
t.assert(a instanceof Uint8Array) | ||
t.assert(a.length >= i) | ||
pool.freeUint8(a) | ||
a = pool.mallocUint16(i) | ||
t.assert(a instanceof Uint16Array) | ||
t.assert(a.length >= i) | ||
pool.freeUint16(a) | ||
a = pool.mallocUint32(i) | ||
t.assert(a instanceof Uint32Array) | ||
t.assert(a.length >= i) | ||
pool.freeUint32(a) | ||
a = pool.mallocFloat(i) | ||
t.assert(a instanceof Float32Array) | ||
t.assert(a.length >= i) | ||
pool.freeFloat(a) | ||
a = pool.mallocDouble(i) | ||
t.assert(a instanceof Float64Array) | ||
t.assert(a.length >= i) | ||
pool.freeDouble(a) | ||
a = pool.mallocArrayBuffer(i) | ||
t.assert(a instanceof ArrayBuffer) | ||
t.assert(a.byteLength >= i) | ||
pool.freeArrayBuffer(a) | ||
} | ||
pool.clearCache() | ||
@@ -59,0 +110,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14456
6
375
96