r/programming • • Apr 13 '14

Puppet Labs move to Clojure

http://puppetlabs.com/blog/new-era-application-services-puppet-labs
101 Upvotes

30 comments sorted by

16

u/strattonbrazil Apr 13 '14

I wish they mentioned performance a bit more. From my personal experiences and almost every benchmark I've ever seen clojure struggles and seems laborious to optimize unless you have a lot of experience with it. I guess it might be comparable to ruby, so it didn't affect them. In my brief use of clojure, the language was very simple and the docs were very easy to use.

9

u/yogthos Apr 13 '14

Could you elaborate on what benchmarks these are exactly. Clojure seems to do pretty well in both the benchmarks game and in the web framework benchmarks.

I'm also not sure what makes Clojure laborious to optimize. Most optimizations tend to revolve around having reflection and Clojure will warn you about functions that are causing reflection when you set the *warn-on-reflection* flag. On top of that the JVM comes bundled with jvisualvm that can be used to easily profile your application and see what actually needs optimizing.

While, without optimizations, the performance is not going to match Java, it's certainly a lot better than Ruby or Python out of the box.

13

u/sdfsfsklfjslk Apr 13 '14 edited Apr 13 '14

Clojure does well on the web frameworks benchmark because there's not actually that much Clojure involved. http-kit and most of the libraries in the compojure example are just tiny wrappers around big Java libraries.

The Clojure benchmarks game entries have also been heavily optimized, and they're still slower, more verbose, and use more memory than the Java equivalents. Clojure might be nice, but idiomatic Clojure is definitely not that fast or memory efficient.

5

u/Tekmo Apr 14 '14

To be fair, that trick is true for a lot of languages in those benchmarks, which have standard libraries that wrap C code.

2

u/igouy Apr 14 '14

That "trick" is true for a lot of languages.

"Lua is a tiny and simple language, partly because it does not try to do
 what C is already good for, such as sheer performance, low-level operations,
 or interface with third-party software. Lua relies on C for those tasks."

4

u/joequin Apr 13 '14 edited Apr 14 '14

You can say idiomatic Clojure isn't fast, but that doesn't mean anything without telling it's what it's slower than. It's still significantly faster than ruby. If you're comparing to java, then it sounds like java isn't even in their consideration when you take into account that they usually use ruby.

Then there's one of Clojure's main benefits. It's often trivial to multithread and as a result you'll multithread things that might not have been worth your time to multithread in other languages including scala.

7

u/yogthos Apr 13 '14

Clojure does well on the web frameworks benchmark because there's not actually that much Clojure involved. http-kit and most of the libraries in the compojure example are just tiny wrappers around big Java libraries.

That's part of the advantage of being hosted on the JVM. You can wrap Java libraries when you need to and get decent performance. The actual application code can be written using idiomatic Clojure and the overall performance will still be plenty fast.

The Clojure benchmarks game entries have also been heavily optimized, and they're still slower, more verbose, and use more memory than the Java equivalents. Clojure might be nice, but idiomatic Clojure is definitely not that fast or memory efficient.

Idiomatic Clojure certainly isn't as fast or memory efficient as Java, but nobody is arguing that. However, when you compare it to other dynamic languages like Ruby or Python there's a big difference to be seen.

For the vast majority of the code in any application raw performance is not all that interesting in any case. The portions of the code that are actually the bottleneck can be optimized or even written in Java if necessary. However, during the 3 years that I've been developing Clojure professionally I haven't had a reason to do any such optimizations yet.

2

u/igouy Apr 14 '14

Have the "Java equivalents" been "heavily optimized'?

1

u/strattonbrazil Apr 13 '14 edited Apr 13 '14

The scores on the benchmark game seem to have improved a lot since I last looked. However, that seems to be an anomaly. I've heard the benchmark game is slightly flawed, because the tests are so short lived and don't take into account JVM startup. Could be wrong.

These are the kind of tests I've seen the in past, where clojure is an order of magnitude slower than java. If things have really changed that much for it, which is certainly possible, maybe it's time for me to give it another chance.

7

u/bcash Apr 13 '14

That article is mainly talking about the same things as you mention: start-up speed.

