netty里的ByteBuf扩容源码分析

  我们知道在实例化一个ByteBuf对象的时候,是可以设置一个capacity和一个maxCapacity,当writerIndex达到capacity的时候,再往里面写入内容,ByteBuf就会进行扩容。
  下面我们来看一些ByteBuf是怎样进行扩容的:
  我们先看一下调用ByteBuf的writeByte(int value)写入一个字节的数据。因为ByteBuf的writeByte(int value)是一个抽象方法,它的具体实现是在AbstractByteBuf里面

public ByteBuf writeByte(int value) {
    ensureWritable0(1);
    _setByte(writerIndex++, value);
    return this;
}

  我们能看到会先调用ensureWritable0()方法来检查是否能往里面写入数据,传入1

final void ensureWritable0(int minWritableBytes) {
     ensureAccessible();
     if (minWritableBytes <= writableBytes()) {
         return;
     }
     if (checkBounds) {
         if (minWritableBytes > maxCapacity - writerIndex) {
             throw new IndexOutOfBoundsException(String.format(
                     "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
                     writerIndex, minWritableBytes, maxCapacity, this));
         }
     }

     // Normalize the current capacity to the power of 2.
     int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);

     // Adjust to the new capacity.
    capacity(newCapacity);
}

  ensureAccessible()方法是检查refCnt是否为0,为0代表该ByteBuf对象以被释放,会抛IllegalReferenceCountException。
  接下来判断minWritableBytes是否小于或等于writableBytes,如果满足代表还有空间满足写入数据,则直接返回。如果不满足,则判断是否检查边界,checkBounds是个boolean值,可在启动JVM的时候由io.netty.buffer.checkBounds参数指定,其默认值是true。如果checkBounds为true,判断将要写入的字节数是否大于最大可写入的字节数(maxCapacity - writerIndex),如果大于直接抛异常,否则继续执行。
  接下来会调用ByteBufAllocator的calculateNewCapacity计算新的capacity。方法的实现在AbstractByteBufAllocator里面:

public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
        if (minNewCapacity < 0) {
            throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)");
        }
        if (minNewCapacity > maxCapacity) {
            throw new IllegalArgumentException(String.format(
                    "minNewCapacity: %d (expected: not greater than maxCapacity(%d)",
                    minNewCapacity, maxCapacity));
        }
        final int threshold = CALCULATE_THRESHOLD; // 4 MiB page

        if (minNewCapacity == threshold) {
            return threshold;
        }

        // If over threshold, do not double but just increase by threshold.
        if (minNewCapacity > threshold) {
            int newCapacity = minNewCapacity / threshold * threshold;
            if (newCapacity > maxCapacity - threshold) {
                newCapacity = maxCapacity;
            } else {
                newCapacity += threshold;
            }
            return newCapacity;
        }

        // Not over threshold. Double up to 4 MiB, starting from 64.
        int newCapacity = 64;
        while (newCapacity < minNewCapacity) {
            newCapacity <<= 1;
        }

        return Math.min(newCapacity, maxCapacity);
    }

  我们能看到扩容是有一个阀值(CALCULATE_THRESHOLD)的,为4MB大小,当所需容量大小(minNewCapacity)小于阀值(threshold)的时候,新的容量(newCapacity)都是是以64位基数向坐移位位计算出来的,通过循环,每次移动移1位,直到newCapacity>=minNewCapacity为止,如果计算出来newCapacity大于maxCapacity,则返回maxCapacity,否则返回newCapacity。也就是说当minNewCapacity=300的时候,newCapacity=512。
  当minNewCapacity>=threshold的时候,则先计算minNewCapacity / threshold * threshold的大小,如果这个值在加上一个threshold(4MB)大于newCapacity的时候,则newCapacity的值取maxCapacity,否则newCapacity=minNewCapacity / threshold * threshold+threshold。
  再回到ensureWritable0()方法,我们能看到,拿到计算出来的newCapacity,原后调用capacity(newCapacity)方法将新的capacity设置进去。

你可能感兴趣的:(netty里的ByteBuf扩容源码分析)