Simple things in Lisp

This post is more a note to myself, so that I shouldn’t forget some interesting things, but perhaps trivial, that I came across this week. It ranges from those kind of things you sometimes bump into them and then forget, to the those you seldom use and suddenly find them cool. Instead of writing it in a post-it or a piece of paper (even if made of bits instead of paper), I’ve decided to write it here.

  • SBCL available virtual address space at startup
  • The experiments I do as part of my research work I run them in the cluster of my research group. This week, it was introduced some usage limits and one of them was precisely the available memory a job can use. So suddenly, all my jobs couldn’t run because they were trying to allocate 8GB (the new limit was 2GB). At first I found it strange since a single job when running didn’t consume more than 120MB. Well, thanks to my colleague Tiago and some googling, I’ve learned that SBCL in a 64bit system allocates 8GB although it might not use them. The solution to the problem turns out to be easy: start SBCL with the option -dynamic-space-usage with the amount of space you wish in megabytes. In my case, something below the 2GB limit. And then around 250 jobs were launched!

  • SBCL optimization notes: doing signed word to integer coercion
  • If there is anything I would really love to see/have is a good tutorial/manual of how to optimize Lisp code, specifically in SBCL. Recently I did some code optimization of a crucial function in my Ant System but is not easy. Although I was able to speed it up a lot in that function, some of the things I tried I ended up removing since it provoked some unstable behavior, i.e., it crashed. Moral of the story, it’s not easy to solve all the compiler notes about optimization. This week, while trying to optimize a bit a mandelbrot program, I came across the following:

    ; note: doing signed word to integer coercion (cost 20), for:
    ; the first argument of CHECK-FIXNUM

    To me it was a little strange note since, in theory, everything was properly declared and was nothing fancy. In short, it was just an (incf x) with x declared as fixnum. Well, the note says it but only after some looking around I realized that by using (safety 1) the compiler will insert a type verification, hence the CHECK-FIXNUM in the note. Changing the safety level to 0, the verification is eliminated. Of course, you need to be sure that will always stay within the range of a fixnum, otherwise it will crash. One more little things that is good to be aware when optimizing Lisp code. I still wish more documentation about these optimization notes would exist.

  • Local macros
  • I must confess that local macros is something I’ve barely used in my code. Yes, I know them. I’ve seen examples, etc, etc, but well, in actual programs of mine, I don’t remember using them. Again, this week, they simply decided to knock on my door. Macros are very useful for many things, one of them being to abstract boilerplate code. So, local macros are also cool for this. To keep it very simple, imagine that you have two methods, one specialized in lists and the other in arrays. Now, you want to access the list/array and do some complicated stuff. That stuff is the same except the part where you access the list and the array because for the list you use nth and for the array aref. The difference between them the arguments order: (nth index list) versus (aref array index). So, you can do this:

    (defmethod do-stuff ((sequence array))
       (macrolet ((access-position (position)
    		 `(aref sequence ,position)))
          (do-stuff-macro))))
    
    (defmethod do-stuff ((sequence list))
       (macrolet ((access-position (position)
    		 `(nth ,position sequence)))
          (do-stuff-macro))))
    


    In short, you define in each method a local macro through macrolet, access-position, that is in turn used by the macro do-stuff-macro. In this simple way, you can develop the common body of the methods in a single place (the do-stuff-macro) and the different things are managed by the dispatch (the sequence type, list and array) and the local macros (the sequence access defined by macrolet). Simple but very cool!

2 thoughts on “Simple things in Lisp

    1. Hi,

      Thanks for all this material. Quite useful and interesting indeed :-)

      Cheers,

Leave a Reply to safdsa Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: