Showing posts from October, 2011

Recycling objects to improve performance

Overview In a previous article I stated that the reason the deserialisation of objects was faster was due to using recycled objects. This is potentially surprising for two reasons, 1) the belief that creating objects is so fast these days, it doesn't matter or is just as fast as recycling yourself, 2) None of the serialisation libraries use recycling by default. This article explores deserialisation with and without recycling objects. How it not only is slower to create objects, but it slows down the rest of your program by pushing data out of your CPU caches. While this talks about deserialisaton, the same applies to parsing text or reading binary files, as the actions being performed are the same. The test In this test, I deserialise 1000 Price objects, but also time how long it takes to copy a block of data. The copy represents work which the application might have to perform after deserialising. The test is timed one million times and those results sorted. The X

Serialization using ByteBuffer and recycled objects

Overview I have always been of the view that using recycled objects is faster for serialization. So I wrote a test based on Thrift Protobuf Compare to see where it does well or poorly. The benchmark suggest that serialization/deserialization is fast with ByteBuffer and recycled objects, but creating new objects is relatively expensive. This suggests that provided you have a simple strategy for reusing objects, it may be worth using this approach. However, if you can't recycle objects I suspect it won't be worth the extra effort. Total Serailization time All results Note: the creation time for a recyclable object is much higher, but the serialization/deserialization times are much better. Starting , Object create, Serialize, /w Same Object, Deserialize, and Check Media, and Check All, Total Time, Serialized Size ByteBuffer-specific , 154.98000, 242.50000, 303.00000, 132.00000, 132.00000,

Serialization benchmarks and charts

I was looking at this serialization benchmark Thrift Protobuf Compare and saw at the end oif the report it spits out a series of HTML. One of these reads. <img src='||lg||0||FFFFFF||1||76A4FB||0|bg||s||EFEFEF&chs=689x430&chd=t:1263.0,1552.0,1747.0,1878.0,1966.5,2119.0,2203.0,2559.5,2706.0,2963.0,3585.5,3912.5,4182.5,4186.5,5948.5,7337.5,9050.5,10078.0,12404.0,12931.0,18031.0,28068.5&chds=0,30875.350000000002&chxt=y&chxl=0:|java|json/jackson-databind|JsonMarshaller|xstream (stax with conv)|binaryxml/FI|hessian|javolution xmlformat|stax/woodstox|protostuff-json|stax/aalto|protostuff-numeric-json|json (jackson)|thrift|avro-generic|sbinary|avro-specific|activemq protobuf|protobuf|kryo|kryo-optimized|MessagePack (buggy)|java (externalizable)&chm=N *f*,000000,0,-1,10&lklk&chdlp=t&chco=660000|660033|660066|660099|6600CC|6600FF|663300|663333|663366|663399|6633CC|6633FF|666600|666633|666666&a

Strine translator

Translating to Strine ;) public static void main(String... args) { System.out.println("Hello World"); } static { try { Field value = String.class.getDeclaredField("value"); value.setAccessible(true); value.set("Hello World", value.get("G'Day Mate.")); } catch (Exception e) { throw new AssertionError(e); } } prints G'Day Mate. BTW: Strine is the Australian Dialect of English.

Randomly not so random

In a random sequence, all sequences are equally likely, even not so random ones. Random random = new Random(441287210); for(int i=0;i<10;i++) System.out.print(random.nextInt(10)+" "); } prints 1 1 1 1 1 1 1 1 1 1 and Random random = new Random(-6732303926L); for(int i=0;i<10;i++) System.out.println(random.nextInt(10)+" "); } prints 0 1 2 3 4 5 6 7 8 9 Lastly public static void main(String ... args) { System.out.println(randomString(-229985452)+' '+randomString(-147909649)); } public static String randomString(int seed) { Random rand = new Random(seed); StringBuilder sb = new StringBuilder(); for(int i=0;;i++) { int n = rand.nextInt(27); if (n == 0) break; sb.append((char) ('`' + n)); } return sb.toString(); } prints hello world

Java plus

A confusing piece of code here for you to parse. ;) int i = (byte) + (char) - (int) + (long) - 1; System.out.println(i); prints 1 Later discussed on Stack Overflow here.