zoukankan      html  css  js  c++  java
  • netty ByteBuf分析

    1.Heap Buffer(堆缓冲区)

    2.Direct Buffer(直接缓冲区)

    3.Composite Buffer(复合缓冲区)

    4.PooledByteBuf 池缓冲

        readerInex 当前读取索引
        writerIndex 当前写索引
        0~readerInex 之间被视为 discard,调用 discardReadBytes() 会释放空间
        
            @Override
            public ByteBuf discardReadBytes() {
                ensureAccessible();
                if (readerIndex == 0) {
                    return this;
                }
                
                if (readerIndex != writerIndex) {
                    setBytes(0, this, readerIndex, writerIndex - readerIndex);
                    writerIndex -= readerIndex;
                    adjustMarkers(readerIndex);
                    readerIndex = 0;
                } else {
                    adjustMarkers(readerIndex);
                    writerIndex = readerIndex = 0;
                }
                return this;
            }
            
            
            @Override
            public ByteBuf ensureWritable(int minWritableBytes) {
                if (minWritableBytes < 0) {
                    throw new IllegalArgumentException(String.format(
                            "minWritableBytes: %d (expected: >= 0)", minWritableBytes));
                }
    
                if (minWritableBytes <= writableBytes()) {
                    return this;
                }
                //写入数据长度大于剩余长度(默认int max - wirter)
                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 = calculateNewCapacity(writerIndex + minWritableBytes);
    
                // Adjust to the new capacity.
                //不同类型扩展实现 处理过程实际是直接修改JDK ByteBuffer
                capacity(newCapacity);
                return this;
            }
            
            
            private int calculateNewCapacity(int minNewCapacity) {
                final int maxCapacity = this.maxCapacity;
                final int threshold = 1048576 * 4; // 4 MiB page
    
                if (minNewCapacity == threshold) {
                    return threshold;
                }
    
                // If over threshold, do not double but just increase by threshold.
                //当超过4M时,直接扩展4M空间
                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);
            }
  • 相关阅读:
    [HNOI2004]L语言
    [TJOI2018]异或
    如何定位低效SQL?
    索引失效的情况有哪些?
    trace的作用?
    show profile的作用?
    索引的使用原则
    MySQL主从复制的步骤
    什么是聚簇索引
    什么是全文索引?
  • 原文地址:https://www.cnblogs.com/solq/p/4309455.html
Copyright © 2011-2022 走看看