Skip to content

Commit 0c81ad3

Browse files
committed
perf(qwp): fuse per-frame CRC into one native call in MmapSegment.tryAppend
The append hot path computed the frame CRC-32C with two Crc32c.update JNI calls -- one over the u32 payloadLen field, one over the payload -- even though the layout [u32 crc][u32 payloadLen][payload] places the payload immediately after payloadLen (at lenAddr+4). The two CRC'd regions are physically contiguous, so a single update over 4 + payloadLen bytes yields the byte-identical CRC while crossing the JNI boundary once per frame instead of twice. This is the same form the recovery scanner already recomputes (reader.crc32c(pos + 4, 4 + payloadLen) in scanForRecovery), which validates every on-disk frame against the producer-written CRC, so the fused value is guaranteed identical and the round-trip is unchanged. No behavior change, no change to hashed or written bytes; saves one native-call dispatch per non-empty frame on the producer's zero-alloc hot path. Verified by MmapSegmentTest and MmapSegmentRecoveryFaultTest.
1 parent 178fbe9 commit 0c81ad3

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

  • core/src/main/java/io/questdb/client/cutlass/qwp/client/sf/cursor

core/src/main/java/io/questdb/client/cutlass/qwp/client/sf/cursor/MmapSegment.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,16 +554,17 @@ public long tryAppend(long payloadAddr, int payloadLen) {
554554
return -1L;
555555
}
556556
// CRC32C over the (payloadLen, payload) pair. Recovery scans validate
557-
// each frame by recomputing this CRC over the on-disk bytes.
557+
// each frame by recomputing this CRC over the on-disk bytes. The
558+
// payloadLen field (4 bytes at lenAddr) and the payload (at lenAddr+4)
559+
// are physically contiguous, so a single CRC pass covers both -- one
560+
// native call per frame, byte-identical to the chained form the
561+
// recovery scanner recomputes (see scanForRecovery).
558562
long lenAddr = mmapAddress + offset + 4;
559563
Unsafe.getUnsafe().putInt(lenAddr, payloadLen);
560564
if (payloadLen > 0) {
561565
Unsafe.getUnsafe().copyMemory(payloadAddr, mmapAddress + offset + FRAME_HEADER_SIZE, payloadLen);
562566
}
563-
int crc = Crc32c.update(Crc32c.INIT, lenAddr, 4);
564-
if (payloadLen > 0) {
565-
crc = Crc32c.update(crc, mmapAddress + offset + FRAME_HEADER_SIZE, payloadLen);
566-
}
567+
int crc = Crc32c.update(Crc32c.INIT, lenAddr, 4L + payloadLen);
567568
Unsafe.getUnsafe().putInt(mmapAddress + offset, crc);
568569
appendCursor = offset + total;
569570
// Plain read + write of the volatile field. `frameCount++` would

0 commit comments

Comments
 (0)