News for hackers. Stuff that matters.
Re:Compact Off-Heap Structures and Tuples In Java by pacoverdi on Wednesday October 17, 2012 @10:17AM (#4664505)
Funny, I was actually manipulating C structures from Java, and after hitting a nasty issue, went procrastinating on twitter where I stumbled upon this blog post :)

For those interested, there are less low-level ways to access fields in structures, for example Javolution http://javolution.org/target/site/apidocs/javolution/io/Stru...

I'm working on a very similar lib but where Structs are stateless, allowing to use them concurrently from multiple threads.

Re:Compact Off-Heap Structures and Tuples In Java by jmount on Wednesday October 17, 2012 @12:20PM (#4665137)
Just a quick question. For the flyweight idea in the article you get additional thread unsafeness even for immutable data in that you are changing where the flyweight points (which is how you want to do it to avoid creating and destroying a lot of flyweight objects). What is your idea to pool/control multiple flyweight accessors?
Re:Compact Off-Heap Structures and Tuples In Java by pacoverdi on Wednesday October 17, 2012 @12:47PM (#4665303)
[Not sure whether the question is directed to the author or to my comment but I'll answer anyway :) ]

Basically, there are 3 notions

1) the buffer (byte[] or whatever) containing the data, immutable at least while the data is being accessed by a given thread

2) the Struct object: defines the structure of a message (field length, offset, arrays etc.)

3) the Message (aka the flyweight) that points to a given offset in buffer and is bound to a Struct instance

The buffer and the message are normally accessed by only one thread, but several threads can share Struct instances (i.e. the dictionary).

Each field (even deep in the field hierarchy) knows its offset relative to the beginning of the message, so values can be accessed like eg.

  int foo = struct.foo.getInt(msg);
The only complicated thing is when dealing with arrays (especially when nested in other arrays). The message does the bookkeeping necessary to safely access elements, ie. the mutable state is stored in the flyweight.
Re:Compact Off-Heap Structures and Tuples In Java by mjpt777 on Wednesday October 17, 2012 @12:34PM (#4665230)
I like to have only one writer thread for any data. This can have its own flyweight. Other threads can be readers each having their own read only flyweight implementation.