Focus on Java, this Tuesday


This Tuesday is JFokus in Stockholm, where I will talk about Java, JRuby and RSpec and how to combine these in exciting ways. Let me tell you, I look forward to speaking about this. I believe there is something incredibly useful in combining technologies like this.

Of course, it would also have been great fun to take about where JRuby is at right now, since there is so much cool stuff going on at the moment. When the 0.9.3 release comes along, I bet many people will be surprised and as excited as I am about this. I will get back with more information what has happened in the codebase lately quite soon.

If you’re going to JFokus, please say hi if you see me!



Advice


I usually don’t like to use this blog for strictly personal reasons, but I will do an exception for once, to get advice from y’all.

Hypothetically, I would like to know what kind of work opportunities I should look for, if I was in a situation where my current work situation was alright, but not satisfactory (since I’ve been at the same place for 6 years.) Further, I’m interested in getting away from Sweden. The places I would most likely want to be situated in would be SF or London.

The picture is complicated by my lack of academic degrees. The kind of experience I have is strictly professional and recreational. (In which category should I put my work on JRuby?). Apart from that, my interests are in the region of work where I can use different languages, and will not be constrained by Java. I haven’t done consulting, and maybe I should do that. On the other hand, I’m not sure that is the right thing for me either.

So, please. I need advice. What should I do with my professional life at this moment?



YARV tail call optimization in JRuby


After some further developments on the YARVMachine, I can report that JRuby trunk supports most of the optimized instructions that YARV makes use of. The big thing missing is the local cache optimization. But that is just an aside from the main course.

Because right now there is support in JRuby’s YARVMachine to handle a limited form of tail call optimization. Specifically, if the last call in a method is to the method we’re in and the receiver of that call is the same as the current self, tail call optimization will happen. That allows JRuby to run this script:

def fact(n, acc)
if n == 0
acc
else
fact n-1, n*acc
end
end

p fact(25000, 1)

That will explode in both YARV and MRI, long before getting anywhere, since the stack goes out. But this will work correctly in JRuby with -y, provided that the tail call optimization is actually turned on. To turn it on, use $JAVA_OPTS and set the variable jruby.tailcall.enabled to true, like this:

JAVA_OPTS="-Djruby.tailcall.enabled=true"
jruby -y testRecFact.rbc

Of course, you need latest trunk for this to work, and you need to compile the YARV file as described in my previous post. But provided you have done that, the above will actually work perfectly, without deepening the stack at all. The reason this is not turned on by default in the YARVMachine is that it isn’t completely safe yet. There is one small kink, which is that if you redefine the current method within the current method, and then call self, this will not work as expected. It should be easy to add checks for this behavior, though, so that will come soon.

Of course, the big problem with something like this is the stack trace. How to handle it? The implementation behavior would make it look like those 25000 calls to fact was just a single one. In the same manner, trace methods won’t get called.

But trunk will actually report when these tail calls have been done. Say you have a script like this:

def a(n)
if n > 20000
raise "hell"
end
a(n+1)
end

a(0)

(it is designed to explode at a certain depth, of course.) In current JRuby trunk, executed with -y, this will generate an output that looks like this:

