Skip to content

Commit

Permalink
Make uuid cache a Set as well
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Feb 11, 2016
1 parent 1da8192 commit 28658e4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 36 deletions.
29 changes: 11 additions & 18 deletions dist/diffhtml.js
Expand Up @@ -2000,7 +2000,7 @@ function cleanMemory(makeNode) {
// Clean out unused elements.
if (makeNode && makeNode.nodes) {
for (var uuid in makeNode.nodes) {
if (!pools.elementObject.cache.uuid[uuid]) {
if (!pools.elementObject.cache.uuid.has(uuid)) {
delete makeNode.nodes[uuid];
}
}
Expand Down Expand Up @@ -2401,7 +2401,7 @@ function createPool(name, opts) {
free: [],
allocated: new Set(),
protected: new Set(),
uuid: {}
uuid: new Set()
};

// Prime the cache with n objects.
Expand All @@ -2422,7 +2422,7 @@ function createPool(name, opts) {
cache.protected.add(value);

if (name === 'elementObject') {
cache.uuid[value.uuid] = value;
cache.uuid.add(value.uuid);
}
},
unprotect: function unprotect(value) {
Expand All @@ -2432,29 +2432,22 @@ function createPool(name, opts) {
}

if (name === 'elementObject') {
delete cache.uuid[value.uuid];
cache.uuid.delete(value.uuid);
}
},
freeAll: function freeAll() {
var freeLength = cache.free.length;
var minusOne = freeLength - 1;

// All of this could go away if we could figure out `Array.from` within
// a PhantomJS web-worker.
var reAlloc = [];
cache.allocated.forEach(function (v) {
return reAlloc.push(v);
cache.allocated.forEach(function (value) {
cache.free.push(value);

if (name === 'elementObject') {
cache.uuid.delete(value.uuid);
}
});
reAlloc = reAlloc.slice(0, size - minusOne);

cache.free.push.apply(cache.free, reAlloc);
cache.allocated.clear();

if (name === 'elementObject') {
reAlloc.forEach(function (element) {
return delete cache.uuid[element.uuid];
});
}
cache.free.length = size;
},
free: function free(value) {
// Already freed.
Expand Down
2 changes: 1 addition & 1 deletion lib/util/memory.js
Expand Up @@ -57,7 +57,7 @@ export function cleanMemory(makeNode) {
// Clean out unused elements.
if (makeNode && makeNode.nodes) {
for (let uuid in makeNode.nodes) {
if (!pools.elementObject.cache.uuid[uuid]) {
if (!pools.elementObject.cache.uuid.has(uuid)) {
delete makeNode.nodes[uuid];
}
}
Expand Down
25 changes: 11 additions & 14 deletions lib/util/pools.js
Expand Up @@ -17,7 +17,7 @@ export function createPool(name, opts) {
free: [],
allocated: new Set(),
protected: new Set(),
uuid: {}
uuid: new Set(),
};

// Prime the cache with n objects.
Expand All @@ -39,7 +39,7 @@ export function createPool(name, opts) {
cache.protected.add(value);

if (name === 'elementObject') {
cache.uuid[value.uuid] = value;
cache.uuid.add(value.uuid);
}
},

Expand All @@ -50,26 +50,23 @@ export function createPool(name, opts) {
}

if (name === 'elementObject') {
delete cache.uuid[value.uuid];
cache.uuid.delete(value.uuid);
}
},

freeAll() {
let freeLength = cache.free.length;
let minusOne = freeLength - 1;

// All of this could go away if we could figure out `Array.from` within
// a PhantomJS web-worker.
let reAlloc = [];
cache.allocated.forEach(v => reAlloc.push(v));
reAlloc = reAlloc.slice(0, size - minusOne);
cache.allocated.forEach(value => {
cache.free.push(value);

cache.free.push.apply(cache.free, reAlloc);
cache.allocated.clear();
if (name === 'elementObject') {
cache.uuid.delete(value.uuid);
}
});

if (name === 'elementObject') {
reAlloc.forEach(element => delete cache.uuid[element.uuid]);
}
cache.allocated.clear();
cache.free.length = size;
},

free(value) {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/memory.js
Expand Up @@ -14,9 +14,9 @@ describe('Integration: Memory management', function() {

it('can allocate/deallocate uuids', function() {
diff.innerHTML(this.fixture, '<p></p>');
assert.equal(Object.keys(pools.elementObject.cache.uuid).length, 2);
assert.equal(pools.elementObject.cache.uuid.size, 2);

diff.innerHTML(this.fixture, '');
assert.equal(Object.keys(pools.elementObject.cache.uuid).length, 1);
assert.equal(pools.elementObject.cache.uuid.size, 1);
});
});
2 changes: 1 addition & 1 deletion test/util/validateMemory.js
Expand Up @@ -11,7 +11,7 @@ export default function validateMemory() {
assert.equal(pools.elementObject.cache.allocated.size, 0,
'Should not leave leftover allocations');

assert.equal(Object.keys(pools.elementObject.cache.uuid).length, 0,
assert.equal(pools.elementObject.cache.uuid.size, 0,
'All UUIDs should be unprotected');

assert.equal(Object.keys(makeNode.nodes).length, 0,
Expand Down

0 comments on commit 28658e4

Please sign in to comment.