Skip to content

Commit e9f0e0d

Browse files
dfa1claude
andcommitted
perf(delta): hoist per-chunk scratch arrays out of loops
Eliminates 3×numChunks heap allocations per encode and decode call. deltaChunk/undeltaChunk now take an out param instead of returning. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 6b6c268 commit e9f0e0d

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

core/src/main/java/io/github/dfa1/vortex/encoding/DeltaEncoding.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ static EncodeResult encode(DType dtype, Object data) {
141141
long[] deltasAll = new long[(int) paddedLen];
142142
long[] chunkBuf = new long[FL_CHUNK_SIZE];
143143
long[] transposed = new long[FL_CHUNK_SIZE];
144+
long[] chunkBases = new long[lanes];
145+
long[] chunkDelta = new long[FL_CHUNK_SIZE];
144146

145147
for (int chunk = 0; chunk < numChunks; chunk++) {
146148
int start = chunk * FL_CHUNK_SIZE;
@@ -156,9 +158,8 @@ static EncodeResult encode(DType dtype, Object data) {
156158
}
157159
int basesOff = chunk * lanes;
158160
System.arraycopy(transposed, 0, basesAll, basesOff, lanes);
159-
long[] chunkBases = new long[lanes];
160161
System.arraycopy(basesAll, basesOff, chunkBases, 0, lanes);
161-
long[] chunkDelta = deltaChunk(transposed, chunkBases, lanes, typeBits, mask);
162+
deltaChunk(transposed, chunkBases, lanes, typeBits, mask, chunkDelta);
162163
System.arraycopy(chunkDelta, 0, deltasAll, chunk * FL_CHUNK_SIZE, FL_CHUNK_SIZE);
163164
}
164165

@@ -181,8 +182,7 @@ static EncodeResult encode(DType dtype, Object data) {
181182
return new EncodeResult(root, List.of(basesSeg, deltasSeg), statsMin, statsMax);
182183
}
183184

184-
private static long[] deltaChunk(long[] transposed, long[] bases, int lanes, int typeBits, long mask) {
185-
long[] out = new long[FL_CHUNK_SIZE];
185+
private static void deltaChunk(long[] transposed, long[] bases, int lanes, int typeBits, long mask, long[] out) {
186186
for (int lane = 0; lane < lanes; lane++) {
187187
long prev = bases[lane] & mask;
188188
for (int row = 0; row < typeBits; row++) {
@@ -192,7 +192,6 @@ private static long[] deltaChunk(long[] transposed, long[] bases, int lanes, int
192192
prev = next;
193193
}
194194
}
195-
return out;
196195
}
197196

198197
private static long[] toLongs(Object data, PType ptype) {
@@ -312,17 +311,18 @@ static Array decode(DecodeContext ctx) {
312311
int numChunks = (int) (deltasLen / FL_CHUNK_SIZE);
313312
long[] decoded = new long[(int) deltasLen];
314313
long[] untransposedChunk = new long[FL_CHUNK_SIZE];
314+
long[] chunkBases = new long[lanes];
315+
long[] chunkDeltas = new long[FL_CHUNK_SIZE];
316+
long[] chunkUndelta = new long[FL_CHUNK_SIZE];
315317

316318
for (int chunk = 0; chunk < numChunks; chunk++) {
317319
int basesOff = chunk * lanes;
318320
int deltaOff = chunk * FL_CHUNK_SIZE;
319321

320-
long[] chunkBases = new long[lanes];
321322
System.arraycopy(basesAll, basesOff, chunkBases, 0, lanes);
322-
long[] chunkDeltas = new long[FL_CHUNK_SIZE];
323323
System.arraycopy(deltasAll, deltaOff, chunkDeltas, 0, FL_CHUNK_SIZE);
324324

325-
long[] chunkUndelta = undeltaChunk(chunkDeltas, chunkBases, lanes, typeBits, mask);
325+
undeltaChunk(chunkDeltas, chunkBases, lanes, typeBits, mask, chunkUndelta);
326326

327327
for (int i = 0; i < FL_CHUNK_SIZE; i++) {
328328
untransposedChunk[transposeIndex(i)] = chunkUndelta[i];
@@ -343,8 +343,7 @@ static Array decode(DecodeContext ctx) {
343343
};
344344
}
345345

346-
private static long[] undeltaChunk(long[] deltas, long[] bases, int lanes, int typeBits, long mask) {
347-
long[] out = new long[FL_CHUNK_SIZE];
346+
private static void undeltaChunk(long[] deltas, long[] bases, int lanes, int typeBits, long mask, long[] out) {
348347
for (int lane = 0; lane < lanes; lane++) {
349348
long prev = bases[lane] & mask;
350349
for (int row = 0; row < typeBits; row++) {
@@ -354,7 +353,6 @@ private static long[] undeltaChunk(long[] deltas, long[] bases, int lanes, int t
354353
prev = next;
355354
}
356355
}
357-
return out;
358356
}
359357

360358
private static long[] readLongs(MemorySegment buf, int count, PType ptype) {

0 commit comments

Comments
 (0)