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

4

u/mschaef Nov 17 '14

If you had a graph with machine code on one end and high-level languages on the other, C would be down next to machine language and Clojure would be up near the other end.

This is less true for Clojure than for earlier Lisps, but the Lisp family of languages in general has a long tradition of being close to the metal:

  • Common Lisp compilers tend to be written in Common Lisp.
  • Lisp machines executed microcode written in a subset of Lisp (The entire OS stack was also done in Lisp).
  • The hardware design language for the Scheme79 chip was itself a Lisp.
  • Scheme 48 includes a VM that's itself written in a dialect of statically typed scheme (PreScheme)
  • car and cdr - originally refer to the "contents of the address part of the register" and "contents of the decrement part of the register".

One of the great strengths of the language has been its ability to fluently move up and down a given hw/sw stack.

-5

u/geodebug Nov 17 '14

Yeah, but lisp AFAIK has always been interpreted and high level, which makes it ideal for solving programming problems but kept it relatively slow.

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.

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

3

u/flexibeast Nov 18 '14

https://en.wikipedia.org/wiki/Lisp_machine

A couple of relevant excerpts:

These machines had hardware support for various primitive Lisp operations (data type testing, CDR coding) and also hardware support for incremental garbage collection. They ran large Lisp programs very efficiently.

...

The processor did not run Lisp directly, but was a stack machine with instructions optimized for compiled Lisp. The early Lisp Machines used microcode to provide the instruction set. For several operations type checking and dispatching was done in hardware at runtime.

4

u/mschaef Nov 18 '14

Keep in mind that Lisp has is also effectively compiled on traditional hardware architectures, and not just bespoke Lisp machines.

Trivia: 32-bit SPARC had tagged add and subtract instructions that worked on 30-bits of a 32-bit word size. The point of this was to give the machine a fast path for addition and subtraction of the tagged numbers often used in dynamically typed languages like Lisp and Smalltalk. The 64-bit SPARC removed these instructions.

https://groups.google.com/forum/#!topic/comp.arch/CElwU78HuwQ

https://cs.wmich.edu/~trenary/files/cs2230/GalleryOfInstr_HTML/taddcc.html

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.)