Why Is Julia So Fast?

Skip to first unread message

Michael Smith

Aug 3, 2014, 6:01:05 AM8/3/14
to julia...@googlegroups.com
I've been following Julia with great interest (and a lot of respect), but one thing that I don't fully understand is why it is so fast. 

For example, let's compare it to R, which is mainly written in C (and a bit of Fortran). On the other hand (according to Wikipedia), Julia's core is implemented in C and C++ (similar to R), its parser in Scheme, and the LLVM compiler framework is used for just-in-time generation of machine code. Scheme is probably not the main reason why Julia is so fast, and whether one uses C or C++ probably does not matter much as well (in terms of speed). 

That leaves LLVM. So what's so special about it? 

If I get R to use LLVM (which is probably not easy, but let's just speak in hypotheticals), could I also get a speed comparable to Julia? 


Jameson Nash

Aug 3, 2014, 8:47:23 PM8/3/14
to julia...@googlegroups.com
I would say that while all of those technologies were certainly crucial to making Julia fast, none of them are magic. There are projects to get other dynamic languages using LLVM's JIT, including Python and R.

So what makes Julia fast?

Language design.

Julia was designed from the beginning to have certain features that are necessary for high performance: type stability, pervasive type inference, execution semantics that closely match the hardware capabilities (pass-by-sharing, machine arithmetic), inlining, and macros.

Additionally, certain oft-requested features were not included which make high performance much more difficult – or impossible – to achieve. These include: pass-by-copy, first class local namespaces (aka eval in function scope). Leaving out other features, such as making everything callable and allowing overloading of the getfield function (a.b access), while not essential to providing performance since they could be similarly optimized, have not been implemented since they might detract from the performance.

Stefan Karpinski

Aug 3, 2014, 9:16:41 PM8/3/14
to Julia Users
The best document explaining Julia's design with respect to performance is this paper. We're also working on another paper that will be more approachable, especially for those with backgrounds in numerical computing. Julia's speed is not due to any one thing, but to the interaction of a number of its features:
  1. an expressive parametric type system, allowing optional type annotation
  2. multiple dispatch using type annotations to select implementations
  3. dataflow type inference, allowing most expressions to be concretely typed
  4. careful design of the language and standard library to allow analysis
  5. aggressive code specialization on run-time types
  6. just-in-time compilation (using LLVM).
Several of these would be hard to introduce into an existing language like R. For example, you could do aggressive type specialization and just-in-time compilation for R, but since R and its libraries weren't designed to make inference effective or easy, it would be difficult to do and might not be as effective as it is in Julia. Many of R's semantics – like lazy evaluation and allowing all values to be NA (including integers) – make efficient implementation challenging. The paper "Evaluating the Design of the R Language" goes through some of the issues that R's design choices cause for its implementation – it's well worth reading.

LLVM is great, but it's not magic. Using it does not automatically make a language implementation fast. What LLVM provides is freedom from doing your own native code generation, as well as a number standard low-level optimizations. You still have to generate good LLVM code. The LLVM virtual machine is a typed register machine with an infinite number of write-once registers – it's easier to work with than actual machine code, but not that far removed (which is the whole point).

On Sun, Aug 3, 2014 at 6:01 AM, Michael Smith <my.r...@gmail.com> wrote:

Isaiah Norton

Aug 3, 2014, 9:43:10 PM8/3/14
to julia...@googlegroups.com
See also the original Julia paper, which covers some these decisions in more detail: http://arxiv.org/pdf/1209.5145v1.pdf

That leaves LLVM. So what's so special about it? 

LLVM moves the goalposts by eliminating a chunk of the necessary work to build a fast language. But having a good low-level optimizer is only one part of a fast language. Semantics matter; it is quite possible to build a slow language with LLVM.

If I get R to use LLVM (which is probably not easy, but let's just speak in hypotheticals), could I also get a speed comparable to Julia? 

In some sense, this is the wrong question. The more pertinent question is: at how much effort (i.e. cost). The example of JavaScript shows that with sufficient effort, a totally crazy language (from a type-stability and runtime-modifiability standpoint) can be made quite fast. But it has taken many years of concerted and expensive effort to get there. Julia is designed to feel "dynamic enough" for interactive use without, as Jameson said, allowing some of the harder-to-optimize things that are possible in other dynamic languages.

Reply all
Reply to author
0 new messages