Compile time for unused functions might take very long time

I'm a Racket newbie and this is perhaps a common question, but I haven't found any answers on this.

Here's a simple function just returning a large list:

(define slow-test
  (range 1 100000000))

(writeln "after")

Note that the function is not called. However, it takes a lot of time when running this program. Running it in DrRacket creates a lot of "Interactions disabled; out of memory" (and offers to increase the memory). Running the program from command line give the same behavior (but no out of memory errors) and it takes about 13s to run.

I am assuming that it has something to do with how Racket reads and compiles functions. Is there some a way of avoiding this behavior (apart from commenting the unused function definitions)?

Environment:

  • Racket v8.3
  • Linux Ubuntu 20.05

Best,

Hakan

(define slow-test
  (range 1 100000000))

That's not a function definition. You're setting the variable slow-test to the result of (range 1 100000000), which is a very large list that takes time and a lot of memory allocations to create. If it were a function, it might look like

(define (slow-test)
  (range 1 100000000))

or

(define slow-test
  (lambda () (range 1 100000000)))
2 Likes

Thanks! That explains everything.

(It has probably been too much Common Lisp lately. :slight_smile: )

1 Like