Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tail recusrion, optimization #2805

Closed
vicuna opened this issue Jun 19, 2001 · 1 comment
Closed

tail recusrion, optimization #2805

vicuna opened this issue Jun 19, 2001 · 1 comment
Labels

Comments

@vicuna
Copy link

vicuna commented Jun 19, 2001

Original bug ID: 397
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Hi,

I'm learning OCAML and I've downloaded your compiler (ocamlopt) for SPARC. I
was trying to compare the performance of gcc versus ocamlopt. My first test
program was a loop that adds the numbers from 1 to 1 billion. I noticed two
things that I think need to be redressed:

  1. ocamlopt (and ocamlc) do not detect tail recursion in the following case:

let rec sum n = if n = 0 then 0 else n + sum (n - 1) in

sum 1000000000;;

This little program causes a stack overflow, needlessly. The compiler does
detect tail recursion in the more trivial case:

let rec stupid n = if n = 0 then 0 else stupid (n - 1) in

stupid 1000000000;;

  1. When I compared the performance of the iterated version of the program:

let a = ref 0 in
for i = 1 to 1000000000 do
a := !a + i
done ;;

with a C equivalent, the C program took 40% of the time required for the
OCAML version. When I looked at the assembly code, I found out that ocamlopt
generated code that recomputes the upper bound of the loop at every
iteration. This is not only suboptimal (since the boundary is a constant)
but also seems to contradict the semantics of the 'for' command, that call
for a one-time computation of the loop boundary values, at the start of the
loop. Had the compiler not done this repeated computation, the OCAML program
would have run, at my estimation, about 30-40% faster.

Thanks,

Dan Arnon

@vicuna
Copy link
Author

vicuna commented Jun 21, 2001

Comment author: administrator

Misunderstanding of what tail recursion is

@vicuna vicuna closed this as completed Jun 21, 2001
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant