Commit 21f8e491 authored by yurys@chromium.org's avatar yurys@chromium.org

Simplify HeapSnapshotFilteredOrderedIterator implementation

Introduced filtered iterator and iterator over array of indexes. This allowed to decompose HeapSnapshotFilteredOrderedIterator.

BUG=None
R=alph@chromium.org

Review URL: https://codereview.chromium.org/205993003

git-svn-id: svn://svn.chromium.org/blink/trunk@169650 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent e77c8b3a
...@@ -315,14 +315,20 @@ function test() ...@@ -315,14 +315,20 @@ function test()
return node.type() === "object" && node.name() !== "B" && node.name() !== "D"; return node.type() === "object" && node.name() !== "B" && node.name() !== "D";
} }
var provider = new WebInspector.HeapSnapshotNodesProvider(snapshot, nodeFilter); var allNodeIndexes = [];
for (var i = 0; i < snapshot._nodes.length; i += snapshot._nodeFieldCount)
allNodeIndexes.push(i);
var provider = new WebInspector.HeapSnapshotNodesProvider(snapshot, nodeFilter, allNodeIndexes);
// Sort by names in reverse order. // Sort by names in reverse order.
InspectorTest.assertEquals(3, provider.length, "nodes provider length"); provider.sortAndRewind({fieldName1: "name", ascending1: false, fieldName2: "id", ascending2: false});
provider.sort({fieldName1: "name", ascending1: false, fieldName2: "id", ascending2: false}, 0, 2, 3); var range = provider.serializeItemsRange(0, 3);
InspectorTest.assertEquals(3, provider.length, "nodes provider length"); InspectorTest.assertEquals(3, range.totalLength, "Node range total length");
var names = []; InspectorTest.assertEquals(0, range.startPosition, "Node range start position");
for (provider.rewind(); provider.hasNext(); provider.next()) InspectorTest.assertEquals(3, range.endPosition, "Node range end position");
names.push(provider.item().name()); var names = range.items.map(function(item)
{
return item.name;
});
InspectorTest.assertEquals("E,C,A", names.join(","), "nodes provider names"); InspectorTest.assertEquals("E,C,A", names.join(","), "nodes provider names");
}, },
...@@ -336,12 +342,15 @@ function test() ...@@ -336,12 +342,15 @@ function test()
} }
var provider = snapshot.createEdgesProviderForTest(snapshot.rootNodeIndex, edgeFilter); var provider = snapshot.createEdgesProviderForTest(snapshot.rootNodeIndex, edgeFilter);
InspectorTest.assertEquals(1, provider.length, "edges provider length"); provider.sortAndRewind({fieldName1: "!edgeName", ascending1: false, fieldName2: "id", ascending2: false});
provider.sort({fieldName1: "!edgeName", ascending1: false, fieldName2: "id", ascending2: false}, 0, 0, 1); var range = provider.serializeItemsRange(0, 10);
InspectorTest.assertEquals(1, provider.length, "edges provider length"); InspectorTest.assertEquals(1, range.totalLength, "Edge range total length");
var names = []; InspectorTest.assertEquals(0, range.startPosition, "Edge range start position");
for (provider.rewind(); provider.hasNext(); provider.next()) InspectorTest.assertEquals(1, range.endPosition, "Edge range end position");
names.push(provider.item().name()); var names = range.items.map(function(item)
{
return item.name;
});
InspectorTest.assertEquals("b", names.join(","), "edges provider names"); InspectorTest.assertEquals("b", names.join(","), "edges provider names");
}, },
......
...@@ -129,8 +129,45 @@ WebInspector.HeapSnapshotEdge.prototype = { ...@@ -129,8 +129,45 @@ WebInspector.HeapSnapshotEdge.prototype = {
} }
}; };
/**
* @interface
*/
WebInspector.HeapSnapshotItemIterator = function() { }
WebInspector.HeapSnapshotItemIterator.prototype = {
rewind: function() { },
/**
* @return {boolean}
*/
hasNext: function() { },
/**
* @return {number}
*/
index: function() { },
/**
* @param {number} newIndex
*/
setIndex: function(newIndex) { },
/**
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
*/
item: function() { },
next: function() { }
};
/** /**
* @constructor * @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotEdge} edge
*/ */
WebInspector.HeapSnapshotEdgeIterator = function(edge) WebInspector.HeapSnapshotEdgeIterator = function(edge)
{ {
...@@ -328,6 +365,8 @@ WebInspector.HeapSnapshotRetainerEdge.prototype = { ...@@ -328,6 +365,8 @@ WebInspector.HeapSnapshotRetainerEdge.prototype = {
/** /**
* @constructor * @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotRetainerEdge} retainer
*/ */
WebInspector.HeapSnapshotRetainerEdgeIterator = function(retainer) WebInspector.HeapSnapshotRetainerEdgeIterator = function(retainer)
{ {
...@@ -601,6 +640,8 @@ WebInspector.HeapSnapshotNode.prototype = { ...@@ -601,6 +640,8 @@ WebInspector.HeapSnapshotNode.prototype = {
/** /**
* @constructor * @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotNode} node
*/ */
WebInspector.HeapSnapshotNodeIterator = function(node) WebInspector.HeapSnapshotNodeIterator = function(node)
{ {
...@@ -627,7 +668,7 @@ WebInspector.HeapSnapshotNodeIterator.prototype = { ...@@ -627,7 +668,7 @@ WebInspector.HeapSnapshotNodeIterator.prototype = {
*/ */
index: function() index: function()
{ {
return this.node.nodeIndex; return /** @type{number} */ (this.node.nodeIndex);
}, },
/** /**
...@@ -653,6 +694,127 @@ WebInspector.HeapSnapshotNodeIterator.prototype = { ...@@ -653,6 +694,127 @@ WebInspector.HeapSnapshotNodeIterator.prototype = {
} }
/**
* @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotItemIterator} iterator
* @param {!Array.<number>|!Uint32Array} indexes
*/
WebInspector.HeapSnapshotIndexRangeIterator = function(iterator, indexes)
{
this._iterator = iterator;
this._indexes = indexes;
this._position = 0;
this._forcedIndex = -1;
}
WebInspector.HeapSnapshotIndexRangeIterator.prototype = {
rewind: function() { },
/**
* @return {boolean}
*/
hasNext: function()
{
return this._position < this._indexes.length
},
/**
* @return {number}
*/
index: function()
{
if (this._forcedIndex !== -1)
return this._forcedIndex;
return this._indexes[this._position];
},
/**
* @param {number} newIndex
*/
setIndex: function(newIndex)
{
this._forcedIndex = newIndex;
},
/**
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
*/
item: function()
{
this._iterator.setIndex(this.index());
return this._iterator.item();
},
next: function()
{
++this._position;
}
}
/**
* @constructor
* @implements {WebInspector.HeapSnapshotItemIterator}
* @param {!WebInspector.HeapSnapshotItemIterator} iterator
*/
WebInspector.HeapSnapshotFilteredIterator = function(iterator, filter)
{
this._iterator = iterator;
this._filter = filter;
this._skipFilteredItems();
}
WebInspector.HeapSnapshotFilteredIterator.prototype = {
rewind: function() { },
/**
* @return {boolean}
*/
hasNext: function()
{
return this._iterator.hasNext();
},
/**
* @return {number}
*/
index: function()
{
return this._iterator.index();
},
/**
* @param {number} newIndex
*/
setIndex: function(newIndex)
{
this._iterator.setIndex(newIndex);
},
/**
* @return {!WebInspector.HeapSnapshotEdge|!WebInspector.HeapSnapshotNode|!WebInspector.HeapSnapshotRetainerEdge}
*/
item: function()
{
return this._iterator.item();
},
next: function()
{
this._iterator.next();
this._skipFilteredItems();
},
_skipFilteredItems: function()
{
while (this._iterator.hasNext() && !this._filter(this._iterator.item())) {
this._iterator.next();
}
}
}
/** /**
* @param {!WebInspector.HeapSnapshotWorkerDispatcher=} dispatcher * @param {!WebInspector.HeapSnapshotWorkerDispatcher=} dispatcher
* @constructor * @constructor
...@@ -1892,63 +2054,27 @@ WebInspector.HeapSnapshot.prototype = { ...@@ -1892,63 +2054,27 @@ WebInspector.HeapSnapshot.prototype = {
/** /**
* @constructor * @constructor
* @param {(!Array.<number>|!Uint32Array)=} unfilteredIterationOrder * @param {!WebInspector.HeapSnapshotItemIterator} iterator
*/ */
WebInspector.HeapSnapshotFilteredOrderedIterator = function(iterator, filter, unfilteredIterationOrder) WebInspector.HeapSnapshotItemProvider = function(iterator)
{ {
this._filter = filter;
this._iterator = iterator; this._iterator = iterator;
this._unfilteredIterationOrder = unfilteredIterationOrder; this._isEmpty = !iterator.hasNext();
/** @type {?Array.<number>|?Uint32Array} */ /** @type {?Array.<number>} */
this._iterationOrder = null; this._iterationOrder = null;
this._position = 0;
this._currentComparator = null; this._currentComparator = null;
this._sortedPrefixLength = 0; this._sortedPrefixLength = 0;
this._sortedSuffixLength = 0; this._sortedSuffixLength = 0;
} }
WebInspector.HeapSnapshotFilteredOrderedIterator.prototype = { WebInspector.HeapSnapshotItemProvider.prototype = {
_createIterationOrder: function() _createIterationOrder: function()
{ {
if (this._iterationOrder) if (this._iterationOrder)
return; return;
if (this._unfilteredIterationOrder && !this._filter) {
this._iterationOrder = this._unfilteredIterationOrder;
this._unfilteredIterationOrder = null;
return;
}
this._iterationOrder = []; this._iterationOrder = [];
var iterator = this._iterator; for (var iterator = this._iterator; iterator.hasNext(); iterator.next())
if (!this._unfilteredIterationOrder && !this._filter) {
for (iterator.rewind(); iterator.hasNext(); iterator.next())
this._iterationOrder.push(iterator.index());
} else if (!this._unfilteredIterationOrder) {
for (iterator.rewind(); iterator.hasNext(); iterator.next()) {
if (this._filter(iterator.item()))
this._iterationOrder.push(iterator.index());
}
} else {
var order = this._unfilteredIterationOrder;
for (var i = 0, l = order.length; i < l; ++i) {
iterator.setIndex(order[i]);
if (this._filter(iterator.item()))
this._iterationOrder.push(iterator.index()); this._iterationOrder.push(iterator.index());
}
this._unfilteredIterationOrder = null;
}
},
rewind: function()
{
this._position = 0;
},
/**
* @return {boolean}
*/
hasNext: function()
{
return this._position < this._iterationOrder.length;
}, },
/** /**
...@@ -1956,50 +2082,7 @@ WebInspector.HeapSnapshotFilteredOrderedIterator.prototype = { ...@@ -1956,50 +2082,7 @@ WebInspector.HeapSnapshotFilteredOrderedIterator.prototype = {
*/ */
isEmpty: function() isEmpty: function()
{ {
if (this._iterationOrder) return this._isEmpty;
return !this._iterationOrder.length;
if (this._unfilteredIterationOrder && !this._filter)
return !this._unfilteredIterationOrder.length;
var iterator = this._iterator;
if (!this._unfilteredIterationOrder && !this._filter) {
iterator.rewind();
return !iterator.hasNext();
} else if (!this._unfilteredIterationOrder) {
for (iterator.rewind(); iterator.hasNext(); iterator.next())
if (this._filter(iterator.item()))
return false;
} else {
var order = this._unfilteredIterationOrder;
for (var i = 0, l = order.length; i < l; ++i) {
iterator.setIndex(order[i]);
if (this._filter(iterator.item()))
return false;
}
}
return true;
},
/**
* @return {*}
*/
item: function()
{
this._iterator.setIndex(this._iterationOrder[this._position]);
return this._iterator.item();
},
/**
* @type {number}
*/
get length()
{
this._createIterationOrder();
return this._iterationOrder.length;
},
next: function()
{
++this._position;
}, },
/** /**
...@@ -2021,14 +2104,15 @@ WebInspector.HeapSnapshotFilteredOrderedIterator.prototype = { ...@@ -2021,14 +2104,15 @@ WebInspector.HeapSnapshotFilteredOrderedIterator.prototype = {
if (end >= this._iterationOrder.length - this._sortedSuffixLength) if (end >= this._iterationOrder.length - this._sortedSuffixLength)
this._sortedSuffixLength = this._iterationOrder.length - begin; this._sortedSuffixLength = this._iterationOrder.length - begin;
} }
var position = begin;
this._position = begin;
var startPosition = this._position;
var count = end - begin; var count = end - begin;
var result = new Array(count); var result = new Array(count);
for (var i = 0 ; i < count && this.hasNext(); ++i, this.next()) var iterator = this._iterator;
result[i] = this.item().serialize(); for (var i = 0 ; i < count; ++i) {
return new WebInspector.HeapSnapshotCommon.ItemsRange(startPosition, this._position, this._iterationOrder.length, result); iterator.setIndex(this._iterationOrder[position++]);
result[i] = iterator.item().serialize();
}
return new WebInspector.HeapSnapshotCommon.ItemsRange(begin, end, this._iterationOrder.length, result);
}, },
sortAndRewind: function(comparator) sortAndRewind: function(comparator)
...@@ -2036,18 +2120,19 @@ WebInspector.HeapSnapshotFilteredOrderedIterator.prototype = { ...@@ -2036,18 +2120,19 @@ WebInspector.HeapSnapshotFilteredOrderedIterator.prototype = {
this._currentComparator = comparator; this._currentComparator = comparator;
this._sortedPrefixLength = 0; this._sortedPrefixLength = 0;
this._sortedSuffixLength = 0; this._sortedSuffixLength = 0;
this.rewind();
} }
} }
/** /**
* @constructor * @constructor
* @extends {WebInspector.HeapSnapshotFilteredOrderedIterator} * @extends {WebInspector.HeapSnapshotItemProvider}
*/ */
WebInspector.HeapSnapshotEdgesProvider = function(snapshot, filter, edgesIter) WebInspector.HeapSnapshotEdgesProvider = function(snapshot, filter, edgesIter)
{ {
this.snapshot = snapshot; this.snapshot = snapshot;
WebInspector.HeapSnapshotFilteredOrderedIterator.call(this, edgesIter, filter); if (filter)
edgesIter = new WebInspector.HeapSnapshotFilteredIterator(edgesIter, filter);
WebInspector.HeapSnapshotItemProvider.call(this, edgesIter);
} }
WebInspector.HeapSnapshotEdgesProvider.prototype = { WebInspector.HeapSnapshotEdgesProvider.prototype = {
...@@ -2132,19 +2217,23 @@ WebInspector.HeapSnapshotEdgesProvider.prototype = { ...@@ -2132,19 +2217,23 @@ WebInspector.HeapSnapshotEdgesProvider.prototype = {
this._iterationOrder.sortRange(compareNodeAndNode, leftBound, rightBound, windowLeft, windowRight); this._iterationOrder.sortRange(compareNodeAndNode, leftBound, rightBound, windowLeft, windowRight);
}, },
__proto__: WebInspector.HeapSnapshotFilteredOrderedIterator.prototype __proto__: WebInspector.HeapSnapshotItemProvider.prototype
} }
/** /**
* @constructor * @constructor
* @extends {WebInspector.HeapSnapshotFilteredOrderedIterator} * @extends {WebInspector.HeapSnapshotItemProvider}
* @param {(!Array.<number>|!Uint32Array)=} nodeIndexes * @param {?function(!WebInspector.HeapSnapshotNode):boolean} filter
* @param {(!Array.<number>|!Uint32Array)} nodeIndexes
*/ */
WebInspector.HeapSnapshotNodesProvider = function(snapshot, filter, nodeIndexes) WebInspector.HeapSnapshotNodesProvider = function(snapshot, filter, nodeIndexes)
{ {
this.snapshot = snapshot; this.snapshot = snapshot;
WebInspector.HeapSnapshotFilteredOrderedIterator.call(this, snapshot._allNodes(), filter, nodeIndexes); var it = new WebInspector.HeapSnapshotIndexRangeIterator(snapshot._allNodes(), nodeIndexes);
if (filter)
it = new WebInspector.HeapSnapshotFilteredIterator(it, filter);
WebInspector.HeapSnapshotItemProvider.call(this, it);
} }
WebInspector.HeapSnapshotNodesProvider.prototype = { WebInspector.HeapSnapshotNodesProvider.prototype = {
...@@ -2226,6 +2315,6 @@ WebInspector.HeapSnapshotNodesProvider.prototype = { ...@@ -2226,6 +2315,6 @@ WebInspector.HeapSnapshotNodesProvider.prototype = {
this._iterationOrder.sortRange(this._buildCompareFunction(comparator), leftBound, rightBound, windowLeft, windowRight); this._iterationOrder.sortRange(this._buildCompareFunction(comparator), leftBound, rightBound, windowLeft, windowRight);
}, },
__proto__: WebInspector.HeapSnapshotFilteredOrderedIterator.prototype __proto__: WebInspector.HeapSnapshotItemProvider.prototype
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment