I'm doing some recursive parsing.
Currently I have a fake stack, where I store states for my finite state machine, so as I drill down recursively I push the state I was in, and pop it later after I've finished processing the recursive bit of text.
Would it be faster to have a 'state id' stack like:
int* stack = 0
int top = 0;
// ...
// drill down bit
if (stack == 0)
stack = (int*)malloc(STACK_JUMP_SIZE);
else if (top % STACK_JUMP_SIZE == 0)
stack = (int*)realloc(stack, (top+STACK_JUMP_SIZE) * sizeof(int));
stack[top++] = currentState;
// ...
// pop up later
{currentState = stack[--top]; {
if (top == 0) {
free(stack);
stack = 0;
} else if ((top+1) % STACK_JUMP_SIZE == 0) {
stack = (int*)realloc(stack, (top+1)*sizeof(int));
}
Or would it be faster to split the thing up into proper functions and let C++ worry about the stack.
(I know someone's gonna tell me 'that's C, it's not c++', so I pre-emptively answer, my program's c++ but has a lot of c in it).