[#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 93c306602a
commit f88dfd0430
3 changed files with 28 additions and 2 deletions

View File

@ -229,7 +229,6 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf ensureWritable(int minWritableBytes) { public ByteBuf ensureWritable(int minWritableBytes) {
ensureAccessible();
if (minWritableBytes < 0) { if (minWritableBytes < 0) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(String.format(
"minWritableBytes: %d (expected: >= 0)", minWritableBytes)); "minWritableBytes: %d (expected: >= 0)", minWritableBytes));
@ -731,13 +730,15 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf writeByte(int value) { public ByteBuf writeByte(int value) {
ensureAccessible();
ensureWritable(1); ensureWritable(1);
setByte(writerIndex++, value); _setByte(writerIndex++, value);
return this; return this;
} }
@Override @Override
public ByteBuf writeShort(int value) { public ByteBuf writeShort(int value) {
ensureAccessible();
ensureWritable(2); ensureWritable(2);
_setShort(writerIndex, value); _setShort(writerIndex, value);
writerIndex += 2; writerIndex += 2;
@ -746,6 +747,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf writeMedium(int value) { public ByteBuf writeMedium(int value) {
ensureAccessible();
ensureWritable(3); ensureWritable(3);
_setMedium(writerIndex, value); _setMedium(writerIndex, value);
writerIndex += 3; writerIndex += 3;
@ -754,6 +756,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf writeInt(int value) { public ByteBuf writeInt(int value) {
ensureAccessible();
ensureWritable(4); ensureWritable(4);
_setInt(writerIndex, value); _setInt(writerIndex, value);
writerIndex += 4; writerIndex += 4;
@ -762,6 +765,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf writeLong(long value) { public ByteBuf writeLong(long value) {
ensureAccessible();
ensureWritable(8); ensureWritable(8);
_setLong(writerIndex, value); _setLong(writerIndex, value);
writerIndex += 8; writerIndex += 8;
@ -788,6 +792,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
ensureAccessible();
ensureWritable(length); ensureWritable(length);
setBytes(writerIndex, src, srcIndex, length); setBytes(writerIndex, src, srcIndex, length);
writerIndex += length; writerIndex += length;
@ -819,6 +824,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
ensureAccessible();
ensureWritable(length); ensureWritable(length);
setBytes(writerIndex, src, srcIndex, length); setBytes(writerIndex, src, srcIndex, length);
writerIndex += length; writerIndex += length;
@ -827,6 +833,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf writeBytes(ByteBuffer src) { public ByteBuf writeBytes(ByteBuffer src) {
ensureAccessible();
int length = src.remaining(); int length = src.remaining();
ensureWritable(length); ensureWritable(length);
setBytes(writerIndex, src); setBytes(writerIndex, src);
@ -837,6 +844,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public int writeBytes(InputStream in, int length) public int writeBytes(InputStream in, int length)
throws IOException { throws IOException {
ensureAccessible();
ensureWritable(length); ensureWritable(length);
int writtenBytes = setBytes(writerIndex, in, length); int writtenBytes = setBytes(writerIndex, in, length);
if (writtenBytes > 0) { if (writtenBytes > 0) {
@ -847,6 +855,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public int writeBytes(ScatteringByteChannel in, int length) throws IOException { public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
ensureAccessible();
ensureWritable(length); ensureWritable(length);
int writtenBytes = setBytes(writerIndex, in, length); int writtenBytes = setBytes(writerIndex, in, length);
if (writtenBytes > 0) { if (writtenBytes > 0) {

View File

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

View File

@ -15,8 +15,10 @@
*/ */
package io.netty.buffer; package io.netty.buffer;
import io.netty.util.IllegalReferenceCountException;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import java.util.Random; import java.util.Random;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -95,6 +97,18 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
super.testNioBufferExposeOnlyRegion(); 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 @Test
@Override @Override
public void testLittleEndianWithExpand() { public void testLittleEndianWithExpand() {