Why does Common Lisp (SBCL) use so much memory for a simple program?

since I am new to Common Lisp I tried to solve problems on SPOJ using Common Lisp ( SBCL ). The first problem is the simple task of reading numbers until 42 is found. Here is my solution:

(defun study-num () (let ((num (parse-integer (read-line t)))) (when (not (= num 42)) (format t "~A~%" num) (study-num)))) (study-num) 

Decision is made. But when I looked at the details of the result, I found that he used 57 M MEM! This is a bloody unreasonable, but I can not understand why. What can I do to optimize?

+4
source share
3 answers

You make repeated recursive calls without enabling enough optimization to activate tail call elimination (SBCL does this, but only when you have “optimization for speed”, high and “optimization for debugging information” is set to low )

The Lisp standard leaves the tail call exception as an implementation quality issue and provides other loop constructs (e.g. LOOP or DO, both of which may be appropriate for this application).

In addition, the recently launched SBCL is likely to be larger than you expect, due to the need to use the runtime and the base image.

+4
source

I think you don’t understand that Common Lisp is an online language environment with a complete library and compiler loaded into RAM to give you the first prompt. After that, loading in your program is probably a barely noticeable increase in size. Lisp does not compile or link an independent executable consisting only of your code and any lib routines available from your code. This is what C and similar languages ​​do. Instead, Lisp adds your code to an already meaningful online environment. As a new user, this seems awful. But if you have a modern general-purpose computer with 100 MB of RAM, it quickly becomes something you can forget about, because you enjoy the benefits of an online environment. Thins is also called the "dynamic language environment".

+4
source

Different Lisp implementations have different ways to create programs. One of them is to dump the image of the Lisp system memory and write it to disk. On reboot, this image loads at runtime and then starts again. This is pretty common.

It also does SBCL when it saves the executable. Thus, this executable file includes the full SBCL.

Some other implementations create small executable files using images (CLISP), some of them can remove unused code from executable files (Allegro CL, LispWorks), while others create very small programs using compilation in C (mocl).

SBCL has only one simple way to reduce the size of the executable: you can compress the image.

+4
source

Source: https://habr.com/ru/post/1392902/


All Articles