eliminate tail recursion for functions with var parameters
Original Reporter info from Mantis: medovina
-
Reporter name: Adam Dingle
Original Reporter info from Mantis: medovina
- Reporter name: Adam Dingle
Description:
Currently the optimizer won't eliminate tail recursion for functions that have var parameters. This is evident in opttail.pas, where there is code starting at the comment
{ check if the parameters actually would support tail recursion elimination }
the scans the parameters and exits if any of them have the var (or out) attribute.
That's too bad, since it's convenient to write many data structure operations recursively. For example, here's an insert procedure for a binary tree. Without this optimization, it could run out of stack space if the tree is unbalanced and tall.
===
type
node = record
i: integer;
left: ^node;
right: ^node;
end;
pnode = ^node;
procedure insert(var t: pnode; i: integer);
begin
if t = nil then
begin
new(t);
t^.i := i;
t^.left := nil;
t^.right := nil;
end
else
if i < t^.i
then insert(t^.left, i)
else insert(t^.right, i);
end;
Mantis conversion info:
- Mantis ID: 32811
- Version: 3.0.2
- Fixed in version: 3.3.1
- Fixed in revision: 43824 (#f6c16323)