From 3a9303852c02991cda4018c2f0fd8c65fed74949 Mon Sep 17 00:00:00 2001 From: "mikhail.naganov@gmail.com" Date: Mon, 21 Sep 2009 07:12:38 +0000 Subject: [PATCH] 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 --- src/zone-inl.h | 19 +++++++++++++------ src/zone.h | 8 +------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/zone-inl.h b/src/zone-inl.h index b3141a4..121ba19 100644 --- a/src/zone-inl.h +++ b/src/zone-inl.h @@ -276,12 +276,19 @@ void ZoneSplayTree::Splay(const Key& key) { } -template -static void DoForEach(Node* node, Callback* callback) { - if (node == NULL) return; - DoForEach(node->left(), callback); - callback->Call(node->key(), node->value()); - DoForEach(node->right(), callback); +template template +void ZoneSplayTree::ForEach(Callback* callback) { + // Pre-allocate some space for tiny trees. + ZoneList 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()); + } } diff --git a/src/zone.h b/src/zone.h index cdbab32..4e4f1d7 100644 --- a/src/zone.h +++ b/src/zone.h @@ -204,10 +204,6 @@ class ZoneScope BASE_EMBEDDED { }; -template -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 - void ForEach(Callback* c) { - DoForEach::Node, Callback>(root_, c); - } + void ForEach(Callback* callback); private: Node* root_; -- 2.7.4