14

I have an interesting architectural question regarding an application that I am developing using both Clojure and Java. The application involves a lot of intensive, concurrent data processing tasks that need to be orchestrated.

Here's the rationale for mixing both Clojure and Java:

  • Java is needed for some pretty CPU-intensive numerical code, where I need to optimise the algorithms to run as fast as possible on the JVM. Clojure can't quite achieve this yet, and such code would not be very idiomatic in Clojure because the algorithms require a lot of mutable data for performance reasons.
  • Clojure is (IMHO) far better for orchestrating the overall flow of the application, with its excellent support for functional programming, interactive dynamic development at the REPL and concurrency features.

Given that I'm using both languages - what logic or principles should I apply to determine the dividing line between the two? In particular, I'm interested in how to design an API/interface that would be at the right kind of level to take advantage of the relative strengths of both languages.

5
  • I am a bit surprised by your first statement: in my experience doing numerical code is perfectly suited for Clojure but doesn't lend itself very well to an OO approach. As far as the performance goes: did you really measure the difference? Type hinting in Clojure will get you very close to Java performance. I also don't understand the need for mutable data for performance reasons. Maybe you can give as an example here? Commented Jan 21, 2011 at 13:13
  • @Mauritz - yes I measured the difference! type hinting didn't get me quite close enough (boxing of primitives is the big killer, but there were also some other overheads I couldn't figure out - maybe I will revisit in Clojure 1.4 :-) ). I need mutability because I have to manipulate lots of temporary working data. a[i]=a[j]+x is very fast in Java, certainly at least an order of magnitude faster than updating any immutable data structure (which implies object allocation and gc overhead). Commented Jan 21, 2011 at 15:07
  • It doesn't really answer your question but you could also consider doing a lot of the numerical side in C++/C and JNI it from Clojure Commented Jan 21, 2011 at 21:47
  • @justinhj - yes I would have liked to do that, the problem is I also have a requirement to target the JVM platform so I have to stick with a JVM-language solution. Luckily my testing showed that Java and C++ came extremely close in performance for this kind of code. Commented Jan 27, 2011 at 10:08
  • Out of curiosity, did you ever try reimplementing this in Clojure? I'm specifically curious if you looked into transients for mutability purposes. Commented Aug 1, 2012 at 18:11

2 Answers 2

10

Without commenting on your perception of the relative advantages of Java and Clojure, and assuming that you did at least some micro-benchmarking to validate that the assumption has some chance of being correct, then the correct approach would seem to be to leave Java only for the parts that require optimization.

The classes responsible for the numeric code and calculation should be written in Java, and everything else in Clojure. I would even take a more aggressive approach and just design the classes to be distinct so that they could be written in Java, but actually write them in Clojure and rewrite them in Java if performance proves to be a problem.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks Yishai - good ideas! along with some other experimenting, I'm converging on this as the best solution
2

Clojure does a good job of helping developers get most of their part in simple functional style, and isolating mutational work into confined areas.

I would apply the same guidelines here: isolate java code as much as you can, as you would do as much of your clojure code in "pure functional style". So the java island would be as small as possible given your constraints, and access to the java island would go through a small set of clojure functions.

Not sure this helps much, but anyway !

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.