• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Performance and Scalability

 
Ranch Hand
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sean,
Thanks a lot for your reply.

So, how about the garbage collection?
 
Sean Corfield
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right, so is it different than Java? or just like Java?
Do programmers have to do something extra for Clojure?
 
Sean Corfield
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
You have cleared a lot of my confusion regarding Clojure.
Shall start to learn this soon.


 
Sean Corfield
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks awesome Sean!
I have bookmarked your blog link and added you in Twitter.

Thank you very much!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic