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) { ...@@ -276,12 +276,19 @@ void ZoneSplayTree<C>::Splay(const Key& key) {
} }
template <typename Node, class Callback> template <typename Config> template <class Callback>
static void DoForEach(Node* node, Callback* callback) { void ZoneSplayTree<Config>::ForEach(Callback* callback) {
if (node == NULL) return; // Pre-allocate some space for tiny trees.
DoForEach<Node, Callback>(node->left(), callback); ZoneList<Node*> nodes_to_visit(10);
callback->Call(node->key(), node->value()); nodes_to_visit.Add(root_);
DoForEach<Node, Callback>(node->right(), callback); 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 { ...@@ -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 // A zone splay tree. The config type parameter encapsulates the
// different configurations of a concrete splay tree: // different configurations of a concrete splay tree:
// //
...@@ -297,9 +293,7 @@ class ZoneSplayTree : public ZoneObject { ...@@ -297,9 +293,7 @@ class ZoneSplayTree : public ZoneObject {
}; };
template <class Callback> template <class Callback>
void ForEach(Callback* c) { void ForEach(Callback* callback);
DoForEach<typename ZoneSplayTree<Config>::Node, Callback>(root_, c);
}
private: private:
Node* root_; 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