4 ms·
Yeah I deal with that a lot with my search engine index too. Honestly it's not that bad once you get used to it. You can get away with referencing the data thr
by marginalia_nu 4y ago
Yeah I deal with that a lot with my search engine index too. Honestly it's not that bad once you get used to it.
You can get away with referencing the data through (mutable and reusable) pointer objects that reference memory mapped areas yet provide a relatively comfortable higher level interface. This gets rid of object churn while keeping a relatively sane interface.
- CHY872 4y agoYes, this is the way. Surprising how high level you can make this work - I’ve seen Collection implementations and all sorts.
- tsss 4y agoCan you give an example how to do that? I've only seen IO and parsing libraries that copy around byte arrays, but you can't cast byte arrays to arbitrary objects, which means you usually have to manipulate the bytes directly (unless you're lucky to work with only strings). At that point I imagine it would be far easier to just use C++ or Rust.
- marginalia_nu 4y agoYeah this sort of work isn't Java's strong suite. A lot of it's sort of like programming old-school C with oven mitts. It'll get a bit better with the Foreign Memory API which is in the JEP pipe. But a very bare bones example might look something like class MdrLayout { static final int FOO_OFFSET = 0; static final int BAR_OFFSET = 4; static final int BAZ_OFFSET = 12; static final int ENTRY_SIZE = 13; } class MyDataRecord { int idx = Integer.MIN_VALUE; ByteBuffer buffer = null; MyDataRecord() { } void movePointer(ByteBuffer buffer, int idx) { this.buffer = buffer; this.idx = idx; } int foo() { return buffer.getInt(ENTRY_SIZE * idx + FOO_OFFSET); } long bar() { return buffer.getLong(ENTRY_SIZE * idx + BAR_OFFSET); } byte baz() { return buffer.get(ENTRY_SIZE * idx + BAZ_OFFSET); } // may have operators like reset(), next() or skip() as well } or class MyDataRecord2 { final ByteBuffer buffer; MyDataRecord2(ByteBuffer buffer) { this.buffer = buffer; } int foo(int idx) { return buffer.getInt(ENTRY_SIZE * idx + FOO_OFFSET); } long bar(int idx) { return buffer.getLong(ENTRY_SIZE * idx + BAR_OFFSET); } byte baz(int idx) { return buffer.get(ENTRY_SIZE * idx + BAZ_OFFSET); } }