-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0b34385
Showing
10 changed files
with
642 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
deePool.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.gitignore | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# deePool | ||
|
||
A highly-efficient simple (no frills) object pool. | ||
|
||
## Explanation | ||
|
||
**deePool** (aka "deep-pool") is an object pool with efficiency (speed, low memory, little-to-no GC) as its primary design goal. | ||
|
||
As such, there are no configuration options to tweak behavior. It does one thing, and one thing well. If you want to re-configure the behavior, modify the code. :) | ||
|
||
Also, this library doesn't really try to do much in the way of graceful handling of errors or mistakes, as that stuff just slows it down. You should be careful in how you use *deePool*. | ||
|
||
## Environment Support | ||
|
||
This library requires ES6+ support. If you need to use it in ES<=5, transpile it with Babel yourself. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
julien-f
|
||
|
||
## Library API | ||
|
||
There's only one method on the API: | ||
|
||
```js | ||
deePool.create( objectFactory ) | ||
``` | ||
|
||
* `create(..)`: produces a new pool instance. You can create as many separate pools as you need. | ||
|
||
`objectFactory` must be a function that produces a single new empty object (or array, etc) that you want available in the pool. Examples: | ||
|
||
```js | ||
var myArrays = deePool.create( function makeArray(){ | ||
return [ | ||
[ "foo", "bar", "baz" ], | ||
[ 1, 2, 3 ] | ||
]; | ||
} ); | ||
|
||
var myWidgets = deePool.create( function makeWidgets(){ | ||
return new SomeCoolWidget(1,2,3); | ||
} ); | ||
``` | ||
|
||
### Pool API | ||
|
||
Each pool has four simple methods on its API: | ||
|
||
```js | ||
pool.use() | ||
|
||
pool.recycle( obj ) | ||
|
||
pool.grow( additionalSize ) | ||
|
||
pool.size() | ||
``` | ||
|
||
* `pool.use()`: retrieves an available object instance from the pool. Example: | ||
|
||
```js | ||
var arr = myArrays.use(); | ||
``` | ||
|
||
**Note:** If the pool doesn't have any free instances, it will automatically grow (double in size, or set to `5` if currently empty) and then return one of the new instances. | ||
|
||
* `pool.recycle(..)`: inserts an object instance back into the pool. Example: | ||
|
||
```js | ||
myArrays.recycle( arr ); | ||
``` | ||
|
||
**Tips:** | ||
- Don't forget to `recycle(..)` object instances after you're done using them. They are not automatically recycled, and your pool will run out of available instances, and thus keep growing unboundedly if you don't recycle. | ||
- Don't `recycle(..)` an object instance until you're fully done with it. As objects are held by reference in JS, if you hold onto a reference and modify an already recycled object, you will cause difficult to track down bugs in your application! To avoid this pitfall, unset your reference(s) to a pooled object immediately after `recycle(..)`. | ||
- Don't `recycle(..)` objects that weren't created for the pool and extracted by a previous `use()`. | ||
- Don't `recycle(..)` an object more than once. This will end up creating multiple references in the pool to the same object, which will cause difficult to track down bugs in your application! To avoid this pitfall, unset your reference(s) to a pooled object immediately after `recycle(..)`. | ||
- Don't create references between object instances in the pool, or you will cause difficult to track down bugs in your application! | ||
- If you need to "condition" or "reset" an object instance for its later reuse, do so before passing it into `recycle(..)`. If a pooled object holds references to other objects, and you want that memory freed up, make sure to unset those references. | ||
|
||
**Note:** If you insert an object instance into a pool that has no empty slots (this is always a mistake, but is not disallowed!), the result will be that the pool grows in size by 1. | ||
|
||
* `pool.grow(..)`: A number passed will specify how many instances to add to the pool (created by `objectFactory()` as specified in the [`deePool.create(..)` call](#Library_API)). If no number is passed, the default is the current size of the pool (effectively doubling it in size). Examples: | ||
|
||
```js | ||
var myPool = deePool.create(makeArray); | ||
|
||
var arr = myPool.use(); // pool size now `5` | ||
|
||
myPool.grow( 3 ); // pool size now `8` | ||
myPool.grow(); // pool size now `16` | ||
``` | ||
|
||
**Tips:** | ||
- A new pool starts out empty (size: `0`). Always call `grow(..)` with a valid positive (non-zero) number to initialize the pool before using it. Otherwise, the call will have no effect; on an empty pool, this will confusingly leave the pool empty. | ||
- An appropriate initial size for the pool will depend on the tasks you are performing; essentially, how many objects will you need to use concurrently? | ||
|
||
You should profile for this with your use-case(s) to determine what the most likely maximum pool size is. You'll want a pool size that's big enough so it doesn't have to grow very often, but not so big that it wastes memory. | ||
- Don't grow the pool manually unless you are really sure you know what you're doing. It's better to set the pool size initially and let it grow automatically (via `use()`) only as it needs to. | ||
|
||
* `pool.size()`: Returns the number of overall slots in the pool (both used and unused). Example: | ||
|
||
```js | ||
var myPool = deePool.create(makeArray); | ||
|
||
myPool.size(); // 0 | ||
|
||
myPool.grow(5); | ||
myPool.grow(10); | ||
myPool.grow(5); | ||
|
||
myPool.size(); // 20 | ||
``` | ||
|
||
## Builds | ||
|
||
The core library file can be built (minified) with an included utility: | ||
|
||
``` | ||
./build-core.js | ||
``` | ||
|
||
However, the recommended way to invoke this utility is via npm: | ||
|
||
``` | ||
npm run-script build-core | ||
``` | ||
|
||
## Tests | ||
|
||
To run the tests, you must first [build the core library](#Builds). | ||
|
||
With `npm`, run: | ||
|
||
``` | ||
npm test | ||
``` | ||
|
||
Or, manually: | ||
|
||
``` | ||
node node-tests.js | ||
``` | ||
|
||
## License | ||
|
||
The code and all the documentation are released under the MIT license. | ||
|
||
http://getify.mit-license.org/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env node | ||
|
||
var fs = require("fs"), | ||
path = require("path"), | ||
// ugly = require("uglify-js"), | ||
|
||
result | ||
; | ||
|
||
console.log("*** Building Core ***"); | ||
console.log("Minifying to deePool.js."); | ||
|
||
try { | ||
// result = ugly.minify(path.join(__dirname,"lib","deePool.src.js"),{ | ||
// mangle: { | ||
// keep_fnames: true | ||
// }, | ||
// compress: { | ||
// keep_fnames: true | ||
// }, | ||
// output: { | ||
// comments: /^!/ | ||
// } | ||
// }); | ||
|
||
|
||
// NOTE: since uglify doesn't yet support ES6, no minifying happening, | ||
// just pass through copying. :( | ||
fs.writeFileSync( | ||
path.join(__dirname,"deePool.js"), | ||
|
||
fs.readFileSync(path.join(__dirname,"lib","deePool.src.js")), | ||
|
||
// result.code + "\n", | ||
{ encoding: "utf8" } | ||
); | ||
|
||
console.log("Complete."); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
(function UMD(name,context,definition){ | ||
if (typeof define === "function" && define.amd) { define(definition); } | ||
else if (typeof module !== "undefined" && module.exports) { module.exports = definition(); } | ||
else { context[name] = definition(name,context); } | ||
})("deePool",this,function DEF(name,context){ | ||
"use strict"; | ||
|
||
const EMPTY_SLOT = Object.freeze(Object.create(null)); | ||
const NOTHING = EMPTY_SLOT; | ||
|
||
return { create }; | ||
|
||
|
||
// ****************************** | ||
|
||
// create a new pool | ||
function create(objectFactory = ()=>({})) { | ||
var objPool = []; | ||
var nextFreeSlot = null; // pool location to look for a free object to use | ||
var nextOpenSlot = null; // pool location to insert next recycled object | ||
|
||
return { | ||
use, | ||
recycle, | ||
grow, | ||
size, | ||
}; | ||
|
||
|
||
// ****************************** | ||
|
||
function use() { | ||
var objToUse = NOTHING; | ||
|
||
// know where the next free object in the pool is? | ||
if (nextFreeSlot != null) { | ||
objToUse = objPool[nextFreeSlot]; | ||
objPool[nextFreeSlot] = EMPTY_SLOT; | ||
} | ||
// otherwise, go looking | ||
else { | ||
// search starts with position + 1 (so, 0) | ||
nextFreeSlot = -1; | ||
} | ||
|
||
// look for the next free obj | ||
for ( | ||
var count = 0, i = nextFreeSlot + 1; | ||
(count < objPool.length) && (i < objPool.length); | ||
i = (i + 1) % objPool.length, count++ | ||
) { | ||
// found free obj? | ||
if (objPool[i] !== EMPTY_SLOT) { | ||
// still need to free an obj? | ||
if (objToUse === NOTHING) { | ||
objToUse = objPool[i]; | ||
objPool[i] = EMPTY_SLOT; | ||
} | ||
// this slot's obj can be used next time | ||
else { | ||
nextFreeSlot = i; | ||
return objToUse; | ||
} | ||
} | ||
} | ||
|
||
// no other free objs left | ||
nextFreeSlot = null; | ||
|
||
// still haven't found a free obj to use? | ||
if (objToUse === NOTHING) { | ||
// double the pool size (or minimum 5 if empty) | ||
grow(objPool.length || 5); | ||
|
||
objToUse = objPool[nextFreeSlot]; | ||
objPool[nextFreeSlot] = EMPTY_SLOT; | ||
nextFreeSlot++; | ||
} | ||
|
||
return objToUse; | ||
} | ||
|
||
function recycle(obj) { | ||
// know where the next empty slot in the pool is? | ||
if (nextOpenSlot != null) { | ||
objPool[nextOpenSlot] = obj; | ||
obj = NOTHING; | ||
} | ||
// otherwise, go looking | ||
else { | ||
// search starts with position + 1 (so, 0) | ||
nextOpenSlot = -1; | ||
} | ||
|
||
// look for the next empty slot | ||
for ( | ||
var count = 0, i = nextOpenSlot + 1; | ||
(count < objPool.length) && (i < objPool.length); | ||
i = (i + 1) % objPool.length, count++ | ||
) { | ||
// found empty slot? | ||
if (objPool[i] === EMPTY_SLOT) { | ||
// obj still needs to be reinserted? | ||
if (obj !== NOTHING) { | ||
objPool[i] = obj; | ||
obj = NOTHING; | ||
} | ||
// this empty slot can be used next time! | ||
else { | ||
nextOpenSlot = i; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// no empty slots left | ||
nextOpenSlot = null; | ||
|
||
// still haven't recycled obj? | ||
if (obj !== NOTHING) { | ||
// insert at the end (growing size of pool by 1) | ||
// NOTE: this is likely mis-use of the library | ||
objPool[objPool.length] = obj; | ||
} | ||
} | ||
|
||
function grow(count = objPool.length) { | ||
nextFreeSlot = objPool.length; | ||
|
||
for (var i = 0; i < count; i++) { | ||
// add new obj to pool | ||
objPool[objPool.length] = objectFactory(); | ||
} | ||
|
||
// look for an open slot | ||
nextOpenSlot = null; | ||
for (var i = 0; i < nextFreeSlot; i++) { | ||
// found an open slot? | ||
if (objPool[i] === EMPTY_SLOT) { | ||
nextOpenSlot = i; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
function size() { | ||
return objPool.length; | ||
} | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
global.deePool = require("./lib/deePool.src.js"); | ||
global.QUnit = require("qunitjs"); | ||
|
||
require("./qunit.config.js"); | ||
require("./tests.js"); | ||
|
||
// temporary hack per: https://github.com/qunitjs/qunit/issues/1081 | ||
QUnit.load(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "deepool", | ||
"version": "1.0.0", | ||
"description": "deePool: highly-efficient pool for objects", | ||
"main": "./lib/deePool.src.js", | ||
"scripts": { | ||
"test": "./node-tests.js", | ||
"build-core": "./build-core.js", | ||
"build": "npm run build-core", | ||
"prepublish": "npm run build-core" | ||
}, | ||
"devDependencies": { | ||
"qunitjs": "~2.1.0", | ||
"uglify-js": "~2.7.5" | ||
}, | ||
"keywords": [ | ||
"object", | ||
"pool", | ||
"performance" | ||
], | ||
"author": "Kyle Simpson <getify@gmail.com>", | ||
"license": "MIT" | ||
} |
Oops, something went wrong.
I get it, but just FYI, it makes it difficult to use in the browser with most existing setups (facebook/create-react-app#1125).