Elmord's Magic Valley

Software, lingüística e rock'n'roll. Às vezes em Português, sometimes in English.

Chez Scheme vs. SBCL: a comparison

2019-11-14 11:06 -0300. Tags: comp, prog, lisp, scheme, in-english

Back at the beginning of the year, when I started working on what would become Fenius (which I haven't worked on for a good while now; sorry about that), I was divided between two languages/platforms for writing the implementation: Chez Scheme (a Scheme implementation) and SBCL (a Common Lisp implementation). I ended up choosing Chez Scheme, since I like Scheme better. After using Chez for a few months now, however, I've been thinking about SBCL again. In this post, I ponder the two options.

Debugging and interactive development

The main reason I've been considering a switch is this: my experience with interactive development with Chez has been less than awesome. The stack traces are uninformative: they don't show the line of code corresponding to each frame (rather, they show the line of code of the entire function, and only after you ask to enter debug mode, inspect the raise continuation, and print the stack frames), and you can't always inspect the values of parameters/local variables. The recommended way to debug seems to be to trace the functions you want to inspect; this will print each call to the function (with arguments) and the return value of each call. But you must do it before executing the function; it won't help you interpret the stack trace of an exception after the fact.

The interaction between Chez and Geiser (an Emacs mode for interactive development with Scheme) often breaks down too: sometimes, trying to tab-complete an identifier will hang Emacs. From my investigation, it seems that what happens is that the Chez process will enter the debugger, but Geiser is unaware of that and keeps waiting for the normal > prompt to appear. Once that happens, it's pretty much stuck forever (you can't tab-complete anymore) until you restart Chez. There is probably a solution to this; I just don't know what it is.

As I have mentioned before, Chez has no concept of running the REPL from inside a module (library in Scheme terminology), which means you can't call the private functions of a module from the REPL. The solution is… not to use modules, or to export everything, or split the code so you can load the module code without the module-wrapping form.

By contrast, SBCL works with SLIME, the Superior Lisp Interaction Mode for Emacs. SLIME lets you navigate the stack trace, see the values of local variables by pressing TAB on the frame, you can press v to jump to the code corresponding to a stack frame (right to the corresponding expression, not just the line), among other features. Common Lisp is more committed to interactive development than Scheme in general, so this point is a clear win for SBCL.

(To be fair, Guile Scheme has pretty decent support for interactive development. However, Guile + Geiser cannot do to stack traces what SBCL + SLIME can.)

Execution speed

In my experience, SBCL and Chez are both pretty fast – not at the "as fast as hand-crafted C" level, but pretty much as fast as I could desire for a dynamically-typed, garbage-collected, safe language. In their default settings, Chez actually often beats SBCL, but SBCL by default generates more debugger-friendly code. With all optimizations enabled, Chez and SBCL seem to be pretty much the same in terms of performance.

One advantage SBCL has is that you can add type annotations to code to make it faster. Be careful with your optimization settings, though: if you compile with (optimize (safety 0)), "[a]ll declarations are believed without assertions", i.e., the compiler will generate code that assumes your types are correct, and will produce undefined behavior (a.k.a. nasal demons) in case it is not.

Startup time and executable size

This one is complicated. In my machine, Chez compiles a "hello world" program to a 837-byte .so file, which takes about 125ms to run – a small but noticeable startup time. A standalone binary compiled with chez-exe weighs in at 2.7MB and takes 83ms – just barely noticeable.

As for SBCL, a "hello world" program compiles to a 228-byte .fasl file, which runs in 15ms, which is really good. The problem is if the file loads libraries. For instance, if I add this to the beginning of the "hello world":

