Skip to content

Commit b92abe1

Browse files
dfa1claude
andcommitted
refactor: FsstEncodingEncoder reuses PTypeIO.set instead of reimplementing it
The private writeUnsigned helper duplicated core.io.PTypeIO.set, the canonical narrow-ptype write helper (MethodHandle-based, byte-offset addressed, already used by every other encoder). Its default branch throwing VortexException was dead: narrowestUnsigned only ever returns U8/U16/U32, all of which PTypeIO.set handles with identical little-endian layout and truncation-on-narrowing. Deleted writeUnsigned and the now-unused VortexException import; the two call sites now pass idx * ptype.byteSize() as the byte offset, matching the established PTypeIO.set idiom. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 7af2a78 commit b92abe1

1 file changed

Lines changed: 8 additions & 21 deletions

File tree

writer/src/main/java/io/github/dfa1/vortex/writer/encode/FsstEncodingEncoder.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import io.github.dfa1.vortex.core.model.DType;
44
import io.github.dfa1.vortex.core.model.PType;
55
import io.github.dfa1.vortex.core.model.EncodingId;
6-
import io.github.dfa1.vortex.core.error.VortexException;
6+
import io.github.dfa1.vortex.core.io.PTypeIO;
77
import io.github.dfa1.vortex.core.io.VortexFormat;
88
import io.github.dfa1.vortex.core.proto.ProtoFSSTMetadata;
99

@@ -121,17 +121,19 @@ public EncodeResult encode(DType dtype, Object data, EncodeContext ctx) {
121121
PType uncompLenPType = PType.narrowestUnsigned(maxUncompLen);
122122
PType codesOffPType = PType.narrowestUnsigned(totalCompressed);
123123

124-
MemorySegment uncompLenBuf = arena.allocate(Math.max((long) n * uncompLenPType.byteSize(), 1));
124+
long uncompLenBytes = uncompLenPType.byteSize();
125+
MemorySegment uncompLenBuf = arena.allocate(Math.max((long) n * uncompLenBytes, 1));
125126
for (int i = 0; i < n; i++) {
126-
writeUnsigned(uncompLenBuf, uncompLenPType, i, byteArrays[i].length);
127+
PTypeIO.set(uncompLenBuf, i * uncompLenBytes, uncompLenPType, byteArrays[i].length);
127128
}
128129

129-
MemorySegment codesOffBuf = arena.allocate((long) (n + 1) * codesOffPType.byteSize());
130+
long codesOffBytes = codesOffPType.byteSize();
131+
MemorySegment codesOffBuf = arena.allocate((long) (n + 1) * codesOffBytes);
130132
long off = 0;
131-
writeUnsigned(codesOffBuf, codesOffPType, 0, 0);
133+
PTypeIO.set(codesOffBuf, 0, codesOffPType, 0);
132134
for (int i = 0; i < n; i++) {
133135
off += compressed[i].length;
134-
writeUnsigned(codesOffBuf, codesOffPType, i + 1, off);
136+
PTypeIO.set(codesOffBuf, (i + 1) * codesOffBytes, codesOffPType, off);
135137
}
136138

137139
byte[] metaBytes = new ProtoFSSTMetadata(
@@ -152,21 +154,6 @@ public EncodeResult encode(DType dtype, Object data, EncodeContext ctx) {
152154
null, null);
153155
}
154156

155-
/// Writes `value` into `seg` at row `idx`, using `ptype`'s byte width.
156-
///
157-
/// @param seg destination segment
158-
/// @param ptype `U8`, `U16`, or `U32` (whatever [PType#narrowestUnsigned(long)] returned)
159-
/// @param idx row index (not a byte offset)
160-
/// @param value the value to write
161-
private static void writeUnsigned(MemorySegment seg, PType ptype, long idx, long value) {
162-
switch (ptype) {
163-
case U8 -> seg.set(ValueLayout.JAVA_BYTE, idx, (byte) value);
164-
case U16 -> seg.set(VortexFormat.LE_SHORT, idx * 2, (short) value);
165-
case U32 -> seg.set(VortexFormat.LE_INT, idx * 4, (int) value);
166-
default -> throw new VortexException(EncodingId.VORTEX_FSST, "unexpected ptype: " + ptype);
167-
}
168-
}
169-
170157
/// Trains a symbol table by iteratively refining candidate symbols against a bounded sample.
171158
///
172159
/// @param byteArrays the raw byte content of every input string

0 commit comments

Comments
 (0)