From cc7908739b1e7b1e10a2e091d943f6fe7938e58b Mon Sep 17 00:00:00 2001 From: luoluoyuyu Date: Thu, 19 Nov 2026 15:11:13 +0800 Subject: [PATCH] perf: cut flush encoding allocations and page-write copies Reduce allocations, boxing and buffer copies in the page/chunk write path without changing chunk size, page size or produced bytes. - reuse page body ByteBuffer and compressed byte[] per writer, and reuse RLE bit-pack scratch buffers - pre-size page writer buffers and grow the chunk page buffer once after the first page - reset chunk writer page-sizing state when a writer is reused - remove the per-value lambda and Integer boxing from the dictionary encoder; encode int RLE values from a primitive buffer - write the first page statistics up front for multi-page chunks instead of copying the whole chunk buffer, and compact in place for single-page chunks - batch Gorilla/Chimp bit writes through a 64-bit accumulator and account for buffered bytes when sizing pages --- .../encoding/encoder/DictionaryEncoder.java | 18 +-- .../encoder/DoublePrecisionChimpEncoder.java | 3 +- .../encoder/DoublePrecisionEncoderV2.java | 3 +- .../tsfile/encoding/encoder/Encoder.java | 8 ++ .../encoding/encoder/GorillaEncoderV2.java | 100 ++++++++++++---- .../encoding/encoder/IntChimpEncoder.java | 3 +- .../encoding/encoder/IntGorillaEncoder.java | 3 +- .../encoding/encoder/IntRleEncoder.java | 112 ++++++++++++++---- .../encoding/encoder/LongChimpEncoder.java | 3 +- .../encoding/encoder/LongGorillaEncoder.java | 3 +- .../encoding/encoder/LongRleEncoder.java | 53 ++++++--- .../tsfile/encoding/encoder/RleEncoder.java | 41 +++++-- .../encoder/SinglePrecisionChimpEncoder.java | 3 +- .../encoder/SinglePrecisionEncoderV2.java | 3 +- .../write/chunk/AlignedChunkWriterImpl.java | 8 ++ .../tsfile/write/chunk/ChunkWriterImpl.java | 79 ++++++++++-- .../tsfile/write/chunk/IChunkWriter.java | 6 + .../tsfile/write/chunk/TimeChunkWriter.java | 75 ++++++++++-- .../tsfile/write/chunk/ValueChunkWriter.java | 70 ++++++++++- .../tsfile/write/page/PageBodyBuffer.java | 53 +++++++++ .../tsfile/write/page/PageBodyEncoder.java | 24 +++- .../apache/tsfile/write/page/PageWriter.java | 57 ++++++++- .../tsfile/write/page/TimePageWriter.java | 45 ++++++- .../tsfile/write/page/ValuePageWriter.java | 61 ++++++++-- 24 files changed, 697 insertions(+), 137 deletions(-) create mode 100644 java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyBuffer.java diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java index b1fc56ffd0..d1c69c270f 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DictionaryEncoder.java @@ -62,14 +62,16 @@ public DictionaryEncoder() { @Override public void encode(Binary value, ByteArrayOutputStream out) { - int i = - entryIndex.computeIfAbsent( - value, - v -> { - indexEntry.add(v); - mapSize += v.getLength(); - return entryIndex.size(); - }); + // Avoid computeIfAbsent: its capturing lambda is allocated on every call, which shows up as + // one of the largest allocation sites when encoding a TEXT column. + Integer i = entryIndex.get(value); + if (i == null) { + indexEntry.add(value); + mapSize += value.getLength(); + // The index must be the size before inserting, matching the original 0-based indexes. + i = entryIndex.size(); + entryIndex.put(value, i); + } valuesEncoder.encode(i, out); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java index 843d17a39d..7c46829c30 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionChimpEncoder.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java index 9b487be872..2bda42d23b 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/DoublePrecisionEncoderV2.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java index c42c22f0fb..412f402cfb 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/Encoder.java @@ -104,6 +104,14 @@ public void encode(BigDecimal value, ByteArrayOutputStream out) { * * @return the maximal possible size of one data item encoded by this encoder */ + /** + * Returns the number of whole bytes that are buffered inside the encoder and not written to the + * output stream yet. Page sizing uses it so buffering bits does not change where pages are split. + */ + public int getPendingByteCount() { + return 0; + } + public int getOneItemMaxSize() { throw new UnsupportedOperationException(); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java index 0088a831c3..413ea690dc 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/GorillaEncoderV2.java @@ -38,8 +38,11 @@ public abstract class GorillaEncoderV2 extends Encoder { protected int storedLeadingZeros = Integer.MAX_VALUE; protected int storedTrailingZeros = 0; - private byte buffer = 0; - protected int bitsLeft = Byte.SIZE; + private long bitBuffer; + private int bitsUsed; + + /** Reusable 8-byte buffer so a full 64-bit group is written with one call. */ + private final byte[] writeBuffer = new byte[Long.BYTES]; protected GorillaEncoderV2() { super(TSEncoding.GORILLA); @@ -50,56 +53,103 @@ public final long getMaxByteSize() { return 0; } + @Override + public int getPendingByteCount() { + return bitsUsed >>> 3; + } + protected void reset() { firstValueWasWritten = false; storedLeadingZeros = Integer.MAX_VALUE; storedTrailingZeros = 0; - buffer = 0; - bitsLeft = Byte.SIZE; + bitBuffer = 0; + bitsUsed = 0; } /** Stores a 0 and increases the count of bits by 1. */ protected void skipBit(ByteArrayOutputStream out) { - bitsLeft--; - flipByte(out); + appendBit(0, out); } /** Stores a 1 and increases the count of bits by 1. */ protected void writeBit(ByteArrayOutputStream out) { - buffer |= (1 << (bitsLeft - 1)); - bitsLeft--; - flipByte(out); + appendBit(1, out); + } + + private void appendBit(int bit, ByteArrayOutputStream out) { + bitBuffer = (bitBuffer << 1) | bit; + if (++bitsUsed == Long.SIZE) { + writeFullLong(out); + } } /** * Writes the given long value using the defined amount of least significant bits. * - * @param value The long value to be written + * @param value The value to write * @param bits How many bits are stored to the stream */ protected void writeBits(long value, int bits, ByteArrayOutputStream out) { while (bits > 0) { - int shift = bits - bitsLeft; - if (shift >= 0) { - buffer |= (byte) ((value >> shift) & ((1 << bitsLeft) - 1)); - bits -= bitsLeft; - bitsLeft = 0; + int take = Math.min(bits, Long.SIZE - bitsUsed); + if (take == Long.SIZE) { + // bitsUsed is 0 here; taking 64 bits must not use a shift (Java shifts by 64 as 0), + // otherwise a stale bitBuffer would leak into the stream. + bitBuffer = value; } else { - shift = bitsLeft - bits; - buffer |= (byte) (value << shift); - bitsLeft -= bits; - bits = 0; + bitBuffer = (bitBuffer << take) | ((value >>> (bits - take)) & ((1L << take) - 1L)); + } + bitsUsed += take; + bits -= take; + if (bitsUsed == Long.SIZE) { + writeFullLong(out); } - flipByte(out); } } - protected void flipByte(ByteArrayOutputStream out) { - if (bitsLeft == 0) { - out.write(buffer); - buffer = 0; - bitsLeft = Byte.SIZE; + private void writeFullLong(ByteArrayOutputStream out) { + byte[] buffer = writeBuffer; + buffer[0] = (byte) (bitBuffer >>> 56); + buffer[1] = (byte) (bitBuffer >>> 48); + buffer[2] = (byte) (bitBuffer >>> 40); + buffer[3] = (byte) (bitBuffer >>> 32); + buffer[4] = (byte) (bitBuffer >>> 24); + buffer[5] = (byte) (bitBuffer >>> 16); + buffer[6] = (byte) (bitBuffer >>> 8); + buffer[7] = (byte) bitBuffer; + out.write(buffer, 0, Long.BYTES); + bitBuffer = 0; + bitsUsed = 0; + } + + /** + * Flushes the bits accumulated so far, padding the last byte with zeros. This reproduces the + * previous byte-at-a-time output exactly, including the extra zero byte written when the stream + * ends on a byte boundary (the decoder expects it). + */ + protected void flushRemainingBits(ByteArrayOutputStream out) { + if (bitsUsed == 0) { + out.write(0); + bitBuffer = 0; + return; + } + int fullBytes = bitsUsed >>> 3; + int remainder = bitsUsed & 7; + long aligned = bitBuffer << (Long.SIZE - bitsUsed); + byte[] buffer = writeBuffer; + for (int i = 0; i < fullBytes; i++) { + buffer[i] = (byte) (aligned >>> (56 - 8 * i)); + } + if (remainder == 0) { + out.write(buffer, 0, fullBytes); + // The empty ending byte is necessary when decoding. + out.write(0); + } else { + buffer[fullBytes] = (byte) (aligned >>> (56 - 8 * fullBytes)); + out.write(buffer, 0, fullBytes + 1); } + bitBuffer = 0; + bitsUsed = 0; } } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java index 2d342a5936..68be792842 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntChimpEncoder.java @@ -95,8 +95,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java index dd8a7da4e1..9bed569928 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntGorillaEncoder.java @@ -69,8 +69,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java index a9fd2e8fc8..3cc1318817 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/IntRleEncoder.java @@ -21,8 +21,12 @@ import org.apache.tsfile.common.conf.TSFileConfig; import org.apache.tsfile.encoding.bitpacking.IntPacker; +import org.apache.tsfile.i18n.Messages; import org.apache.tsfile.utils.ReadWriteForEncodingUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -30,19 +34,43 @@ /** Encoder for int value using rle or bit-packing. */ public class IntRleEncoder extends RleEncoder { + private static final Logger logger = LoggerFactory.getLogger(IntRleEncoder.class); + /** Packer for packing int values. */ private IntPacker packer; + /** Scratch array for packing one group, reused so each group does not allocate it. */ + private final int[] packBuffer = new int[TSFileConfig.RLE_MIN_REPEATED_NUM]; + + /** + * Values buffered until {@link #flush(ByteArrayOutputStream)}. A primitive array avoids boxing + * every dictionary index (the dictionary encoder feeds one index per value). + */ + private int[] intValues = new int[TSFileConfig.RLE_MIN_REPEATED_NUM * 4]; + + private int intValueCount; + + /** Primitive mirror of {@code bufferedValues} so encoding an int never boxes it. */ + private final int[] intBufferedValues = new int[TSFileConfig.RLE_MIN_REPEATED_NUM]; + + private int intPreValue; + public IntRleEncoder() { super(); bufferedValues = new Integer[TSFileConfig.RLE_MIN_REPEATED_NUM]; preValue = 0; + // Kept non-null for RleEncoder.reset(); the values themselves are buffered in intValues. values = new ArrayList<>(); } @Override public void encode(int value, ByteArrayOutputStream out) { - values.add(value); + if (intValueCount == intValues.length) { + int[] grown = new int[intValues.length << 1]; + System.arraycopy(intValues, 0, grown, 0, intValueCount); + intValues = grown; + } + intValues[intValueCount++] = value; } @Override @@ -63,18 +91,68 @@ public void encode(boolean value, ByteArrayOutputStream out) { @Override public void flush(ByteArrayOutputStream out) throws IOException { // we get bit width after receiving all data - this.bitWidth = ReadWriteForEncodingUtils.getIntMaxBitWidth(values); + this.bitWidth = getIntMaxBitWidth(intValues, intValueCount); packer = new IntPacker(bitWidth); - for (Integer value : values) { - encodeValue(value); + for (int i = 0; i < intValueCount; i++) { + encodeIntValue(intValues[i]); } super.flush(out); } + /** Primitive version of {@link RleEncoder#encodeValue(Comparable)} that avoids boxing. */ + @SuppressWarnings("squid:S3776") + private void encodeIntValue(int value) { + if (!isBitWidthSaved) { + byteCache.write(bitWidth); + isBitWidthSaved = true; + } + if (value == intPreValue) { + repeatCount++; + if (repeatCount >= TSFileConfig.RLE_MIN_REPEATED_NUM + && repeatCount <= TSFileConfig.RLE_MAX_REPEATED_NUM) { + return; + } else if (repeatCount == TSFileConfig.RLE_MAX_REPEATED_NUM + 1) { + repeatCount = TSFileConfig.RLE_MAX_REPEATED_NUM; + try { + writeRleRun(); + } catch (IOException e) { + logger.error(Messages.get("log.encoding.rle_full_rle_run_error"), repeatCount, e); + } + repeatCount = 1; + intPreValue = value; + } + } else { + if (repeatCount >= TSFileConfig.RLE_MIN_REPEATED_NUM) { + try { + writeRleRun(); + } catch (IOException e) { + logger.error(Messages.get("log.encoding.rle_write_num_error"), repeatCount, e); + } + } + repeatCount = 1; + intPreValue = value; + } + intBufferedValues[numBufferedValues] = value; + numBufferedValues++; + if (numBufferedValues == TSFileConfig.RLE_MIN_REPEATED_NUM) { + writeOrAppendBitPackedRun(); + } + } + + private static int getIntMaxBitWidth(int[] values, int count) { + int max = 1; + for (int i = 0; i < count; i++) { + max = Math.max(max, Integer.SIZE - Integer.numberOfLeadingZeros(values[i])); + } + return max; + } + @Override protected void reset() { super.reset(); preValue = 0; + intPreValue = 0; + intValueCount = 0; } /** write bytes to an outputStream using rle format: [header][value]. */ @@ -82,31 +160,28 @@ protected void reset() { protected void writeRleRun() throws IOException { endPreviousBitPackedRun(TSFileConfig.RLE_MIN_REPEATED_NUM); ReadWriteForEncodingUtils.writeUnsignedVarInt(repeatCount << 1, byteCache); - ReadWriteForEncodingUtils.writeIntLittleEndianPaddedOnBitWidth(preValue, byteCache, bitWidth); + ReadWriteForEncodingUtils.writeIntLittleEndianPaddedOnBitWidth( + intPreValue, byteCache, bitWidth); repeatCount = 0; numBufferedValues = 0; } @Override protected void clearBuffer() { - for (int i = numBufferedValues; i < TSFileConfig.RLE_MIN_REPEATED_NUM; i++) { - bufferedValues[i] = 0; + intBufferedValues[i] = 0; } } @Override protected void convertBuffer() { - byte[] bytes = new byte[bitWidth]; - - int[] tmpBuffer = new int[TSFileConfig.RLE_MIN_REPEATED_NUM]; + byte[] bytes = packScratch(bitWidth); for (int i = 0; i < TSFileConfig.RLE_MIN_REPEATED_NUM; i++) { - tmpBuffer[i] = bufferedValues[i]; + packBuffer[i] = intBufferedValues[i]; } - packer.pack8Values(tmpBuffer, 0, bytes); - // we'll not write bit-packing group to OutputStream immediately - // we buffer them in list - bytesBuffer.add(bytes); + packer.pack8Values(packBuffer, 0, bytes); + // the packed group is appended to the reusable bit-packing buffer + appendBitPackedGroup(bytes, bitWidth); } @Override @@ -119,11 +194,8 @@ public int getOneItemMaxSize() { @Override public long getMaxByteSize() { - if (values == null) { - return 0; - } // try to calculate max value - int groupNum = (values.size() / 8 + 1) / 63 + 1; - return (long) 8 + groupNum * 5 + values.size() * 4; + int groupNum = (intValueCount / 8 + 1) / 63 + 1; + return (long) 8 + groupNum * 5 + (long) intValueCount * 4; } } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java index d3076a5721..0759f8b650 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongChimpEncoder.java @@ -106,8 +106,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java index 1a13b7d806..c8a7fff5c4 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongGorillaEncoder.java @@ -69,8 +69,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java index f9e9c55702..5148a07a07 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/LongRleEncoder.java @@ -33,17 +33,33 @@ public class LongRleEncoder extends RleEncoder { /** Packer for packing long value. */ private LongPacker packer; + /** Scratch array for packing one group, reused so each group does not allocate it. */ + private final long[] packBuffer = new long[TSFileConfig.RLE_MIN_REPEATED_NUM]; + + /** + * Values buffered until {@link #flush(ByteArrayOutputStream)}. A primitive array avoids boxing. + */ + private long[] longValues = new long[TSFileConfig.RLE_MIN_REPEATED_NUM * 4]; + + private int longValueCount; + /** Constructor of LongRleEncoder. */ public LongRleEncoder() { super(); bufferedValues = new Long[TSFileConfig.RLE_MIN_REPEATED_NUM]; preValue = (long) 0; - values = new ArrayList(); + // Kept non-null for RleEncoder.reset(); values are buffered in longValues. + values = new ArrayList<>(); } @Override public void encode(long value, ByteArrayOutputStream out) { - values.add(value); + if (longValueCount == longValues.length) { + long[] grown = new long[longValues.length << 1]; + System.arraycopy(longValues, 0, grown, 0, longValueCount); + longValues = grown; + } + longValues[longValueCount++] = value; } /** @@ -55,18 +71,27 @@ public void encode(long value, ByteArrayOutputStream out) { @Override public void flush(ByteArrayOutputStream out) throws IOException { // we get bit width after receiving all data - this.bitWidth = ReadWriteForEncodingUtils.getLongMaxBitWidth(values); + this.bitWidth = getLongMaxBitWidth(longValues, longValueCount); packer = new LongPacker(bitWidth); - for (Long value : values) { - encodeValue(value); + for (int i = 0; i < longValueCount; i++) { + encodeValue(longValues[i]); } super.flush(out); } + private static int getLongMaxBitWidth(long[] values, int count) { + int max = 1; + for (int i = 0; i < count; i++) { + max = Math.max(max, Long.SIZE - Long.numberOfLeadingZeros(values[i])); + } + return max; + } + @Override protected void reset() { super.reset(); preValue = (long) 0; + longValueCount = 0; } /** @@ -92,15 +117,12 @@ protected void clearBuffer() { @Override protected void convertBuffer() { - byte[] bytes = new byte[bitWidth]; - long[] tmpBuffer = new long[TSFileConfig.RLE_MIN_REPEATED_NUM]; + byte[] bytes = packScratch(bitWidth); for (int i = 0; i < TSFileConfig.RLE_MIN_REPEATED_NUM; i++) { - tmpBuffer[i] = bufferedValues[i]; + packBuffer[i] = bufferedValues[i]; } - packer.pack8Values(tmpBuffer, 0, bytes); - // we'll not write bit-packing group to OutputStream immediately - // we buffer them in list - bytesBuffer.add(bytes); + packer.pack8Values(packBuffer, 0, bytes); + appendBitPackedGroup(bytes, bitWidth); } @Override @@ -112,11 +134,8 @@ public int getOneItemMaxSize() { @Override public long getMaxByteSize() { - if (values == null) { - return 0; - } // try to calculate max value - int groupNum = (values.size() / 8 + 1) / 63 + 1; - return (long) 8 + groupNum * 5 + values.size() * 8; + int groupNum = (longValueCount / 8 + 1) / 63 + 1; + return (long) 8 + groupNum * 5 + (long) longValueCount * 8; } } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java index f3a8be7cd2..4872361f18 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/RleEncoder.java @@ -25,6 +25,7 @@ import org.apache.tsfile.file.metadata.enums.TSEncoding; import org.apache.tsfile.i18n.Messages; import org.apache.tsfile.utils.Binary; +import org.apache.tsfile.utils.PublicBAOS; import org.apache.tsfile.utils.ReadWriteForEncodingUtils; import org.slf4j.Logger; @@ -33,7 +34,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigDecimal; -import java.util.ArrayList; import java.util.List; /** @@ -78,10 +78,13 @@ public abstract class RleEncoder> extends Encoder { protected int numBufferedValues; /** - * we will write all bytes using bit-packing to OutputStream once. Before that, all bytes are - * saved in list. + * Buffers the packed bytes of the current bit-packing run. It is reused across pages so that + * packing a group does not allocate a new byte array per group. */ - protected List bytesBuffer; + protected PublicBAOS bitPackedBuffer; + + /** Scratch array for packing one group of {@code RLE_MIN_REPEATED_NUM} values. */ + private byte[] packScratch = new byte[32]; /** flag which indicate encoding mode false -- rle true -- bit-packing. */ protected boolean isBitPackRun; @@ -102,7 +105,7 @@ public abstract class RleEncoder> extends Encoder { /** constructor. */ protected RleEncoder() { super(TSEncoding.RLE); - bytesBuffer = new ArrayList<>(); + bitPackedBuffer = new PublicBAOS(); isBitPackRun = false; isBitWidthSaved = false; byteCache = new ByteArrayOutputStream(); @@ -112,7 +115,7 @@ protected void reset() { numBufferedValues = 0; repeatCount = 0; bitPackedGroupCount = 0; - bytesBuffer.clear(); + bitPackedBuffer.reset(); isBitPackRun = false; isBitWidthSaved = false; byteCache.reset(); @@ -183,8 +186,8 @@ public void writeOrAppendBitPackedRun() { /** * End a bit-packing run write all bit-packing group to OutputStream bit-packing format: {@code - * [header][lastBitPackedNum][bit-packing group]+ [bit-packing group]+ are saved in List - * bytesBuffer }. + * [header][lastBitPackedNum][bit-packing group]+ [bit-packing group]+ are appended to the reusable + * bit-packing buffer. * * @param lastBitPackedNum - in last bit-packing group, it may have useful values less than 8. * This param indicates how many values are useful @@ -196,14 +199,28 @@ protected void endPreviousBitPackedRun(int lastBitPackedNum) { byte bitPackHeader = (byte) ((bitPackedGroupCount << 1) | 1); byteCache.write(bitPackHeader); byteCache.write(lastBitPackedNum); - for (byte[] bytes : bytesBuffer) { - byteCache.write(bytes, 0, bytes.length); - } - bytesBuffer.clear(); + byteCache.write(bitPackedBuffer.getBuf(), 0, bitPackedBuffer.size()); + bitPackedBuffer.reset(); isBitPackRun = false; bitPackedGroupCount = 0; } + /** + * Returns a scratch array with at least {@code length} bytes for packing one group, then appends + * the packed bytes to the current bit-packing run. + */ + protected byte[] packScratch(int length) { + if (packScratch.length < length) { + packScratch = new byte[length]; + } + return packScratch; + } + + /** Appends one packed group to the current bit-packing run. */ + protected void appendBitPackedGroup(byte[] bytes, int length) { + bitPackedBuffer.write(bytes, 0, length); + } + /** * Encode T value using rle or bit-packing. It may not write to OutputStream immediately * diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java index e31bc592f9..75dc4c5407 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionChimpEncoder.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java index 813a1e3fa5..2103fb9c09 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/encoding/encoder/SinglePrecisionEncoderV2.java @@ -46,8 +46,7 @@ public void flush(ByteArrayOutputStream out) { // flip the byte no matter it is empty or not // the empty ending byte is necessary when decoding - bitsLeft = 0; - flipByte(out); + flushRemainingBits(out); // the encoder may be reused, so let us reset it reset(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java index ecdf141515..71143dedec 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/AlignedChunkWriterImpl.java @@ -481,6 +481,14 @@ public void sealCurrentValuePage(int valueIndex) { valueChunkWriterList.get(valueIndex).sealCurrentPage(); } + @Override + public void setExpectedPageCount(int expectedPageCount) { + timeChunkWriter.setExpectedPageCount(expectedPageCount); + for (ValueChunkWriter valueChunkWriter : valueChunkWriterList) { + valueChunkWriter.setExpectedPageCount(expectedPageCount); + } + } + @Override public void clearPageWriter() { timeChunkWriter.clearPageWriter(); diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java index b4683382b0..c592a69ca8 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ChunkWriterImpl.java @@ -58,7 +58,15 @@ public class ChunkWriterImpl implements IChunkWriter { private long chunkOrdinal = -1; /** all pages of this chunk. */ - private final PublicBAOS pageBuffer; + private PublicBAOS pageBuffer; + + /** Expected number of pages of the next chunk, used to size the chunk buffer once. */ + private int expectedPageCount; + + /** True when the first page statistics were written up front (multi-page chunk). */ + private boolean firstPageStatisticsWritten; + + private int firstPageStatisticsSize; private int numOfPages; @@ -332,21 +340,34 @@ private void checkPageSizeAndMayOpenANewPage() { private void writePageToPageBuffer() { try { if (numOfPages == 0) { // record the firstPageStatistics + boolean includeFirstPageStatistics = expectedPageCount > 1; this.firstPageStatistics = pageWriter.getStatistics(); + this.firstPageStatisticsSize = + includeFirstPageStatistics ? firstPageStatistics.getSerializedSize() : 0; this.sizeWithoutStatistic = - pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, true, numOfPages); + pageWriter.writePageHeaderAndDataIntoBuff( + pageBuffer, true, numOfPages, includeFirstPageStatistics); + this.firstPageStatisticsWritten = includeFirstPageStatistics && firstPageStatisticsSize > 0; + if (firstPageStatisticsWritten) { + this.firstPageStatistics = null; + } } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer - byte[] b = pageBuffer.toByteArray(); - pageBuffer.reset(); - pageBuffer.write(b, 0, this.sizeWithoutStatistic); - firstPageStatistics.serialize(pageBuffer); - pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + if (!firstPageStatisticsWritten) { + byte[] b = pageBuffer.toByteArray(); + pageBuffer.reset(); + pageBuffer.write(b, 0, this.sizeWithoutStatistic); + firstPageStatistics.serialize(pageBuffer); + pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + } pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); firstPageStatistics = null; } else { pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); } + if (numOfPages == 0) { + growPageBufferAfterFirstPage(); + } // update statistics of this chunk numOfPages++; this.statistics.mergeStatistics(pageWriter.getStatistics()); @@ -368,10 +389,17 @@ public void writeToFileWriter( TsFileIOWriter tsfileWriter, Function measurementNameRemapper) throws IOException { sealCurrentPage(); + compactPreWrittenFirstPageStatistics(); writeAllPagesOfChunkToTsFile(tsfileWriter, statistics, measurementNameRemapper); - // reinit this chunk writer + // reinit this chunk writer so that a recycled writer behaves like a fresh one: reset the page + // size check heuristic (and SDT last point), otherwise the first page of the next chunk could + // be sized differently. pageBuffer.reset(); + firstPageStatisticsWritten = false; + firstPageStatisticsSize = 0; + this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; + this.isLastPoint = false; numOfPages = 0; sizeWithoutStatistic = 0; firstPageStatistics = null; @@ -415,6 +443,41 @@ public void clearPageWriter() { pageWriter = null; } + @Override + public void setExpectedPageCount(int expectedPageCount) { + this.expectedPageCount = expectedPageCount; + } + + /** + * Removes the first page statistics that were written up front when the chunk ends up with a + * single page. Shifts the page data in place, so no new array is allocated. + */ + private void compactPreWrittenFirstPageStatistics() { + if (!firstPageStatisticsWritten || numOfPages != 1) { + return; + } + byte[] buffer = pageBuffer.getBuf(); + int dataStart = sizeWithoutStatistic + firstPageStatisticsSize; + int tail = pageBuffer.size() - dataStart; + if (tail > 0) { + System.arraycopy(buffer, dataStart, buffer, sizeWithoutStatistic, tail); + } + pageBuffer.truncate(sizeWithoutStatistic + tail); + firstPageStatisticsWritten = false; + } + + private void growPageBufferAfterFirstPage() { + if (expectedPageCount <= 1 || pageBuffer.size() == 0) { + return; + } + long capacity = (long) pageBuffer.size() * expectedPageCount; + if (capacity > pageBuffer.getBuf().length && capacity < Integer.MAX_VALUE) { + PublicBAOS grown = new PublicBAOS((int) capacity); + grown.write(pageBuffer.getBuf(), 0, pageBuffer.size()); + pageBuffer = grown; + } + } + @Override public boolean checkIsUnsealedPageOverThreshold( long size, long pointNum, boolean returnTrueIfPageEmpty) { diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java index 4a198841c9..404546e036 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/IChunkWriter.java @@ -54,6 +54,12 @@ void writeToFileWriter( /** set the current pageWriter to null, friendly for gc */ void clearPageWriter(); + /** + * Gives the writer the expected number of pages of the next chunk. Writers may use it to grow the + * chunk buffer once after the first page instead of doubling it repeatedly. + */ + default void setExpectedPageCount(int expectedPageCount) {} + /** * used for compaction to check whether the chunk is over threshold or not. Return true if there * is no unsealed chunk. diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java index 8f4a01d00a..1cd44149ae 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/TimeChunkWriter.java @@ -62,6 +62,14 @@ public class TimeChunkWriter { /** all pages of this chunk. */ private PublicBAOS pageBuffer; + /** Expected number of pages of the next chunk, used to size the chunk buffer once. */ + private int expectedPageCount; + + /** True when the first page statistics were written up front (multi-page chunk). */ + private boolean firstPageStatisticsWritten; + + private int firstPageStatisticsSize; + private int numOfPages; /** write data into current page */ @@ -194,21 +202,34 @@ public long getRemainingPointNumberForCurrentPage() { public void writePageToPageBuffer() { try { if (numOfPages == 0) { // record the firstPageStatistics + boolean includeFirstPageStatistics = expectedPageCount > 1; this.firstPageStatistics = pageWriter.getStatistics(); + this.firstPageStatisticsSize = + includeFirstPageStatistics ? firstPageStatistics.getSerializedSize() : 0; this.sizeWithoutStatistic = - pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, true, numOfPages); + pageWriter.writePageHeaderAndDataIntoBuff( + pageBuffer, true, numOfPages, includeFirstPageStatistics); + this.firstPageStatisticsWritten = includeFirstPageStatistics && firstPageStatisticsSize > 0; + if (firstPageStatisticsWritten) { + this.firstPageStatistics = null; + } } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer - byte[] b = pageBuffer.toByteArray(); - pageBuffer.reset(); - pageBuffer.write(b, 0, this.sizeWithoutStatistic); - firstPageStatistics.serialize(pageBuffer); - pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + if (!firstPageStatisticsWritten) { + byte[] b = pageBuffer.toByteArray(); + pageBuffer.reset(); + pageBuffer.write(b, 0, this.sizeWithoutStatistic); + firstPageStatistics.serialize(pageBuffer); + pageBuffer.write(b, this.sizeWithoutStatistic, b.length - this.sizeWithoutStatistic); + } pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); firstPageStatistics = null; } else { pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); } + if (numOfPages == 0) { + growPageBufferAfterFirstPage(); + } // update statistics of this chunk numOfPages++; this.statistics.mergeStatistics(pageWriter.getStatistics()); @@ -270,10 +291,16 @@ public void writePageHeaderAndDataIntoBuff(ByteBuffer data, PageHeader header) public void writeToFileWriter(TsFileIOWriter tsfileWriter) throws IOException { sealCurrentPage(); + compactPreWrittenFirstPageStatistics(); writeAllPagesOfChunkToTsFile(tsfileWriter); - // reinit this chunk writer + // reinit this chunk writer so that a recycled writer behaves like a fresh one: reset the page + // size check heuristic (and SDT last point), otherwise the first page of the next chunk could + // be sized differently. pageBuffer.reset(); + firstPageStatisticsWritten = false; + firstPageStatisticsSize = 0; + this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; numOfPages = 0; sizeWithoutStatistic = 0; firstPageStatistics = null; @@ -313,6 +340,40 @@ public void clearPageWriter() { pageWriter = null; } + public void setExpectedPageCount(int expectedPageCount) { + this.expectedPageCount = expectedPageCount; + } + + /** + * Removes the first page statistics that were written up front when the chunk ends up with a + * single page. Shifts the page data in place, so no new array is allocated. + */ + private void compactPreWrittenFirstPageStatistics() { + if (!firstPageStatisticsWritten || numOfPages != 1) { + return; + } + byte[] buffer = pageBuffer.getBuf(); + int dataStart = sizeWithoutStatistic + firstPageStatisticsSize; + int tail = pageBuffer.size() - dataStart; + if (tail > 0) { + System.arraycopy(buffer, dataStart, buffer, sizeWithoutStatistic, tail); + } + pageBuffer.truncate(sizeWithoutStatistic + tail); + firstPageStatisticsWritten = false; + } + + private void growPageBufferAfterFirstPage() { + if (expectedPageCount <= 1 || pageBuffer.size() == 0) { + return; + } + long capacity = (long) pageBuffer.size() * expectedPageCount; + if (capacity > pageBuffer.getBuf().length && capacity < Integer.MAX_VALUE) { + PublicBAOS grown = new PublicBAOS((int) capacity); + grown.write(pageBuffer.getBuf(), 0, pageBuffer.size()); + pageBuffer = grown; + } + } + public int getNumOfPages() { return numOfPages; } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java index 12eebc59f6..3425138776 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/chunk/ValueChunkWriter.java @@ -64,7 +64,15 @@ public class ValueChunkWriter { private long chunkOrdinal = -1; /** all pages of this chunk. */ - private final PublicBAOS pageBuffer; + private PublicBAOS pageBuffer; + + /** Expected number of pages of the next chunk, used to size the chunk buffer once. */ + private int expectedPageCount; + + /** True when the first page statistics were written up front (multi-page chunk). */ + private boolean firstPageStatisticsWritten; + + private int firstPageStatisticsSize; private int numOfPages; @@ -205,7 +213,7 @@ public void write(long[] timestamps, Binary[] values, boolean[] isNull, int batc } public void writeEmptyPageToPageBuffer() throws IOException { - if (numOfPages == 1 && firstPageStatistics != null) { + if (!firstPageStatisticsWritten && numOfPages == 1 && firstPageStatistics != null) { // if the first page is not an empty page byte[] b = pageBuffer.toByteArray(); pageBuffer.reset(); @@ -221,14 +229,23 @@ public void writeEmptyPageToPageBuffer() throws IOException { public void writePageToPageBuffer() { try { if (numOfPages == 0) { + boolean includeFirstPageStatistics = expectedPageCount > 1; if (pageWriter.getStatistics().getCount() != 0) { // record the firstPageStatistics if it is not empty page this.firstPageStatistics = pageWriter.getStatistics(); + this.firstPageStatisticsSize = + includeFirstPageStatistics ? firstPageStatistics.getSerializedSize() : 0; } this.sizeWithoutStatistic = - pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, true, numOfPages); + pageWriter.writePageHeaderAndDataIntoBuff( + pageBuffer, true, numOfPages, includeFirstPageStatistics); + this.firstPageStatisticsWritten = includeFirstPageStatistics && firstPageStatisticsSize > 0; + if (firstPageStatisticsWritten) { + this.firstPageStatistics = null; + } } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer - if (firstPageStatistics != null) { // Consider previous page is an empty page + if (!firstPageStatisticsWritten && firstPageStatistics != null) { + // Fallback when the chunk turned out to have a second page but no page count was known. byte[] b = pageBuffer.toByteArray(); pageBuffer.reset(); pageBuffer.write(b, 0, this.sizeWithoutStatistic); @@ -241,6 +258,9 @@ public void writePageToPageBuffer() { pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, false, numOfPages); } + if (numOfPages == 0) { + growPageBufferAfterFirstPage(); + } // update statistics of this chunk numOfPages++; this.statistics.mergeStatistics(pageWriter.getStatistics()); @@ -322,10 +342,16 @@ public void writeToFileWriter( TsFileIOWriter tsfileWriter, Function measurementNameRemapper) throws IOException { sealCurrentPage(); + compactPreWrittenFirstPageStatistics(); writeAllPagesOfChunkToTsFile(tsfileWriter, measurementNameRemapper); - // reinit this chunk writer + // reinit this chunk writer so that a recycled writer behaves like a fresh one: reset the page + // size check heuristic (and SDT last point), otherwise the first page of the next chunk could + // be sized differently. pageBuffer.reset(); + firstPageStatisticsWritten = false; + firstPageStatisticsSize = 0; + this.valueCountInOnePageForNextCheck = MINIMUM_RECORD_COUNT_FOR_CHECK; numOfPages = 0; sizeWithoutStatistic = 0; firstPageStatistics = null; @@ -405,6 +431,40 @@ public void clearPageWriter() { pageWriter = null; } + public void setExpectedPageCount(int expectedPageCount) { + this.expectedPageCount = expectedPageCount; + } + + /** + * Removes the first page statistics that were written up front when the chunk ends up with a + * single page. Shifts the page data in place, so no new array is allocated. + */ + private void compactPreWrittenFirstPageStatistics() { + if (!firstPageStatisticsWritten || numOfPages != 1) { + return; + } + byte[] buffer = pageBuffer.getBuf(); + int dataStart = sizeWithoutStatistic + firstPageStatisticsSize; + int tail = pageBuffer.size() - dataStart; + if (tail > 0) { + System.arraycopy(buffer, dataStart, buffer, sizeWithoutStatistic, tail); + } + pageBuffer.truncate(sizeWithoutStatistic + tail); + firstPageStatisticsWritten = false; + } + + private void growPageBufferAfterFirstPage() { + if (expectedPageCount <= 1 || pageBuffer.size() == 0) { + return; + } + long capacity = (long) pageBuffer.size() * expectedPageCount; + if (capacity > pageBuffer.getBuf().length && capacity < Integer.MAX_VALUE) { + PublicBAOS grown = new PublicBAOS((int) capacity); + grown.write(pageBuffer.getBuf(), 0, pageBuffer.size()); + pageBuffer = grown; + } + } + public int getNumOfPages() { return numOfPages; } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyBuffer.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyBuffer.java new file mode 100644 index 0000000000..9158680b22 --- /dev/null +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyBuffer.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tsfile.write.page; + +import java.nio.ByteBuffer; + +/** + * Reusable scratch buffers for building a page body. + * + *

A page writer owns one instance, so the uncompressed page buffer and the compressor output + * buffer are reused across the pages of a chunk instead of being allocated per page. The buffers + * are only valid until the page body has been copied into the chunk buffer; callers must not retain + * them. Reusing them keeps the produced page and chunk bytes unchanged. + */ +final class PageBodyBuffer { + + private ByteBuffer uncompressed; + private byte[] compressed; + + /** Returns a cleared heap buffer with at least {@code capacity} bytes. */ + ByteBuffer uncompressed(int capacity) { + if (uncompressed == null || uncompressed.capacity() < capacity) { + uncompressed = ByteBuffer.allocate(capacity); + } else { + uncompressed.clear(); + } + return uncompressed; + } + + /** Returns an output array with at least {@code capacity} bytes for the compressor. */ + byte[] compressed(int capacity) { + if (compressed == null || compressed.length < capacity) { + compressed = new byte[capacity]; + } + return compressed; + } +} diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java index e86946b5e1..31552bd577 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageBodyEncoder.java @@ -43,6 +43,24 @@ static ByteBuffer encode( int pageIndex, long chunkOrdinal) throws IOException { + return encode( + pageData, uncompressedSize, compressor, encryptParameter, pageIndex, chunkOrdinal, null); + } + + /** + * Encodes the page body, reusing the caller's compressor output buffer when it is large enough. + * The returned buffer (and the reusable arrays it may reference) is only valid until the caller + * has copied the page body; it must not be retained. + */ + static ByteBuffer encode( + ByteBuffer pageData, + int uncompressedSize, + ICompressor compressor, + EncryptParameter encryptParameter, + int pageIndex, + long chunkOrdinal, + PageBodyBuffer buffer) + throws IOException { byte[] plaintext; int plaintextOffset = 0; int compressedSize; @@ -56,7 +74,11 @@ static ByteBuffer encode( plaintext = compressor.compress(pageData.array(), inputOffset, uncompressedSize); compressedSize = plaintext.length; } else { - plaintext = new byte[compressor.getMaxBytesForCompression(uncompressedSize)]; + int maxBytesForCompression = compressor.getMaxBytesForCompression(uncompressedSize); + plaintext = + buffer == null + ? new byte[maxBytesForCompression] + : buffer.compressed(maxBytesForCompression); compressedSize = compressor.compress(pageData.array(), inputOffset, uncompressedSize, plaintext); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java index ce840dfff0..e643d8711f 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java @@ -18,6 +18,7 @@ */ package org.apache.tsfile.write.page; +import org.apache.tsfile.common.conf.TSFileDescriptor; import org.apache.tsfile.compress.ICompressor; import org.apache.tsfile.encoding.encoder.Encoder; import org.apache.tsfile.encrypt.EncryptParameter; @@ -56,6 +57,10 @@ public class PageWriter { private Encoder valueEncoder; private PublicBAOS valueOut; + // Reused across the pages of a chunk so that encoding and compression do not allocate the page + // body buffers for every page. + private final PageBodyBuffer pageBodyBuffer = new PageBodyBuffer(); + /** * statistic of current page. It will be reset after calling {@code * writePageHeaderAndDataIntoBuff()} @@ -74,8 +79,8 @@ public PageWriter(IMeasurementSchema measurementSchema) { } private PageWriter(Encoder timeEncoder, Encoder valueEncoder) { - this.timeOut = new PublicBAOS(); - this.valueOut = new PublicBAOS(); + this.timeOut = new PublicBAOS(bufferCapacity()); + this.valueOut = new PublicBAOS(bufferCapacity()); this.timeEncoder = timeEncoder; this.valueEncoder = valueEncoder; this.encryptParam = EncryptUtils.getEncryptParameter(); @@ -92,13 +97,18 @@ public PageWriter(IMeasurementSchema measurementSchema, EncryptParameter encrypt } private PageWriter(Encoder timeEncoder, Encoder valueEncoder, EncryptParameter encryptParam) { - this.timeOut = new PublicBAOS(); - this.valueOut = new PublicBAOS(); + this.timeOut = new PublicBAOS(bufferCapacity()); + this.valueOut = new PublicBAOS(bufferCapacity()); this.timeEncoder = timeEncoder; this.valueEncoder = valueEncoder; this.encryptParam = encryptParam; } + /** Initial capacity for the time and value buffers; avoids repeated growth for the first page. */ + private static int bufferCapacity() { + return Math.max(1024, TSFileDescriptor.getInstance().getConfig().getPageSizeInByte() / 2); + } + /** write a time value pair into encoder */ public void write(long time, boolean value) { timeEncoder.encode(time, timeOut); @@ -224,6 +234,19 @@ public ByteBuffer getUncompressedBytes() throws IOException { return buffer; } + /** Same as {@link #getUncompressedBytes()} but reuses the writer's scratch buffer. */ + private ByteBuffer getUncompressedBytesForWriting() throws IOException { + prepareEndWriteOnePage(); + int timeSize = timeOut.size(); + int valueSize = valueOut.size(); + ByteBuffer buffer = pageBodyBuffer.uncompressed(timeSize + valueSize + 4); + ReadWriteForEncodingUtils.writeUnsignedVarInt(timeSize, buffer); + buffer.put(timeOut.getBuf(), 0, timeSize); + buffer.put(valueOut.getBuf(), 0, valueSize); + buffer.flip(); + return buffer; + } + /** write the page header and data into the PageWriter's output stream. */ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) throws IOException { @@ -233,15 +256,32 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) /** write the page header and data into the PageWriter's output stream. */ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex) throws IOException { + return writePageHeaderAndDataIntoBuff(pageBuffer, first, pageIndex, false); + } + + /** + * @param includeFirstPageStatistics when {@code first} is true, serialize the first page + * statistics after the page header. Multi-page chunks need them; writing them up front avoids + * copying the whole chunk buffer when the second page arrives. + */ + public int writePageHeaderAndDataIntoBuff( + PublicBAOS pageBuffer, boolean first, int pageIndex, boolean includeFirstPageStatistics) + throws IOException { if (statistics.getCount() == 0) { return 0; } - ByteBuffer pageData = getUncompressedBytes(); + ByteBuffer pageData = getUncompressedBytesForWriting(); int uncompressedSize = pageData.remaining(); ByteBuffer pageBody = PageBodyEncoder.encode( - pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); + pageData, + uncompressedSize, + compressor, + encryptParam, + pageIndex, + chunkOrdinal, + pageBodyBuffer); int pageBodySize = pageBody.remaining(); // write the page header to IOWriter @@ -251,6 +291,9 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); sizeWithoutStatistic += ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); + if (includeFirstPageStatistics) { + statistics.serialize(pageBuffer); + } } else { ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); @@ -273,6 +316,8 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, public long estimateMaxMemSize() { return timeOut.size() + valueOut.size() + + timeEncoder.getPendingByteCount() + + valueEncoder.getPendingByteCount() + timeEncoder.getMaxByteSize() + valueEncoder.getMaxByteSize(); } diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java index 6463ef51be..61ffb0de1f 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/TimePageWriter.java @@ -18,6 +18,7 @@ */ package org.apache.tsfile.write.page; +import org.apache.tsfile.common.conf.TSFileDescriptor; import org.apache.tsfile.compress.ICompressor; import org.apache.tsfile.encoding.encoder.Encoder; import org.apache.tsfile.encrypt.EncryptParameter; @@ -49,6 +50,10 @@ public class TimePageWriter { private Encoder timeEncoder; private final PublicBAOS timeOut; + // Reused across the pages of a chunk so that encoding and compression do not allocate the page + // body buffers for every page. + private final PageBodyBuffer pageBodyBuffer = new PageBodyBuffer(); + /** * statistic of current page. It will be reset after calling {@code * writePageHeaderAndDataIntoBuff()} @@ -56,7 +61,7 @@ public class TimePageWriter { private TimeStatistics statistics; public TimePageWriter(Encoder timeEncoder, ICompressor compressor) { - this.timeOut = new PublicBAOS(); + this.timeOut = new PublicBAOS(timeBufferCapacity()); this.timeEncoder = timeEncoder; this.statistics = new TimeStatistics(); this.compressor = compressor; @@ -65,13 +70,18 @@ public TimePageWriter(Encoder timeEncoder, ICompressor compressor) { public TimePageWriter( Encoder timeEncoder, ICompressor compressor, EncryptParameter encryptParam) { - this.timeOut = new PublicBAOS(); + this.timeOut = new PublicBAOS(timeBufferCapacity()); this.timeEncoder = timeEncoder; this.statistics = new TimeStatistics(); this.compressor = compressor; this.encryptParam = encryptParam; } + private static int timeBufferCapacity() { + return Math.max( + 1024, TSFileDescriptor.getInstance().getConfig().getMaxNumberOfPointsInPage() * 2); + } + /** write a time into encoder */ public void write(long time) { timeEncoder.encode(time, timeOut); @@ -107,6 +117,16 @@ public ByteBuffer getUncompressedBytes() throws IOException { return buffer; } + /** Same as {@link #getUncompressedBytes()} but reuses the writer's scratch buffer. */ + private ByteBuffer getUncompressedBytesForWriting() throws IOException { + prepareEndWriteOnePage(); + int timeSize = timeOut.size(); + ByteBuffer buffer = pageBodyBuffer.uncompressed(timeSize); + buffer.put(timeOut.getBuf(), 0, timeSize); + buffer.flip(); + return buffer; + } + /** write the page header and data into the PageWriter's output stream. */ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) throws IOException { @@ -116,15 +136,27 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) /** write the page header and data into the PageWriter's output stream. */ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex) throws IOException { + return writePageHeaderAndDataIntoBuff(pageBuffer, first, pageIndex, false); + } + + public int writePageHeaderAndDataIntoBuff( + PublicBAOS pageBuffer, boolean first, int pageIndex, boolean includeFirstPageStatistics) + throws IOException { if (statistics.getCount() == 0) { return 0; } - ByteBuffer pageData = getUncompressedBytes(); + ByteBuffer pageData = getUncompressedBytesForWriting(); int uncompressedSize = pageData.remaining(); ByteBuffer pageBody = PageBodyEncoder.encode( - pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); + pageData, + uncompressedSize, + compressor, + encryptParam, + pageIndex, + chunkOrdinal, + pageBodyBuffer); int pageBodySize = pageBody.remaining(); // write the page header to IOWriter @@ -134,6 +166,9 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); sizeWithoutStatistic += ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); + if (includeFirstPageStatistics) { + statistics.serialize(pageBuffer); + } } else { ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); @@ -156,7 +191,7 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, * @return allocated size in time, value and outputStream */ public long estimateMaxMemSize() { - return timeOut.size() + timeEncoder.getMaxByteSize(); + return timeOut.size() + timeEncoder.getPendingByteCount() + timeEncoder.getMaxByteSize(); } /** reset this page */ diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java index 16c101ee67..276665858a 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java @@ -18,6 +18,7 @@ */ package org.apache.tsfile.write.page; +import org.apache.tsfile.common.conf.TSFileDescriptor; import org.apache.tsfile.compress.ICompressor; import org.apache.tsfile.encoding.encoder.Encoder; import org.apache.tsfile.encrypt.EncryptParameter; @@ -64,15 +65,19 @@ public class ValuePageWriter { private final PublicBAOS bitmapOut; + // Reused across the pages of a chunk so that encoding and compression do not allocate the page + // body buffers for every page. + private final PageBodyBuffer pageBodyBuffer = new PageBodyBuffer(); + private static final int MASK = 1 << 7; private static final byte[] NULL_BITMAP_BATCH = new byte[1024]; public ValuePageWriter(Encoder valueEncoder, ICompressor compressor, TSDataType dataType) { - this.valueOut = new PublicBAOS(); + this.valueOut = new PublicBAOS(valueBufferCapacity()); this.bitmap = 0; this.size = 0; - this.bitmapOut = new PublicBAOS(); + this.bitmapOut = new PublicBAOS(bitmapBufferCapacity()); this.valueEncoder = valueEncoder; this.statistics = Statistics.getStatsByType(dataType); this.compressor = compressor; @@ -84,16 +89,25 @@ public ValuePageWriter( ICompressor compressor, TSDataType dataType, EncryptParameter encryptParam) { - this.valueOut = new PublicBAOS(); + this.valueOut = new PublicBAOS(valueBufferCapacity()); this.bitmap = 0; this.size = 0; - this.bitmapOut = new PublicBAOS(); + this.bitmapOut = new PublicBAOS(bitmapBufferCapacity()); this.valueEncoder = valueEncoder; this.statistics = Statistics.getStatsByType(dataType); this.compressor = compressor; this.encryptParam = encryptParam; } + private static int valueBufferCapacity() { + return Math.max(1024, TSFileDescriptor.getInstance().getConfig().getPageSizeInByte()); + } + + private static int bitmapBufferCapacity() { + return Math.max( + 16, TSFileDescriptor.getInstance().getConfig().getMaxNumberOfPointsInPage() / 8 + 8); + } + /** write a time value pair into encoder */ public void write(long time, boolean value, boolean isNull) { setBit(isNull); @@ -394,6 +408,19 @@ public ByteBuffer getUncompressedBytes() throws IOException { return buffer; } + /** Same as {@link #getUncompressedBytes()} but reuses the writer's scratch buffer. */ + private ByteBuffer getUncompressedBytesForWriting() throws IOException { + prepareEndWriteOnePage(); + int bitmapSize = bitmapOut.size(); + int valueSize = valueOut.size(); + ByteBuffer buffer = pageBodyBuffer.uncompressed(Integer.BYTES + bitmapSize + valueSize); + buffer.putInt(size); + buffer.put(bitmapOut.getBuf(), 0, bitmapSize); + buffer.put(valueOut.getBuf(), 0, valueSize); + buffer.flip(); + return buffer; + } + public int writeEmptyPageIntoBuff(PublicBAOS pageBuffer) { return ReadWriteForEncodingUtils.writeUnsignedVarInt(0, pageBuffer); } @@ -407,6 +434,12 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first) /** write the page header and data into the PageWriter's output stream. */ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, int pageIndex) throws IOException { + return writePageHeaderAndDataIntoBuff(pageBuffer, first, pageIndex, false); + } + + public int writePageHeaderAndDataIntoBuff( + PublicBAOS pageBuffer, boolean first, int pageIndex, boolean includeFirstPageStatistics) + throws IOException { if (size == 0) { return 0; } else if (statistics.getCount() == 0) { @@ -414,11 +447,17 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, return writeEmptyPageIntoBuff(pageBuffer); } - ByteBuffer pageData = getUncompressedBytes(); + ByteBuffer pageData = getUncompressedBytesForWriting(); int uncompressedSize = pageData.remaining(); ByteBuffer pageBody = PageBodyEncoder.encode( - pageData, uncompressedSize, compressor, encryptParam, pageIndex, chunkOrdinal); + pageData, + uncompressedSize, + compressor, + encryptParam, + pageIndex, + chunkOrdinal, + pageBodyBuffer); int pageBodySize = pageBody.remaining(); // write the page header to IOWriter @@ -428,6 +467,9 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); sizeWithoutStatistic += ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); + if (includeFirstPageStatistics) { + statistics.serialize(pageBuffer); + } } else { ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer); ReadWriteForEncodingUtils.writeUnsignedVarInt(pageBodySize, pageBuffer); @@ -447,7 +489,12 @@ public int writePageHeaderAndDataIntoBuff(PublicBAOS pageBuffer, boolean first, * @return allocated size in time, value and outputStream */ public long estimateMaxMemSize() { - return Integer.BYTES + bitmapOut.size() + 1 + valueOut.size() + valueEncoder.getMaxByteSize(); + return Integer.BYTES + + bitmapOut.size() + + 1 + + valueOut.size() + + valueEncoder.getPendingByteCount() + + valueEncoder.getMaxByteSize(); } /** reset this page */