Skip to content

Commit 91ce229

Browse files
committed
0.5.3: BAOS compaction (re-use code); set position/writerIndex limit to buf.length
1 parent e3bf901 commit 91ce229

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
group = com.trivago
22

3-
version = 0.5.2
3+
version = 0.5.3
44

55
org.gradle.jvmargs = -ea -Dfile.encoding=UTF-8 -XX:+UseG1GC -Xms512m -Xmx1G -Djava.net.preferIPv4Stack=true
66
systemProp.file.encoding = UTF-8

src/main/java/com/trivago/fastutilconcurrentwrapper/io/BAOS.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import it.unimi.dsi.fastutil.bytes.ByteArrays;
77
import it.unimi.dsi.fastutil.io.MeasurableStream;
88
import it.unimi.dsi.fastutil.io.RepositionableStream;
9+
import jakarta.validation.constraints.Positive;
910
import jakarta.validation.constraints.PositiveOrZero;
1011
import lombok.val;
1112

@@ -61,8 +62,7 @@ public class BAOS extends ByteArrayOutputStream implements RepositionableStream,
6162
public byte[] array (){ return buf; }
6263

6364
public void array (byte[] array) {
64-
buf = array;
65-
count = 0; position = 0;
65+
buf = array; count = 0; position = 0;
6666
}
6767

