Skip to content

Commit

Permalink
[hotfix][network] Drop reductant getSizeUnsafe from Buffer interface
Browse files Browse the repository at this point in the history
The implementations of getSize and getSizeUnsafe are exactly the same now, which do not need the synchronized way.
So We could remove the getSizeUnsafe to make it clean and clear.
  • Loading branch information
zhijiangW committed May 31, 2019
1 parent ad0886b commit aff9b55
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 37 deletions.
Expand Up @@ -158,17 +158,6 @@ public interface Buffer {
*/
void setReaderIndex(int readerIndex) throws IndexOutOfBoundsException;

/**
* Returns the size of the written data, i.e. the <tt>writer index</tt>, of this buffer in an
* non-synchronized fashion.
*
* <p>This is where writable bytes start in the backing memory segment.
*
* @return writer index (from 0 (inclusive) to the size of the backing {@link MemorySegment}
* (inclusive))
*/
int getSizeUnsafe();

/**
* Returns the size of the written data, i.e. the <tt>writer index</tt>, of this buffer.
*
Expand Down
Expand Up @@ -290,11 +290,6 @@ public void setReaderIndex(int readerIndex) throws IndexOutOfBoundsException {
readerIndex(readerIndex);
}

@Override
public int getSizeUnsafe() {
return writerIndex();
}

@Override
public int getSize() {
return writerIndex();
Expand Down
Expand Up @@ -152,11 +152,6 @@ public void setReaderIndex(int readerIndex) throws IndexOutOfBoundsException {
readerIndex(readerIndex);
}

@Override
public int getSizeUnsafe() {
return writerIndex();
}

@Override
public int getSize() {
return writerIndex();
Expand Down
Expand Up @@ -193,7 +193,7 @@ Optional<BufferAndAvailability> getNextBuffer() throws IOException, InterruptedE
}
}

numBytesIn.inc(next.buffer().getSizeUnsafe());
numBytesIn.inc(next.buffer().getSize());
numBuffersIn.inc();
return Optional.of(new BufferAndAvailability(next.buffer(), next.isMoreAvailable(), next.buffersInBacklog()));
}
Expand Down
Expand Up @@ -201,7 +201,7 @@ Optional<BufferAndAvailability> getNextBuffer() throws IOException {
moreAvailable = !receivedBuffers.isEmpty();
}

numBytesIn.inc(next.getSizeUnsafe());
numBytesIn.inc(next.getSize());
numBuffersIn.inc();
return Optional.of(new BufferAndAvailability(next, moreAvailable, getSenderBacklog()));
}
Expand Down
Expand Up @@ -216,15 +216,13 @@ private static void testCreateSlice1(boolean isBuffer) {

assertEquals(0, slice.getReaderIndex());
assertEquals(10, slice.getSize());
assertEquals(10, slice.getSizeUnsafe());
assertSame(buffer, slice.unwrap().unwrap());

// slice indices should be independent:
buffer.setSize(8);
buffer.setReaderIndex(2);
assertEquals(0, slice.getReaderIndex());
assertEquals(10, slice.getSize());
assertEquals(10, slice.getSizeUnsafe());
}

@Test
Expand All @@ -244,15 +242,13 @@ private static void testCreateSlice2(boolean isBuffer) {

assertEquals(0, slice.getReaderIndex());
assertEquals(10, slice.getSize());
assertEquals(10, slice.getSizeUnsafe());
assertSame(buffer, slice.unwrap().unwrap());

// slice indices should be independent:
buffer.setSize(8);
buffer.setReaderIndex(2);
assertEquals(0, slice.getReaderIndex());
assertEquals(10, slice.getSize());
assertEquals(10, slice.getSizeUnsafe());
}

@Test
Expand Down Expand Up @@ -312,13 +308,11 @@ private static void testSetGetSize(boolean isBuffer) {
NetworkBuffer buffer = newBuffer(1024, 1024, isBuffer);

assertEquals(0, buffer.getSize()); // initially 0
assertEquals(0, buffer.getSizeUnsafe());
assertEquals(buffer.writerIndex(), buffer.getSize());
assertEquals(0, buffer.readerIndex()); // initially 0

buffer.setSize(10);
assertEquals(10, buffer.getSize());
assertEquals(10, buffer.getSizeUnsafe());
assertEquals(buffer.writerIndex(), buffer.getSize());
assertEquals(0, buffer.readerIndex()); // independent
}
Expand Down
Expand Up @@ -233,8 +233,7 @@ private void testGetSetReaderIndex(ReadOnlySlicedNetworkBuffer slice) {
/**
* Tests the independence of the writer index via
* {@link ReadOnlySlicedNetworkBuffer#setSize(int)},
* {@link ReadOnlySlicedNetworkBuffer#getSize()}, and
* {@link ReadOnlySlicedNetworkBuffer#getSizeUnsafe()}.
* {@link ReadOnlySlicedNetworkBuffer#getSize()}.
*/
@Test
public void testGetSetSize1() {
Expand All @@ -244,8 +243,7 @@ public void testGetSetSize1() {
/**
* Tests the independence of the writer index via
* {@link ReadOnlySlicedNetworkBuffer#setSize(int)},
* {@link ReadOnlySlicedNetworkBuffer#getSize()}, and
* {@link ReadOnlySlicedNetworkBuffer#getSizeUnsafe()}.
* {@link ReadOnlySlicedNetworkBuffer#getSize()}.
*/
@Test
public void testGetSetSize2() {
Expand All @@ -254,14 +252,10 @@ public void testGetSetSize2() {

private void testGetSetSize(ReadOnlySlicedNetworkBuffer slice, int sliceSize) {
assertEquals(DATA_SIZE, buffer.getSize());
assertEquals(DATA_SIZE, buffer.getSizeUnsafe());
assertEquals(sliceSize, slice.getSize());
assertEquals(sliceSize, slice.getSizeUnsafe());
buffer.setSize(DATA_SIZE + 1);
assertEquals(DATA_SIZE + 1, buffer.getSize());
assertEquals(DATA_SIZE + 1, buffer.getSizeUnsafe());
assertEquals(sliceSize, slice.getSize());
assertEquals(sliceSize, slice.getSizeUnsafe());
}

@Test
Expand Down

0 comments on commit aff9b55

Please sign in to comment.