[Gambas-user] Gambas has Gosub now!

Emil Lenngren emil.lenngren at ...626...
Mon Jul 9 00:13:58 CEST 2012


I have a little optimization idea:

Instead of allocating a new array EVERY time a gosub is called, instead use
the normal gambas stack.
First, let GP represent the offset 0 in &BP[fp->n_local + fp->n_ctrl].

At a gosub, run STACK_check(1 + fp->stack_usage - fp->n_local), then the
return address and ctrl values are stored on the stack starting at
&BP[fp->n_local + fp->n_ctrl + GP], then GP and SP is added by 1 +
fp->n_ctrl.

On a gosub return, if GP == 0, it is a normal return. If not, it is a gosub
return. Then first subtract 1 + fp->n_ctrl from GP and SP and restore the
ctrl values and PC.

At exception handling, going to a Catch, change
while (SP > (BP + FP->n_local + FP->n_ctrl))
to
while (SP > (BP + FP->n_local + FP->n_ctrl + GP))

I think there is nothing else that has to be done. Leaving a function pops
off the stack until it reaches PP, so that is fine.

It is also possible to completely remove the function
STACK_free_gosub_stack.

Can this be a good idea?

/Emil

2012/3/7 Kevin Fishburne <kevinfishburne at ...1887...>

> On 03/05/2012 09:00 PM, Benoît Minisini wrote:
> > Le 06/03/2012 02:11, Emil Lenngren a écrit :
> >> Yeah, if there is no stack relocation, I can assume that a lot of
> pointers
> >> into the stack will not change during other external calls, which should
> >> make code more optimized.
> >>
> >> Since local variables and stack frames in gambas take up more space than
> >> they do in C, I think the stack should be bit bigger than the default
> in C
> >> (compiled with gcc).
> >>
> >> /Emil
> >>
> > Stack allocation through mmap() has been implemented in revision #4536.
> > Tested on my 64 bits system only.
> >
> > Now recursion speed is equivalent between GoSub and normal functions as
> > soon as there are control variables. If there is no control variables to
> > save, GoSub is about 2.5 times faster than normal functions.
> >
> That is awesome. Optimizations using traditional syntax are always
> welcome. C-like speed achieved with BASIC functions. :)
>
> --
> Kevin Fishburne
> Eight Virtues
> www: http://sales.eightvirtues.com
> e-mail: sales at ...1887...
> phone: (770) 853-6271
>
>
>
> ------------------------------------------------------------------------------
> Virtualization & Cloud Management Using Capacity Planning
> Cloud computing makes use of virtualization - but cloud computing
> also focuses on allowing computing to be delivered as a service.
> http://www.accelacomm.com/jaw/sfnl/114/51521223/
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



More information about the User mailing list