Saturday, December 21, 2013

Back to native

Usually the end of the year is time to take a step back, reflect on the past and plan for the future. For me it is also a time for a change. After 9 years working in Java (SE) I will switch back to C++. So I'll use the opportunity to share some thoughts I have accumulated in recent years.
Generally I avoid being a language zealot (like some of my colleagues ;). For me it is more important to create simple and beautiful solutions leveraging whatever technology is available.

Some say Java tends to be too ceremonial and I would agree on that.
Nowadays the industry has accepted that checked exceptions are not so useful after all.
Proper resource management with all this exception handling feels very clumsy and is definitely inferior compared to the good old RIIA mechanism available in C++.
Some design mistakes from the yearly day of Java are still present for the sake of compatibility. These are things like decorator pattern overuse in stream handling and all these unnecessary methods in Object most of which you rarely use but constantly pay the price for.

It was interesting for me to see how C++ has advanced for the last decade. I see there are many new and useful features C++11. For example lambdas are now part of the standard while in Java they have been delayed for years and are still not present.
The preprocessor and the necessity to use it has always been for me one of the major issues in C++. So I was hoping that this was addressed somehow in the recent evolution of the language. Alas! All these #include and #define directives and the involved text substitution without any awareness of the language feel like a stone age hack. And it is still there.
Even before I started working in Java I thought it would be great if C++ could be compiled to some standard intermediate format. This could improve both compile-time and runtime performance. When you compile to machine instructions ahead-of-time (AOT) you rarely know the exact CPU where the code will run, so you cannot take advantage of new CPU features. I was glad to find recently that there is already a considerable effort in that direction - namely LLVM project even though it is not C/C++ specific.

Wednesday, May 22, 2013

Java Serialization Performance

The Java program I work on in my day job maintains its data model in a tree-like structure of Java beans. The data volume is not big but still it is significant - about 500K property values in about 30K bean objects.
To ensure our program can restart from point of failure we often have to save this data model to disk. We use custom XML serialization. As this takes quite some time (several seconds) I started researching different alternatives.

It turned out that JAXB is not appropriate for the job as it does not cope well with cyclic dependencies and cross-references. It also requires adding a lot of annotations and our data model employs about 50 classes.

Next I tried standard Java object serialization but it turned out to preform even worse - it saved the data model in 15s in a 10MB file.

A quick search about Java serialization showed an open source library Kryo. I was able to test it very easily as it requires no annotations and almost no code changes. Kryo turned out to be lightning fast! It saved the same data model in just 400ms in a 5MB file. At first I did not believe all our data was saved, so I loaded it back and compared it to the original data but there were no differences.
One of the main reasons for this performance is that instead of reflection Kryo uses dynamic byte code generation via ReflectASM.
I also found another project run by Nate - Spine to be very interesting. It is related to game development. Obviously a very skilled developer.
Kudos Nate, great job!

Wednesday, May 15, 2013

Batch convert MOV to MP4 in Ubuntu

for i in *.MOV; do avconv -i $i -b:v 3000k ${i%.MOV}.MP4; done

Video: mjpeg -> libx264
Audio: pcm_s16be -> libvo_aacenc
Compression: 10 -> 1

These are video clips from my Panasonic photo camera

Wednesday, May 8, 2013

Saturday, February 23, 2013

Function or method

Lately I have been reading about Python. Here is an example that shows some interesting properties of functions and methods.
Given this code in

def f(a):
    print a
class C:
    def __init__(self, x):
        self.x = x
    def __str__(self):
        return "C(%d)" % self.x

Let's try some statements in the Python console

>>> from methods import *
>>> f("Hello")
>>> c = C(5)
>>> print c
>>> c.f = f
>>> c.f("bye")
>>> C.g = f
>>> c.g()
>>> g = c.g
>>> g()