Explicit TCE

Tyler Jameson Little beatgammit at gmail.com
Fri Oct 12 11:49:12 PDT 2012


>> My mention of overhead was just how complicated it would be to
>> implement. The general algorithm is (for each become keyword):
>>
>> * determine max stack size (consider all branches in all 
>> recursive
>> contexts)
>> * allocate stack size for top-level function
>> * do normal TCE stuff (use existing stack for new call)
>
> What's wrong with just allocating a new stack _in-place_  of 
> the old?
> In other words make 'become' synonym for 'reuse the current 
> stack frame'.
> Effectively you still stay in constant space that is maximum of 
> all functions being called.

That would work too. If scope() is disallowed, it doesn't matter 
where the stack comes from. It's only slightly cheaper to reuse 
the current stack (CPU), but making a new one would be lighter on 
memory.

> I see nice staff. My use case is optimizing virtual machine, 
> the one inside std.regex primarily.

Yeah, that is a great example! I've read some bug reports about 
std.regex using a ton of memory, especially with CTFE. Since 
regex is by definition a state machine, this would be a 
particularly elegant fit (granted, backreferences et al break 
that model, but it's still a nice metaphor).

The main problem I see is working with other compilers like 
GCC/LLVM. If this can be done on those compilers, I don't see any 
major hurdle to getting this implemented.


More information about the Digitalmars-d mailing list