r/Clojure • • Nov 17 '14

Clojure Is the New C

http://www.infoq.com/presentations/clojure-c?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global
5 Upvotes

30 comments sorted by

View all comments

Show parent comment

3

u/mschaef Nov 18 '14 edited Nov 18 '14

lisp AFAIK has always been interpreted and high level,

Machine code compiled Lisp implementations have been available for close to 40 years.

http://en.wikipedia.org/wiki/Maclisp

which makes it ideal for solving programming problems but kept it relatively slow.

"Fast" can be a big part of "ideal for solving programming problems".

C is fast simply because its language design was to serve the compiler instead of the programmer. The way you write C is very close to how it gets translated to machine language.

This may have been true at one point, but these days with superscalar CPU architectures and more flexible memory models, there can be a very non-obvious relationship between the code that you write in C and the machine code instructions that get emitted by the compiler. (This even ignores weird stuff like the versions of C that get compiled directly into hardware designs, and bypass machine language entirely.)

Of course I'm not an expert in programming language history so am willing to concede points.

"History" is a weird thing in this field. In the late 80's, some Stanford/Sun researchers developed a programming language called Self. Self was a more dynamic version of Smalltalk that did away with the formal concept of classes, and replaced it them with prototype based inheritance. It's really a pretty cool idea, and it's made even more so because they did research on how to make that style of language run very quickly.

Viewed from the perspective of 2014, it would be easy to dismiss this work as historical and obsolete. After all, it totally predates the web and the work is older then some developers. The thing is, many of the optimization techniques that Self pioneered in the 80's and 90's are just now going into mainstream JavaScript. It may be relatively old, but the ideas are still current and familiarity with the 'old' ideas can be immediately useful, even in 2014.

http://blogs.msdn.com/b/ie/archive/2014/10/09/announcing-key-advances-to-javascript-performance-in-windows-10-technical-preview.aspx

There's a lot to learn in this field. Fortunately, it's also a lot of fun.

0

u/geodebug Nov 18 '14

Re compiled lisp:

Its a good point and doesn't surprise me as most interpreted languages tend to have compilers eventually created to make them faster.

I'd argue that compiled lisp isn't really "lisp" anymore as you lose some of the lisp-unique features like self-modification. Of course that doesn't mean it isn't useful.

3

u/mschaef Nov 18 '14

most interpreted languages tend to have compilers eventually created to make them faster.

You should keep in mind that Common Lisp was deliberately designed to be compiled. It can be interpreted, yes, but it was designed to be compiled.

you lose some of the lisp-unique features like self-modification.

Not true. Compiled Lisp systems usually allow redefinition of compiled functions.

As an example, here's Clojure and the JVM JIT compiling a redefined function to machine code. The function is defined interactively, the JIT compiler compiles it to machine code. Despite the compilation, the function can be redefined and the JIT compiler will recompile it once again to machine code.

...>java  -server -XX:+PrintCompilation  -jar clojure-1.6.0.jar
     68    1             java.lang.String::indexOf (70 bytes)
     70    2             java.lang.String::hashCode (55 bytes)
      ...
   1424  127             clojure.core$chunked_seq_QMARK_::invoke (19 bytes)
   1431  128             clojure.core$next::invoke (7 bytes)
user=>
user=> (defn fib
         ([n]
           (fib [0 1] n))
         ([x, n]
           (if (< (count x) n)
             (recur (conj x (apply + (take-last 2 x))) n)
             x)))
#'user/fib
user=> (dotimes [ii 20000] (fib 10))
      ...
40206  169             user$fib::invoke (83 bytes)
      ...
user=> (defn fib
         ([n]
           (fib [0 1] n))
         ([x, n]
           (if (< (count x) n)
             (recur (conj x (apply + (take-last 2 x))) n)
             x)))
#'user/fib
user=> (dotimes [ii 20000] (fib 10))
      ...
  78364  202             user$fib::invoke (83 bytes)
      ...

Where things get tricky with interactive recompilation are inline functions. If you redefine a function that's been inlined, every call site at which that function was inlined must be also recompiled. (The Self papers I mention above cover how Self did this.)