Skip to content

Commit

Permalink
issue2:
Browse files Browse the repository at this point in the history
Added some optimizations
- arguments leaking and param reassignment
- memoizee.. but it does not appear to really make a difference

memoizee confirmed memory leak fixes

shrinking leak test for travis

remove yarn

fix travis
  • Loading branch information
nmccready committed Jul 12, 2017
1 parent 0afa914 commit 0d31daa
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 1,004 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ build/
npm-debug.log
bower_components/
.versions

package-lock.json
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ install:
branches:
except:
- /^v[0-9]/

env:
- CXX=g++-4.8
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "eslint !./node_modules *.js ./**/*.js",
"mocha": "mocha $@",
"mocha": "mocha",
"test": "npm run lint && mocha ./test/**/*test.js ./test/*.test.js"
},
"repository": {
Expand All @@ -20,13 +20,15 @@
"author": "Nicholas McCready",
"license": "MIT",
"dependencies": {
"debug": "2.X",
"debug": "2.3.0",
"memoizee": "^0.4.5",
"object-assign": "4.1.0"
},
"devDependencies": {
"chai": "3.X",
"eslint": "3.X",
"hook-std": "0.X",
"memwatch-next": "^0.3.0",
"mocha": "3.X"
}
}
7 changes: 4 additions & 3 deletions src/debugFabFactory.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module.exports = function debugFactory(debugApi, options) {
module.exports = function debugFactory(_debugApi, _options) {
var wrapLazyEval = require('./lazy-eval');
var formatArgs = require('./formatArgs');

options = options || {
var options = _options || {
formatArgs: true
};

debugApi = debugApi ? debugApi : require('debug');
var debugApi = _debugApi ? _debugApi : require('debug');
debugApi = wrapLazyEval(debugApi);

debugApi = formatArgs({
debugApi: debugApi,
options: options
Expand Down
26 changes: 13 additions & 13 deletions src/lazy-eval.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


var slice = [].slice,
objectAssign = require('object-assign');

var objectAssign = require('object-assign');
var memoize = require('memoizee');

function _resolveOutput(func, bindThis) {
var wrapped = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
var i = arguments.length;
var args = [];
while (i--) args[i] = arguments[i];

// lazy function eval to keep output memory pressure down, if not used
if (typeof args[0] === 'function') {
Expand All @@ -21,12 +19,12 @@ function _resolveOutput(func, bindThis) {
};


function wrapEval(debug) {
function wrapEval(_debug) {

var debugOrig = debug,
noop = function(){};
var debugOrig = _debug;
var noop = function(){};

debug = function (namespace) {
function debug(namespace) {

var instance = debugOrig(namespace);

Expand All @@ -42,9 +40,11 @@ function wrapEval(debug) {
return instance;
}

objectAssign(debug, debugOrig);
var debugMemoized = memoize(debug);

objectAssign(debugMemoized, debugOrig);

return debug;
return debugMemoized;
}

module.exports = wrapEval;
18 changes: 12 additions & 6 deletions src/spawn.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
function spawnFactory(namespace, debugFabFactory) {
namespace = namespace || '';
function spawnFactory(_namespace, _debugFabFactory) {
var memoize = require('memoizee');
var namespace = _namespace || '';
var debugFabFactory = _debugFabFactory;

if(!debugFabFactory){
debugFabFactory = require('./debugFabFactory')();
}

function Debugger(base, ns){
base = base || '';
ns = ns || '';
var newNs = ns ? base + ':' + ns : base;
function Debugger(_base, _ns){
var base = _base || '';
var ns = _ns || '';

var newNs = ns ? [base, ns].join(':') : base;
var debug = debugFabFactory(newNs);

this.debug = debug;
this.debug.spawn = this.spawn;
}
Expand All @@ -20,6 +24,8 @@ function spawnFactory(namespace, debugFabFactory) {
return dbg.debug;
};

Debugger.prototype.spawn = memoize(Debugger.prototype.spawn);

var rootDebug = (new Debugger(namespace)).debug;

return rootDebug;
Expand Down
47 changes: 43 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
'use strict';
var expect = require('chai').expect;
var hook = require('hook-std');
var memwatch = require('memwatch-next');

memwatch.on('leak', function(info) {
console.log("LEAK");
console.log(info);
throw new Error("there is a LEAK");
process.exit(500);
});

var heapDiff = new memwatch.HeapDiff();

describe('index / spawn', function () {

var rootDbg, unhook;
var rootDbg, unhook, date;

before(function(){
date = Date.now();
})

after(function(){
var hde = heapDiff.end();
var change = hde.change;
change.details = null;

console.log(JSON.stringify({
before: hde.before,
after: hde.after,
change: change
}, null, 2));
console.log("index.test.js Total time: " + (Date.now() - date));
})

describe('namespacing', function () {
before(function () {
beforeEach(function () {
var origDebug = require('..')();
origDebug.save('root*');
origDebug.enable(origDebug.load())
Expand Down Expand Up @@ -41,8 +68,8 @@ describe('index / spawn', function () {
describe('child1', function(){
var child1Dbg;

before(function () {
child1Dbg = rootDbg.spawn('child1')
beforeEach(function () {
child1Dbg = rootDbg.spawn('child1');
})

it('handles functions', function (done) {
Expand All @@ -67,6 +94,18 @@ describe('index / spawn', function () {
unhook();
});

it('leak', function () {
// memory leak attempt
for(var i = 0; i < 1000;i++){
child1Dbg = rootDbg.spawn('child1');
leakTest('leakTest' + i);
}
});

function leakTest(testName) {
child1Dbg(function () {return testName;});
}

describe('grandChild1', function(){
var grandChild1;

Expand Down
62 changes: 50 additions & 12 deletions test/src/lazy-eval-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
var expect = require('chai').expect;
hook = require('hook-std')
var hook = require('hook-std');
var memwatch = require('memwatch-next');
var debugFact = require('../../src/debugFabFactory')();

memwatch.on('leak', function(info) {
console.log("LEAK");
console.log(info);
throw new Error("there is a LEAK");
process.exit(500);
});

var heapDiff = new memwatch.HeapDiff();

describe('lazy-eval', function () {

var debug;
var debug, unhook, date;

before(function(){
date = Date.now();
})

after(function(){
var hde = heapDiff.end();
var change = hde.change;
change.details = null;

console.log(JSON.stringify({
before: hde.before,
after: hde.after,
change: change
}, null, 2));
console.log("lazy-eval-test.js Total time: " + (Date.now() - date));
})

describe('enabled', function () {
before(function () {

debug = require('../../src/debugFabFactory')();
debug.save('enabled');
debug.enable(debug.load())
debug = debug('enabled');
debugFact.save('enabled');
debugFact.enable(debugFact.load())
debug = debugFact('enabled');
// console.log(debug);
})

Expand All @@ -25,6 +51,20 @@ describe('lazy-eval', function () {
unhook();
});


it('leak', function () {
// memory leak attempt
for(var i = 0; i < 1000;i++){
debug = debugFact('enabled');
leakTest("leak" + i);
}

function leakTest(name){
debug(function () {return name;});
}
});


it('normal', function (done) {
unhook = hook.stderr(function (str) {
expect(str.match(/crap/)).to.be.ok;
Expand All @@ -38,11 +78,9 @@ describe('lazy-eval', function () {

describe('disabled', function () {
before(function () {

debug = require('../../src/debugFabFactory')();
debug.save(null);
debug.enable(debug.load())
debug = debug('disabled');
debugFact.save(null);
debugFact.enable(debugFact.load())
debug = debugFact('disabled');
})

it('handles functions', function () {
Expand Down

0 comments on commit 0d31daa

Please sign in to comment.