Recursion Error
Bwain| No Comments »So I tried the following:
void ctor(int i) { print “ctor ” i “\n”; ctor(i+1); }
ctor(0);
I ran it, and it only recurses 757 times before it kind of stops. Its odd, I can’t tell where its failing, it seems odd to me that it would just run out of RAM. I tried running a similar function in perl, and it just keeps going. I tracked the RAM use, that’s definitely not the issue…
I narrowed the point of failure to a single deque assignment. I’m not too sure as to what would cause an stl function to silently end the process. Maybe memory is thrashed. In any case, I suspect I’m doing too much processing for each function recursion. I need to list exactly what happens when I’m doing a function call and see if I can’t streamline it.
I found this, explaining how stl will throw exceptions when they have memory failures, and possibly ways to alleviate it. When I run it, the task manager only report the process at 20M, which is nothing. Don’t know why stl would be failing. Also, the runtime stack is WAY deep. The debugger wont (can’t) display all of it. Each function call adds about 4 layers to the runtime stack. Given that the script is at 760 calls deep, that’s about 3040 runtime stack calls. But perl still works really well. Maybe I should try a release compile and see how far that gets.
Did a release build, and it executed the function call 3890 deep. Still, not as efficient as perl, but I guess it does appear to be a ram issue. Tomorrow, I’ll work on optimizing/streamlining the function calls. Crud… I ran the perl example until it ran out of RAM, it went 2262191 deep…. Python defaults to a stack depth of 1000, but when you extend it, it runs even deeper than perl.
Looked around online. To do a scalable, production ready scripting language, requires flattening the code into opcodes, and running a virtual machine in a similar way that I’m running the simd virtual machine. I’m not going to get into that now, I just want to get it working. Speed and scalability will have to wait. Found this book that explains a generalized virtual machine implementation.
I’ve been using the debugger to trace down the cause of the bad_alloc. I’ve narrowed it down to a SymbolTable constructor. But that doesn’t solve anything. The SymbolTables have been working forever, now that I’m allocating thousands of them, they’re crashing. I’ve noticed that there is a LOT of overhead for the symbolTable base class. So the next step definitely should be a streamlining step. There are many std::map and std::multimap’s being created unnecessarily. The symbol table for the stack should be a lot lighter, if its going to be instanced like that.