6868
/** The current writing position. */
@@ -107,14 +107,12 @@ public BAOS (ByteArrayOutputStream baos) {
107107
/// @see #resize(int)
108108
@CanIgnoreReturnValue
109109
public byte[] trim () {
110-
buf = ByteArrays.trim(buf, count);
111-
return buf;
110+
return buf = ByteArrays.trim(buf, count);
112111
}
113112

114113
@Override
115114
public void write (int b) {
116-
if (position >= buf.length)
117-
buf = ByteArrays.grow(buf, position + 1, count);
115+
grow(1);
118116
buf[position++] = (byte)b;
119117
if (count < position) count = position;
120118
}
@@ -123,19 +121,19 @@ public void write (int b) {
123121
public void write (byte[] b, @PositiveOrZero int off, @PositiveOrZero int len) {
124122
//ByteArrays.ensureOffsetLength(b, off, len);
125123
Objects.checkFromIndexSize(off, len, b.length);
126-
if (position + len > buf.length)
127-
buf = ByteArrays.grow(buf, position + len, position);
128-
System.arraycopy(b, off, buf, position, len);
124+
grow(len);
125+
System.arraycopy(b,off, buf,position, len);
129126
position += len;
130127
if (count < position) count = position;
131128
}
132129

133130
@Override
134131
public void position (@PositiveOrZero long newPosition) {
135-
if (newPosition > Integer.MAX_VALUE) throw new IllegalArgumentException("Position too large: "+ newPosition);
136-
position = (int)newPosition;
132+
position = (int) Math.min(newPosition, buf.length);
133+
}
134+
public void writerIndex (@PositiveOrZero int newPosition) {
135+
position = Math.min(newPosition, buf.length);
137136
}
138-
public void writerIndex (@PositiveOrZero int newPosition){ position = newPosition; }
139137

140138
@Override public @PositiveOrZero long position (){ return position; }
141139
public @PositiveOrZero int writerIndex (){ return position; }
@@ -164,13 +162,12 @@ public void resize (@PositiveOrZero int targetCapacity) {
164162
/// @param additionalCapacity the number of bytes to add to the current buffer size
165163
/// @see #size()
166164
/// @see org.springframework.util.ResizableByteArrayOutputStream#grow(int)
167-
public void grow (@PositiveOrZero int additionalCapacity) {
168-
//Assert.isTrue(additionalCapacity >= 0, "Additional capacity must be 0 or higher");
169-
if (count < position) additionalCapacity = additionalCapacity + position - count;
170-
int needed = count + additionalCapacity;
171-
if (needed > buf.length){// size + new > capacity
172-
int newCapacity = (int)Math.min(Math.max((long)buf.length + (buf.length >> 1), needed), Arrays.MAX_ARRAY_SIZE);
173-
resize(newCapacity);
165+
public void grow (@Positive int len) {
166+
if (position + len > buf.length){
167+
int newLength = (int)Math.max(Math.min((long) buf.length + ( buf.length >> 1), Arrays.MAX_ARRAY_SIZE), position + len);
168+
val resizedBuffer = new byte[newLength];
169+
System.arraycopy(buf,0, resizedBuffer,0, count);
170+
buf = resizedBuffer;
174171
}
175172
}
176173

@@ -239,8 +236,7 @@ public void writeInt (int v) {
239236

240237
public void writeMedium (int v) {
241238
write(v >> 16);
242-
write(v >> 8);
243-
write(v);
239+
writeShort(v);
244240
}
245241

246242
public void writeUUID (UUID uuid) {
@@ -348,8 +344,7 @@ public BAOS append (CharSequence csq, int start, int end) {
348344
final int len = end - start;
349345
Objects.checkFromIndexSize(start, len, csq.length());
350346
final int len2 = len << 1;
351-
if (position + len2 > buf.length)
352-
buf = ByteArrays.grow(buf, position + len2, position);
347+
grow(len2);
353348

354349
JBytes.DirectByteArrayAccess.copyCharsToByteArray(csq, start, buf, position, len);
355350

src/test/java/com/trivago/fastutilconcurrentwrapper/io/BAOSTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,8 @@ void testPositionBeyondLength() {
223223

224224
@Test @DisplayName("Position throws exception for values too large")
225225
void testPositionTooLarge() {
226-
assertThrows(IllegalArgumentException.class, () ->
227-
stream.position(((long) Integer.MAX_VALUE) + 1)
228-
);
226+
stream.position(((long) Integer.MAX_VALUE) + 1);
227+
assertEquals(stream.capacity(), stream.position());
229228
}
230229

231230
@Test
@@ -839,12 +838,12 @@ public void testPositionWrite () {
839838
@Test
840839
public void testPositionWrite2 () {
841840
val fbaos = new BAOS();
842-
fbaos.position(fbaos.array().length + 2);
841+
fbaos.position(fbaos.array().length + 2);// limits to len
843842
fbaos.write(1);
844-
assertEquals(163, fbaos.length());
845-
assertEquals(163, fbaos.size());
846-
assertEquals(163, fbaos.position());
847-
assertEquals(163, fbaos.writerIndex());
843+
assertEquals(161, fbaos.length());
844+
assertEquals(161, fbaos.size());
845+
assertEquals(161, fbaos.position());
846+
assertEquals(161, fbaos.writerIndex());
848847
}
849848

850849
@Test
@@ -1007,6 +1006,7 @@ void _oos () throws IOException, ClassNotFoundException {
10071006
/// all: 2_281, op/s=219_195
10081007
@Test
10091008
void _benchmark () {
1009+
System.out.println("_benchmark BAOS/BAIS");
10101010
IntStream.range(0, 2).forEach(iteration->{
10111011
long t = System.nanoTime();
10121012
for (int loop = 1; loop < 500_000; loop++){
@@ -1049,6 +1049,8 @@ void _benchmark () {
10491049

10501050
@Test
10511051
void _benchmarkJDK () {
1052+
System.out.println("_benchmarkJDK DOS&BAOS/BAIS");
1053+
10521054
IntStream.range(0, 2).forEach(iteration->{
10531055
long t = System.nanoTime();
10541056
for (int loop = 1; loop < 500_000; loop++){
@@ -1094,6 +1096,8 @@ void _benchmarkJDK () {
10941096

10951097
@Test
10961098
void _benchmarkJDKBuffer () {
1099+
System.out.println("_benchmarkJDKBuffer ByteBuffer/BAIS");
1100+
10971101
IntStream.range(0, 2).forEach(__->{
10981102
long t = System.nanoTime();
10991103
for (int loop = 1; loop < 500_000; loop++){

0 commit comments

Comments
 (0)