[Gambas-user] Gambas has Gosub now!

Emil Lenngren emil.lenngren at ...626...
Mon Mar 5 18:28:53 CET 2012


It is no strange at all that stack reallocation is slower and slower as the
recursion gets deeper. Because when the stack has to be resized, a new
memory area is allocated and the old stack is copied over to the new, and
the old gets freed. As the recursion gets deeper, more and more data has to
be copied each time.

I wonder why the stack in gambas is kind of unlimited. If you accidently
create a program that do infinite recursion, no one complains and in a
short time the computer's memory has run out.

A better approach (what I personally think) is to set a limit at maybe 48
MB or something (which only should actually be reached if you do infinite
recursion). The normal stack size in a C program is 8 MB on my computer,
and if it gets overflowed, you get segmentation fault.
If you mmap the stack with mmap(NULL, 48 << 20, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0), the real memory usage will only be what
is actually used (only the pages that are touched). One extra benifit with
that is that the stack doesn't have to be relocated ;)

/Emil

2012/3/5 Benoît Minisini <gambas at ...1...>

> Le 04/03/2012 10:16, Benoît Minisini a écrit :
> > Le 04/03/2012 02:23, Benoît Minisini a écrit :
> >>
> >> You are right, I didn't think about that. So the current design does not
> >> work, and I don't have another one.
> >>
> >> I will remove GOSUB if it cannot be implemented in a right way. :-/
> >>
> >> Regards,
> >>
> >
> > Here is the only solution I found in revision #4531:
> >
> > * Now I use a dedicated control variable set for each GOSUB subroutine.
> >
> > * GOSUB labels now must be at top-level, and must be declared *after*
> > the GOSUB call. Consequently, recursion is not possible anymore.
> >
> > I hope that GOSUB is still useful!
> >
>
> Here is a new implementation of GOSUB in revision #4534.
>
> GOSUB can be used on any top-level label now.
>
> GOSUB can be recursive now, because control variables are saved and
> restored on the RETURN instruction.
>
> GOSUB is still faster than calling a true method, and far faster when
> using recursion. Stack allocation is heavier, and slower and slower as
> far as the recursion level grows (dunno why). GOSUB does not have that
> problem.
>
> Enjoy it!
>
> --
> Benoît Minisini
>
>
> ------------------------------------------------------------------------------
> 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