tags:

views:

141

answers:

2

I can fill a TreeView control with particular Registry keys by enumerating registry keys recursively and put them in the TreeView control; then for performance reason, I attempt to use a nonrecursive/iterative approach to enumerate registry keys, but how can I fill the TreeView since a "tree" is naturally recursive (at least, in my understanding)? Is recursion is the only way to achieve it? Would someone give some code snippets/examples or redirect me to webpages explain this matter?

BTW, I'm a Delphi/Free Pascal programmer, but C/C++ programming language explanation should be no problem at all. Cheers :-)

I have asked about this on the Free Pascal mailing list, too.

Thanks in advance.

+1  A: 

If performance is the issue, consider delayed filling of a TreeView. Start from creating top level of a tree. Fill each next level of a tree only when user expands it. This should solve a stack overflow problem as well.

Here you can find sources of the Native Registry Editor project which you could use as a sample.

Kirill V. Lyadvinsky
Thanks Lyadvinsky for your quick response, that's a nice project, I'll look at it; but there are a few nightmare on recursion, such as: - Default stack sizes are different among the OS platforms. - Superdeep recursion will trigger a stack overflow exception.That's why I thought to refactored the recursion part with nonrecursive/iterative approach.
Phantom
"delyed fillingof a TreeView" + Delphi = VirtualTreeView :-) (http://www.soft-gems.net/ and http://code.google.com/p/virtual-treeview/)
Ulrich Gerhardt
Nice Ulrich, thanks.
Phantom
It is possible to recurse without using up the stack space. Optimizing compilers are able to accomplish certain types of recursion into efficient non-recursive loops. Your code has to be written a certain way to allow that, though. I do not recall the particular details right now, but I have seen discussions on StackOverflow and other websites that describe it in more detail.
Remy Lebeau - TeamB
For "superdeep" recursion to cause a stack overflow, @MrBA, the depth would have to be on the order of hundreds, if not thousands of levels deep. Your stack is at least a megabyte; how many bytes do you have in your stack frame? And how deeply have you ever seen registry keys be nested?
Rob Kennedy
Are you aware of any Delphi or Pascal compilers that perform such optimizations, @Remy?
Rob Kennedy
Rob, No, I'm not aware of Delphi or Pascal compilers that perform such optimizations. Could you tell me how, at least in Delphi?
Phantom
Remy, could the "certain way" you said be made compatible with Delphi and FPC?
Phantom
BTW, Rob, good catch :-) I quote your editing to my post "I have asked about this on the Free Pascal mailing list, too." Is that the rule here on stackoverflow.com? This question is my first question here.
Phantom
http://en.wikipedia.org/wiki/Tail_recursion
Remy Lebeau - TeamB
ahaa, thanks Remy for the links :-) I'm new to this. Quoted from wikipedia "tail recursion is a special case of recursion in which ...". Conclusion at a glance, it seems the most types of recursion can't be made into a tail recursion." However, I will study this article further.
Phantom
It didn't really have anything to do with Stack Overflow, @MrBA. It's just polite that when you ask a question *anywhere*, you tell people where else you've asked. Then they can get background information about your question and avoid duplicating other people's effort. And I wasn't asking *you* whether you know of any tail-call-optimizing compilers. I was asking *Remy*, expecting the answer *no*, thus questioning the usefulness of his comment.
Rob Kennedy
Oops, Rob, I missed the word "@Remy" after the comma. Sorry :-) For the politeness, I usually summarize enough background information I got from others if they are exist. I didn't quote the link because of frighening about the spammers identifying my real e-mail. However, I appreciate your effort, that's not a problem at all. :-)
Phantom
Well, Rob! an FPC compiler maintainer said the recent version of FPC understand the tail recursion. fpc -i |grep -i tailrecto find out more. Is there a comparable option for Delphi?
Phantom
+2  A: 

First of all, worrying about the "performance" of recursion when you're reading the data from the registry and putting data into a tree control sounds a bit silly. The costs of recursion are in the nanosecond range, while the cost of reading from the registry is in the microsecond to millisecond range. The cost of inserting into the tree control will depend on things like visibility and how many items it contains, but it's typically closer to the range of the registry than of recursion. If you're going to insert a lot of items into a control, you usually want to lock the control so it's not updated during the insertion, then turn updating back on after you're done.

Second, yes, it can be done without recursion. The usual way is to have a container of some sort to hold data that will need to be processed, such as a queue or stack. When you're walking the registry tree, you retrieve data, and when/if you encounter a "subdirectory" in the tree, you push it on the stack. When you finish with the current "directory", you retrieve the next one from the stack/queue/whatever and process it the same way. When the collection is empty, you're done.

Jerry Coffin
Thanks Jerry for your answer, but performance is not the only reason. See my reply to the first answer. Anyway, thanks to your second statement. :-)
Phantom
Any sort of Tree/ListView VCL control will have much better performance is you use BeginUpdate/EndUpdate. Just tossing this tip out there, for anyone considering this.
Chris Thornton
One more knowledge, thanks Chris.
Phantom