And it's true. Clojure apps do have a start-up penalty worse than Java (because they have to load the JVM as well as a whole JAR file full of the Clojure runtime, etc.), there are some mitigations though, and this shouldn't detract from Clojure's use in services (like the stuff mentioned in the original article).

1

u/igouy Apr 14 '14

short lived and don't take into account JVM startup

How much difference do you think that would make for those tiny programs with those workloads?

What about Java® VM warm-up?

11

u/grim_radical Apr 13 '14

The first project we did in Clojure was PuppetDB, which ended up being significantly (1-2 orders of magnitude) faster than the comparable ruby code without really any effort on optimization. Comparisons like this are always fraught with peril, but it really wasn't much of a contest in our experience. And once you add in the ability to safely and trivially parallelize various algorithms...

For optimization, we use standard JVM profiling tools (JMX, Yourkit, Eclipse MAT, etc) and common sense. :) Definitely avoid reflection, using type hints where necessary. Lazy sequences can complicate analyzing performance. I highly recommend no.disassemble if you want to see what the bytecode for a given clojure function looks like. And of course, in the worst case you can always drop down to Java.

There are of course faster languages out there, but Clojure's abilities in terms of speed, expressivity, libraries, and immutability really spoke to us.

3

u/blufox Apr 13 '14

Are you rewriting puppet? Or is this only for newer products and services? Any expected time frame if it is a rewrite?

21

u/Plorkyeran Apr 13 '14

Even if Clojure was consistently half the speed of the equivalent code in Java, it'd still be a huge step up in performance from Ruby.

3

u/lacosaes1 Apr 13 '14

If they're going to rewrite something for performance, why don't pick a language with a very good performance on the JVM like Scala?

12

u/grim_radical Apr 13 '14

We'd already written PuppetDB in Clojure a while ago, and thus have several years' experience with the language. It's been fast enough and practical enough for our needs, and a pleasure to use. This is not a knock against Scala, which is also a great language.

7

u/lacosaes1 Apr 13 '14

... and thus have several years' experience with the language.

Fair enough. That's a big reason.

4

u/roybatty Apr 13 '14

The article didn't mention anything about performance really (except the JVM itself). And besides that what makes you think that Clojure doesn't have good performance?

-7

u/lacosaes1 Apr 13 '14

They're abandoning Ruby because its performance is shit so yeah, performance is the big factor here. Now what I'm saying is that you're rewriting shit because you need performance and you're picking the JVM, why not rewrite it in Scala?

6

u/roybatty Apr 13 '14

The programming language we chose for PuppetDB is Clojure, and we’ve been moving towards adopting Clojure for most of our new back-end projects.

I'm assuming they took a look at Scala, didn't like it, like Clojure as a language, and Clojure performance is within their performance requirements. They can always dip down into Java anyway if they need to squeeze out some more performance.

3

u/vivainio Apr 13 '14

It doesn't seem like they are rewriting stuff, but rather picking up Clojure for new projects. It's a ruby shop, so speed is probably not a big concern for them in the first place.

0

u/s_m_c Apr 14 '14

JRuby has good performance on the JVM and will improve further with the maturing of invoked dynamic.

-2

u/revscat Apr 13 '14

But not JRuby. I'd be curious to know what made them decide to go with Closure rather than JRuby, especially given that Puppet is primarily a Ruby shop.

17

u/TJSomething Apr 13 '14

1

u/[deleted] Apr 14 '14

Which points to some issue in the Ruby object/dispatch model that causes this slowness. Can't blame JRuby's performance on GC or threads in the case of JRuby.

4

u/lynxbat Apr 13 '14

They do use JRuby for their Razor project via Torquebox

2

u/[deleted] Apr 14 '14

It's true that Clojure is a bit slower than Java, but it is more than fast enough for most people. If speed was absolutely key for most people, we'd still be using C and Java for everything, instead of Python and Ruby (which Clojure tends to outrun, excepting the C written libraries with Python/Ruby bindings).

Also tuning isn't that hard. Add type hints!

3

u/[deleted] Apr 13 '14

I wonder if they are using Incanter? I am fascinated by being able to do statistical operations on the JVM.

1

u/esanchma Apr 14 '14

but... pallet has been around since 2010, and it's awesome.

-2

u/subreddit_as_hashtag Apr 13 '14

This is great news. Maybe now I'll use Puppet.