Changed ByteInput to lazily allocate BitInput object

BitInput not always required, mostly only ByteInput is used
Removed specialized BitInput constructors from ByteInput
ByteInput#unalign will allocate a BitInput of readableBytes size
BitInput#readSlice will allocate a BitInput of specified size
This commit is contained in:
Collin Smith 2020-08-08 03:47:36 -07:00
parent 673b437c61
commit 3e6e2c05ce
2 changed files with 10 additions and 14 deletions

View File

@ -14,16 +14,18 @@ public class BitInput implements Aligned, AlignedReader, UnalignedReader {
}
final ByteInput byteInput;
// final ByteBuf buffer;
final long numBits;
long bitsRead;
private int bitsCached;
private long cache;
BitInput(ByteInput byteInput) {
this(byteInput, 0, 0L, (long) byteInput.bytesRemaining() * Byte.SIZE);
}
BitInput(ByteInput byteInput, int bitsCached, long cache, long numBits) {
this.byteInput = byteInput;
// this.buffer = byteInput.buffer;
this.bitsCached = bitsCached;
this.cache = cache;
this.numBits = numBits;
@ -109,7 +111,8 @@ public class BitInput implements Aligned, AlignedReader, UnalignedReader {
// length should include the last byte that bits belong (round to ceil)
final long numBytes = (numBits - bitsCached + Byte.SIZE - 1) / Byte.SIZE;
return byteInput.readSlice(numBytes, bitsCached, cache, numBits).unalign();
final ByteInput byteInput = this.byteInput.readSlice(numBytes);
return byteInput.bitInput = new BitInput(byteInput, bitsCached, cache, numBits);
}
public BitInput discard(long bits) {

View File

@ -16,16 +16,11 @@ public class ByteInput implements Aligned, AlignedReader {
return bytes == null ? emptyByteInput() : new ByteInput(Unpooled.wrappedBuffer(bytes));
}
final BitInput bitInput;
final ByteBuf buffer;
BitInput bitInput;
ByteInput(ByteBuf buffer) {
this(buffer, 0, 0L, (long) buffer.readableBytes() * Byte.SIZE);
}
ByteInput(ByteBuf buffer, int bitsCached, long cache, long numBits) {
this.buffer = buffer;
this.bitInput = new BitInput(this, bitsCached, cache, numBits);
}
@Override
@ -49,6 +44,8 @@ public class ByteInput implements Aligned, AlignedReader {
}
public BitInput unalign() {
// FIXME: should bitInput.bitsRead = (long) bytesRead * Byte.SIZE
if (bitInput == null) this.bitInput = new BitInput(this);
return bitInput;
}
@ -99,13 +96,9 @@ public class ByteInput implements Aligned, AlignedReader {
}
public ByteInput readSlice(long numBytes) {
return readSlice(numBytes, 0, 0L, numBytes * Byte.SIZE);
}
public ByteInput readSlice(long numBytes, int bitsCached, long cache, long numBits) {
assert numBytes <= Integer.MAX_VALUE : "ByteBuf only supports int length";
final ByteBuf slice = buffer.readSlice((int) numBytes);
return new ByteInput(slice, bitsCached, cache, numBits);
return new ByteInput(slice);
}
/**