aspose file tools*
The moose likes Clojure and the fly likes Performance and Scalability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Performance and Scalability" Watch "Performance and Scalability" New topic
Author

Performance and Scalability

Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Hi Amit,
As I read, that, the Clojure program will convert into Java Byte Code and then will be executed inside JVM.
So, will that be a bit slower than Java?
How will you scale a big enterprise model using Clojure? Any coding guidelines?


Thanks and Regards,
~Sujoy
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

Sujoy Choudhury wrote:As I read, that, the Clojure program will convert into Java Byte Code and then will be executed inside JVM.
So, will that be a bit slower than Java?

Java works the exact same way: Java source code is compiled to Java bytecode and executed inside the JVM. Since the Clojure language is much smaller / simpler than the Java language, the Clojure compiler is much faster than the Java compiler. But I don't think you're really meaning that part of the process

Performance is a question that comes up quite often on the Clojure mailing list and folks usually jump in to show how to create an idiomatic version of the code that runs at "native" speed compared to the (closest) equivalent Java. It's easy to write slow Clojure code tho' because naive implementations are often doing a lot more work than they need to, but in general, there's no real reason why any particular Clojure program should be slower than some Java equivalent.

The way I integrate Clojure with Java is via the clojure.lang.RT (runtime) class. I actually load the Clojure source code files at application startup which contain the public API to my Clojure codebase (the Model in my MVC web application). Clojure then compiles those .clj on demand to bytecode and as the application runs the hotspot compiler in the JVM will further optimize the bytecode and replace parts with native code. For my use case, the compilation hit is small enough not to matter and the benefit is that I can re-run my loader method at any time to pick up changes in my Clojure codebase without needing to stop the application or recompile anything. If I was feeling really adventurous, I could incorporate the REPL into my application and then we could actually connect into the live, running application from any machine and inspect the Clojure model directly (and even hot swap code for critical fixes etc).

How will you scale a big enterprise model using Clojure? Any coding guidelines?

One useful thing is (set! *warn-on-reflection* true) so that you can identify - and fix - any situations in your code where Java reflection is being used. Otherwise, just regular profiling like you'd do in Java if you think you have a performance problem... People are already processing massive data sets very successfully in Clojure (according to what's being posted on the Clojure mailing list).

If you're more concerned with structural scaling of the code, from an organizational point of view, Clojure has namespaces which allow code to be organized into packages, much like Java code usually is. The more compact nature of Clojure tends to lead to simpler namespace structures than the "equivalent" package structures you'd typically see in Java (in my opinion).
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Hi Sean,
Thanks a lot for your reply.

So, how about the garbage collection?
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

Sujoy Choudhury wrote:So, how about the garbage collection?

Could you elaborate on that question? What do you think will be different between GC in a Java program and GC in a Clojure program?
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

right, so is it different than Java? or just like Java?
Do programmers have to do something extra for Clojure?
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

Sujoy Choudhury wrote:right, so is it different than Java? or just like Java?
Do programmers have to do something extra for Clojure?

As far as the JVM is concerned, it doesn't care what language was the original source of the bytecode it runs. You can have an application written in any combination of languages that compile down to bytecode and it'll all look like one application to the JVM. So in that respect, Clojure is "just like" Java. I have three languages in my current application - none of them Java - and it all just works without needing to do anything special beyond having the necessary libraries on the classpath.

That said, every application has different runtime characteristics and you may need to tune the JVM differently for each application or even for each new release of the same application (if a lot of new functionality was added). So it's certainly possible that the "same" application written in Clojure would need different JVM settings to those needed for that application written in Java - but that is also true for different styles of programming even within Java, e.g., whether the code relies on lots of short-lived objects or whether the code has or generates a large number of classes or any number of differences, since those all affect how the JVM uses memory and how GC will be affected.
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Thanks a lot.
You have cleared a lot of my confusion regarding Clojure.
Shall start to learn this soon.


Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

Glad that helped!

A blog post of mine might help you get a sense of just how easy the interop is between Clojure to Java: writing a log4j appender in Clojure

This is an example of using a Java library (log4j) from Clojure (via the clojure.tools.logging library) with a custom log appender (called by Java code in log4j) that is actually written in Clojure!
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

That looks awesome Sean!
I have bookmarked your blog link and added you in Twitter.

Thank you very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performance and Scalability