Eliminate recursion in ZoneSplayTree traversal.

Convert the code to be similar with JS version. Recursive traversal is dangerous as it can cause stack exhaustion on deep trees.

Review URL: http://codereview.chromium.org/211024

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2939 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent ef246011
......@@ -276,12 +276,19 @@ void ZoneSplayTree<C>::Splay(const Key& key) {
}
template <typename Node, class Callback>
static void DoForEach(Node* node, Callback* callback) {
if (node == NULL) return;
DoForEach<Node, Callback>(node->left(), callback);
callback->Call(node->key(), node->value());
DoForEach<Node, Callback>(node->right(), callback);
template <typename Config> template <class Callback>
void ZoneSplayTree<Config>::ForEach(Callback* callback) {
// Pre-allocate some space for tiny trees.
ZoneList<Node*> nodes_to_visit(10);
nodes_to_visit.Add(root_);
int pos = 0;
while (pos < nodes_to_visit.length()) {
Node* node = nodes_to_visit[pos++];
if (node == NULL) continue;
callback->Call(node->key(), node->value());
nodes_to_visit.Add(node->left());
nodes_to_visit.Add(node->right());
}
}
......
......@@ -204,10 +204,6 @@ class ZoneScope BASE_EMBEDDED {
};
template <typename Node, class Callback>
static void DoForEach(Node* node, Callback* callback);
// A zone splay tree. The config type parameter encapsulates the
// different configurations of a concrete splay tree:
//
......@@ -297,9 +293,7 @@ class ZoneSplayTree : public ZoneObject {
};
template <class Callback>
void ForEach(Callback* c) {
DoForEach<typename ZoneSplayTree<Config>::Node, Callback>(root_, c);
}
void ForEach(Callback* callback);
private:
Node* root_;
......
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