[#2653] Remove unnecessary ensureAccessible() calls

Motivation:

I introduced ensureAccessible() class as part of 6c47cc9711 in some places. Unfortunally I also added some where these are not needed and so caused a performance regression.

Modification:

Remove calls where not needed.

Result:

Fixed performance regression.
This commit is contained in:
Norman Maurer 2014-07-14 21:02:07 +02:00
parent ccd88596f3
commit c4d0a87e19
3 changed files with 28 additions and 2 deletions

View File

@ -229,7 +229,6 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf ensureWritable(int minWritableBytes) {
ensureAccessible();
if (minWritableBytes < 0) {
throw new IllegalArgumentException(String.format(
"minWritableBytes: %d (expected: >= 0)", minWritableBytes));
@ -759,13 +758,15 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf writeByte(int value) {
ensureAccessible();
ensureWritable(1);
setByte(writerIndex++, value);
_setByte(writerIndex++, value);
return this;
}
@Override
public ByteBuf writeShort(int value) {
ensureAccessible();
ensureWritable(2);
_setShort(writerIndex, value);
writerIndex += 2;
@ -774,6 +775,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf writeMedium(int value) {
ensureAccessible();
ensureWritable(3);
_setMedium(writerIndex, value);
writerIndex += 3;
@ -782,6 +784,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf writeInt(int value) {
ensureAccessible();
ensureWritable(4);
_setInt(writerIndex, value);
writerIndex += 4;
@ -790,6 +793,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf writeLong(long value) {
ensureAccessible();
ensureWritable(8);
_setLong(writerIndex, value);
writerIndex += 8;
@ -816,6 +820,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
ensureAccessible();
ensureWritable(length);
setBytes(writerIndex, src, srcIndex, length);
writerIndex += length;
@ -847,6 +852,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
ensureAccessible();
ensureWritable(length);
setBytes(writerIndex, src, srcIndex, length);
writerIndex += length;
@ -855,6 +861,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf writeBytes(ByteBuffer src) {
ensureAccessible();
int length = src.remaining();
ensureWritable(length);
setBytes(writerIndex, src);
@ -865,6 +872,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public int writeBytes(InputStream in, int length)
throws IOException {
ensureAccessible();
ensureWritable(length);
int writtenBytes = setBytes(writerIndex, in, length);
if (writtenBytes > 0) {
@ -875,6 +883,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
ensureAccessible();
ensureWritable(length);
int writtenBytes = setBytes(writerIndex, in, length);
if (writtenBytes > 0) {

View File

@ -129,6 +129,7 @@ final class UnsafeDirectSwappedByteBuf extends SwappedByteBuf {
@Override
public ByteBuf writeShort(int value) {
wrapped.ensureAccessible();
wrapped.ensureWritable(2);
_setShort(wrapped.writerIndex, value);
wrapped.writerIndex += 2;
@ -137,6 +138,7 @@ final class UnsafeDirectSwappedByteBuf extends SwappedByteBuf {
@Override
public ByteBuf writeInt(int value) {
wrapped.ensureAccessible();
wrapped.ensureWritable(4);
_setInt(wrapped.writerIndex, value);
wrapped.writerIndex += 4;
@ -145,6 +147,7 @@ final class UnsafeDirectSwappedByteBuf extends SwappedByteBuf {
@Override
public ByteBuf writeLong(long value) {
wrapped.ensureAccessible();
wrapped.ensureWritable(8);
_setLong(wrapped.writerIndex, value);
wrapped.writerIndex += 8;

View File

@ -15,8 +15,10 @@
*/
package io.netty.buffer;
import io.netty.util.IllegalReferenceCountException;
import org.junit.Test;
import java.io.IOException;
import java.util.Random;
import static org.junit.Assert.*;
@ -95,6 +97,18 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
super.testNioBufferExposeOnlyRegion();
}
@Test(expected = IndexOutOfBoundsException.class)
@Override
public void testEnsureWritableAfterRelease() {
super.testEnsureWritableAfterRelease();
}
@Test(expected = IndexOutOfBoundsException.class)
@Override
public void testWriteZeroAfterRelease() throws IOException {
super.testWriteZeroAfterRelease();
}
@Test
@Override
public void testLittleEndianWithExpand() {