(require 'asdf)        ;; to be able to load ASDF packages
(require 'cl-ppcre)    ;; a popular regex library

…now the script takes 422ms to run, which is very noticeable.

SBCL can also generate standalone executables, which are actually dumps of the whole running SBCL image: you can load all the libraries you want and generate an executable with all of them preloaded. If we do that, we're back to the excellent 15ms startup time – but the executable has 45MB, because it contains a full-fledged SBCL in it (plus libraries). It's a bit of a bummer if you intend to create multiple independent command-line utilities, for example. Also, I guess it's easier to convince people to download a 2.7MB file than a 45MB one when you want them to try out your fancy new application, though that may not be that much of a concern these days. (The binary compresses down to 12MB with gzip, and 7.6MB with xz.)

Another worry I have is memory consumption (which is a concern in cheap VPSes such as the one running this blog, for instance): running a 45MB binary will use at least 45MB of RAM, right? Well, not necessarily. When you run an executable, the system does not really load all of the executable's contents into memory: it maps the code (and other) sections of the executable into memory, but they will actually only be loaded from the disk to RAM as the memory pages are touched by the process. This means that most of those 45MB might never actually take up RAM.

In fact, using GNU time (not the shell time builtin, the one in /usr/bin, package time on Debian) to measure maximum memory usage, the SBCL program uses 19MB of RAM, while the Chez program uses 27MB. So the 45MB SBCL binary is actually more memory-friendly than the 2.7MB Chez one. Who'd guess?

Available libraries

Common Lisp definitely has the edge here, with over a thousand libraries (of varying quality) readily available via Quicklisp. There is no central repository or catalog of Chez (or Scheme) libraries, and there are not many Chez libraries that I'm aware of (although I wish I had learned about Thunderchez earlier).

[Addendum (2019-11-16): @Caonima67521344 on Twitter points out there is the Akku package manager for Chez and other Schemes.]

The language itself

This one is a matter of personal taste, but I just like Scheme better than Common Lisp. I like having a single namespace for functions and variables (which is funny considering I was a big fan of Common Lisp's two namespaces back in the day), and not having to say funcall to call a function stored in a variable. I like false being distinct from the empty list, and for cdr of the empty list to be an error rather than nil. I like Scheme's binding-based modules better than Common Lisp's symbol-based packages (although Chez modules are annoying to use, as I mentioned before; Guile is better in this regard). Common Lisp's case insensitivity by default plus all standard symbols being internally uppercase is a bit annoying too. Scheme has generally more consistent names for things as well. I used to dislike hygienic macros back in the day, but nowadays, having syntax-case available to break hygiene when necessary, I prefer hygienic macros as well.

And yet… Common Lisp and Scheme aren't that different. Most of those things don't have a huge impact in the way I code. (Well, macros are very different, but anyway.) One things that does have an impact is using named let and recursion in Scheme vs. loops in Common Lisp: named let (similar to Clojure's loop/recur) is one of my favorite Scheme features, and I use it all the time. However, it is not difficult to implement named let as a macro in Common Lisp, and if you only care about tail-recursive named let (i.e., Clojure's loop/recur), it's not difficult to implement an efficient version of it in Common Lisp as a macro. Another big difference is call/cc (first class continuations) in Scheme, but I pretty much never use call/cc in my code, except possibly as escape continuations (which are equivalent to Common Lisp's return).

On the flip side, Common Lisp has CLOS (the Common Lisp Object System) in all its glory, with generic functions and class redefinition powers and much more. Guile has GOOPS, which provides many of the same features, but I'm not aware of a good equivalent for Chez.


As is usually the case when comparing programming languages/platforms, none of the options is an absolute winner in all regards. Still, for my use case and for the way I like to program, SBCL looks like a compelling option. I'll have to try it for a while and see how it goes, and tell you all about it in a future post.

Comentários / Comments (6)

Michał "phoe" Herda, 2020-04-15 18:18:44 -0300 #

SBCL is capable of core compression. This usually brings the core from 45MB to around 12-13 MB.

Brit Butler, 2020-04-15 18:30:46 -0300 #

As someone on the Common Lisp side (though not very active these days) but often wondering what life is like on the other side of the pond (esp Guile, Chez, and Gerbil) it's nice to read a balanced and thoughtful write-up like this one. I think I love the deep image-based interactive nature of CL even more than I love macros and speed of generated code so it's especially good to know that those pieces are a bit spotty in Guile and Chez-land. However your SBCL adventures turn out, I hope you have fun. Happy hacking!

Vítor De Araújo, 2020-04-16 05:05:16 -0300 #

@Michał "phoe" Herda: That's true. But then startup time increases (from ~15ms to ~200ms on my system) and so does memory consumption (because now it has to touch all pages to decompress them). But it's a tradeoff that may make sense in some circumstances.

@Brit Butler: Thanks! :) If you want to try a Scheme with reasonable support for interactive development and don't care too much about execution speed, I would recommend giving Guile + Geiser a go. It's not as good as the SBCL + SLIME combo, but maybe worth a try if you want to have a taste of what programming in Scheme is like. I don't know Gerbil, though, so I don't know how it fares against Guile and Chez.

Dick Chez, 2020-04-18 06:15:04 -0300 #

Oh god I hate Scheme modules, precisely because they're basically static as far as the REPL is concerned. I encountered them in Racket, where they're every bit as obnoxious as they are in Chez, and this was true even before Chez-based Racket was a thing.

Vítor De Araújo, 2020-04-18 07:07:33 -0300 #

@Dick Chez: But in Racket at least you can run `(enter! modulename)` to run code from inside the module environment if I remember correctly, right? In Chez you don't even have that.

Guile's module system, on the other hand, is on par with what you see in Common Lisp land.

John Cowan, 2020-12-09 15:39:33 +0100 #

I solve Scheme library-debugging issues in a very simple and portable way: there is a foo.{sls,scm,sld} file that defines imports and exports, and a foo-impl.scm file that contains actual code to be included by the library file. R7RS provides `include` as a built-in; for R6RS I just package it as a trivial library.

Then in order to debug a library I don't import the library at the REPL, but instead load/include the implementation file. That gives me access to all procedures, including the ones that aren't exported. This also has the advantage that I can wrap the same code with different module declarations: for R6RS, for R7RS, for native Chicken, for native Guile, etc. (Fortunately, all of these use different naming conventions for mapping a library name to the filesystem.)

One thing I'd like to have is hooks into a REPL that let me generate test files from REPL noodling, something like this (where lines beginning with commas are REPL commands):

> ,open-tests "foo"
> (eq? (car '(a b c)) 'a)
> ,ok
> (eq? (cdr '(a b c)) 'a)
> ,ok
> (car 'a)
> ERROR: domain error
> ,error
> ,close-tests

This has the effect of writing three tests into the test file, one that tests for a true result, one that tests for a false result, and one that tests for an error. There is no one unit-test library for Scheme yet, though.

Deixe um comentário / Leave a comment

Main menu

Posts recentes

Comentários recentes


em-portugues (213) comp (139) prog (69) in-english (52) life (47) unix (35) pldesign (35) lang (32) random (28) about (27) mind (25) lisp (23) mundane (22) fenius (20) web (18) ramble (17) img (13) rant (12) hel (12) privacy (10) scheme (10) freedom (8) esperanto (7) lash (7) music (7) academia (7) copyright (7) bash (7) home (6) mestrado (6) shell (6) conlang (5) emacs (5) misc (5) php (4) latex (4) editor (4) etymology (4) android (4) book (4) politics (4) worldly (4) wrong (3) c (3) kbd (3) tour-de-scheme (3) security (3) film (3) network (3) cook (2) poem (2) philosophy (2) comic (2) treta (2) wm (2) lows (2) llvm (2) physics (2) perl (1) golang (1) translation (1) en-esperanto (1) audio (1) german (1) kindle (1) old-chinese (1) pointless (1)


Quod vide

Copyright © 2010-2021 Vítor De Araújo
O conteúdo deste blog, a menos que de outra forma especificado, pode ser utilizado segundo os termos da licença Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International.

Powered by Blognir.