testRaise.rb:-1:in `a' TAIL CALL(20001): hell (RuntimeError)
from testRaise.rb:-1

So it is quite obvious that the exception was raised in the 20001:th invocation of ‘a’.

This work goes forward very fast, and it’s funny to see progress happen this quickly. Yesterday SASADA Koichi visited the #jruby and #rubinius IRC channels, and we had some very interesting discussions on further collaboration. The future looks bright indeed, and this weekend I’ll probably have time enough to take a first crack at a YARV compiler for JRuby.



Executing YARV (in JRuby)


I guess the title says it all. Oh, it doesn’t? You want what? A tutorial on how to play with this? Well, alright then.

What you will need to get started is first and foremost JRuby trunk. Download and build that. You will also need either a very recent version of YARV or Ruby trunk. Download either and build that too. I have the source in $YARV_SRC and $RUBY_TRUNK_SRC. I’ve also installed them with suffixes, so I have ruby-yarv and ruby-trunk globally available. That’s a quite good setup.

Before compiling some Ruby, we need to change the change the compile-script slightly. So open up the file in $YARV_SRC/tool/compile.rb and disable the peephole_optimization and inline_const_cache, since JRuby doesn’t really support these features yet. If you use Ruby trunk, you also need to do a global search-and-replace in this file, from YARVCore to VM.

Now you’re finished to compile something. The goal for this execution have been to get an iterative fibonacci sequence running, so save this code in a file called fib.rb:

def fib_iter_ruby(n)
i = 0
j = 1
cur = 1
while cur <= n
k = i
i = j
j = k + j
cur = cur + 1
end
i
end

puts fib_iter_ruby(5000)

Next, compile this file:

ruby-yarv $YARV_SRC/tool/compile.rb -o fib.rbc fib.rb

Modify as needed if you use Ruby trunk instead. Now, how to run this? With the y-flag to JRuby:

jruby -y fib.rbc

As you can see, simple files like this just work. Of course, there are much missing yet, but basic interpretation works really nice. Some more additions to the YARV machine will come quite soon, probably. But the next step will be to implement a compiler for YARV too. From that point it should be possible to execute Ruby-files in JRuby in several different ways; by regular interpretation, YARV compilation, YARV interpretation, Java bytecode AOT compiling, Java bytecode JIT compiling and also possibly Rubinius interpretation. The future is, as always, interesting.



Rubinius and Lisp


Yesterday on the #rubinius-channel, me, Evan and MenTaLguY had the beginnings of a discussion about a Lisp dialect for the Rubinius VM. Since this sound exactly like the kind of thing I’ve been thinking about for some time, I got excited. But anyway, Pat Eyler covered the issue much better and did a small interview with us that can be read here. Ruby is wonderful. Rubinius is sweet. With Lisp added to the mix, it will be better.

I’ll probably follow this up with some more information and ideas, as soon it’s fleshed out.



That Rails Thing


I need to join the fray. Pat Eyler has announced a competition together with APress. The first installment is about how Rails has made me a better programmer. Details to be found here.

So, let’s get at it. Rails has inspired and affected me in numerous ways. You got to realize that I have been programming web applications in Java for way too many years. Before that I’ve done both ASP and PHP. Nothing ever felt natural. I’ve tried many of the LISP frameworks (and Uncommon Web is really cool). I’ve embraced continuation-based frameworks. But none felt so powerful, yet nonintrusive. Rails embodies the best parts of Ruby.

For me, Rails is very interesting for several reasons. First and foremost is the fact that it is an incredibly good framework. It’s really amazing how good it is. And that in itself acts as an inspiration. To know that software can be this good makes me want to strive to perfect my API’s, make my libraries more usable, and finding new and novel ways to improve my DSL’s.

But that’s only part of it. The flip side of this is that Rails is not perfect. In fact, there are myriad ways it can be improved. And that gives me hope, because it also means that no matter how good my code gets, it can always get better. And that means I’ll never be totally bored!

In a very literal sense, Rails has made me a better programmer in another way. Since Ruby on Rails was the first Ruby-application to make it into Karolinska Institutet, it means I can thank Rails for being able to write more Ruby at work. And writing Ruby instead of Java must make you a better programmer, neh?

Of course, Rails is an excellent testing tool for seeing how far we have left with JRuby. It goes without saying that you become a better programmer by implementing a language…

I have several applications and libraries that I keep in mind when writing my code. Those libraries act as a gauge against which I measure my code. Code quality, testing ability, interface, sheer trickyness; in all these areas, Rails is one of the top frameworks I know.



Five Things.


Oh well. It has finally hit me, as I knew it would sooner or later. Charles O Nutter managed to nail me.

If you don’t know what this is, it’s called the “5 Things Meme” and it means I’ll tell you 5 things about me that you probably don’t know, and then pass the baton on to 5 other people. We’ll see about that.

  • I am an “academic failure” in that I haven’t even finished the Swedish equivalent to High School (in Swedish it’s Gymnasium). There is no thing about computers I have learned in school. And even though I have no education, I actually work centrally at Karolinska Institutet, which is one of the more esteemed universities in Sweden.
  • I am an amateur musician. I play guitar, some piano, sing quite a lot and play a few other instruments (but not very good). I create some music, but nothing that has been finished enough to make public. Until recently I was a singer in a Swedish barbershop chorus called the Entertainmen. We came 14:th on the World Barbershop Competition in Salt Lake City 2 years ago. I used to sing Lead, but switched to Bari.
  • I train martial arts. I have been doing so for most of my life, but not with enough regularity to attain a black belt yet. What I’ve been training for the last few years is called Bujinkan Budo Taijutsu, and it seems I will stay with it.
  • I am a vegetarian, and has been for the past 10 years. I don’t eat meat, fowl or fish, but I do eat egg and dairy products.
  • I live in a former mental hospital, in a beautiful park 20 minutes from central Stockholm. The hospital was built 1908 and looks very much like a castle.

So. Now I’ve done my share. What’s left is to give this burden to someone else. Since I don’t like the concept of chain *anything* I will introduce a small twist by tagging 5 people who probably won’t do it, for several reasons. But if they do it, it will be all the more interesting. So, without further ado:



I will present at JFokus


A few weeks ago I noted that Sun in Sweden announced on the JavaForum meetup that they were going to have a full day Java conference in January. This conference is called JFokus and will have 4 different tracks with 4 speakers on each track. I will feature in one of these tracks, talking about testing Java code with JRuby and RSpec, and also how to build a DSL for doing database integration testing using Ruby. I believe it will be very interesting, and the rest of the day looks incredible nice too.

So, if you’re in Sweden, Jan 30:th, look into it!

More information can be found at the official site: http://www.jfokus.se/.



Profile support in JRuby trunk


As noted a few posts ago, Sandbox has been ported and run in JRuby. You did need to use a special version, only available from a branch in the JRuby source tree. But now this support has been added to trunk. This means you can check out the latest version of JRuby trunk, gem install javasand, and everything will work.

For more info, see my previous post on this: here.



A Personal blog


Due to lots and lots and lots of stuff happening in my life right now, I haven’t had the time to write anything substantial here in a while. Hopefully, this will change.

I have also decided to start a new blog, for more… personal musings. Books, music, movies. Stuff like that. Nothing that would interest the regular readers of this blog, probably. But anyway, here’s the address: http://olabini.blogspot.com.

Ah, right: Have a merry christmas and a happy new year, everyone!