With market data it’s common to use some Message class(s) that “embeds” a fixed-length character array like 20-char for example.
Allocating an array object off-site on heap is very costly in memory footprint. One extra allocation per Message.
Also slower reading at run time due to data-cache inefficiency. Data cache favors contiguous data structures. See CPU(data)cache prefetching
c/c++ and c# (via Struct) can easily support exactly this “embedding”. I feel java also has some support. Beside JNI, I wonder if there’s another, pure-java solution.
Q: in java, how can I have embedded fixed-length char-array field in my Message or Acct object, rather than a separate array object allocated somewhere off-site?
- Solution: If the fixed length is small like 10, I could maintain 10 individual char fields.
- Solution: assuming the chars are ascii (8-bit rather than 16-bit in java), I can group first eight chars into a 64-bit long int field. Provide a translation interface when reading/writing the field. With 10 such fields I can support 80-char embedded.
- Solution: If not possible, I would use a gigantic singleton off-site char array to hold fixed-length “segments”. Then I need a single int “position”. Every Acct object has a field this.position, where this.position * fixedLength = offset, to identify one segment.
- There are two published solutions described in ringBuffer@pre_allocated objects to preempt JGC
Among them, not sure which solution is fastest in practice.