Recursivity
BlitzMax Forums/BlitzMax Beginners Area/Recursivity
| ||
I was thinking.. what about using Bmax for code that's extremely recursive? Can you pump up the stack for a minute or two using some simple commands? I remember having a crash in Bmax when trying something out and since then I've been weary about using recursive solutions, which is a shame. Extreme stacks might not be best practice often but when they're needed they're extremely handy. |
| ||
Depends on which OS your on, they all have different defaults for stack size and ways to deal with it. I only know that Windows has 1MB by default. Which means you can go 10000+ deep depending on argument/local count. The only way to change the stack size in a running program on Windows is by creating new threads or fibers with an explicit stack size. But since bmx threading api hasnt exposed that, you would have to modify it a bit. Or, modify bmk and set the stack size in the link stage. There is also a field for stack size in the executable header that can be modified after the fact, which is probably the cleanest approach. |
| ||
Grable, thanks. I guess the best way to be economical about the stack without hacking would be to use VAR keyword and keep all the parameters inside that single address / pointer? |
| ||
Var arguments still have to copy the address to the stack, so use the same amount of memory. I dont think arguments are the biggest worry here anyway. Your bound to have more locals than arguments in a sufficiently advanced function. And even if you have NO arguments and locals, the stack would still grow by at least 8 bytes because of the return pointer and stack frame setup. You can estimate the maximum recursion depth though: Local stacksize:Int = 2097152' 2mb Local numargs:Int = 2 Local numlocals:Int = 5 Local framesize:Int = 5 ' RET,EBX,ESI,EDI,EBP Local funcsize:Int = (numargs + numlocals + framesize) * 4 Print "maxdepth=" + (stacksize / funcsize) May i ask what are you doing that requires such deep recursion? |
| ||
I won't bore you with the details but I've used Bmax for a month now and already I'm considering to use Bmax as my primary programming language and prototyping tool, as I love it. But if I am to throw away all my other tools, which I hope to do, then I know I really need to be able to do recursive algorithms of some level with it. And I don't want any nasty surprises. It's more the concept that needs to be available in some form. I need to be able to do stuff, it does not have to be quick or anything. |
| ||
Its not something ive ever worried about to be honest. Just know that 2MB (the default on my setup) is a lot, and in the general case youl never overflow it. And when in doubt, calculate an estimate. After making a tool to change the stack size in the executable header, i found out that MinGW (at least 5.1.0 that i use) sets the reserved stack size to 2MB, commit is still 4K. For comparison, BCC sets its stack to 4MB. EDIT: After some more digging, it seems BMK sets the stack size to 4MB as well. So you can go pretty deep :) Im posting the tool here if you ever need more stack space, i made it in C though because it was easier. pestacksize.c |
| ||
Nice, thanks alot. |