AdaptiveRecvByteBufAllocator類詳解

1 介紹

/**
 * The {@link RecvByteBufAllocator} that automatically increases and
 * decreases the predicted buffer size on feed back.
 * <p>
 * It gradually increases the expected number of readable bytes if the previous
 * read fully filled the allocated buffer.  It gradually decreases the expected
 * number of readable bytes if the read operation was not able to fill a certain
 * amount of the allocated buffer two times consecutively.  Otherwise, it keeps
 * returning the same prediction.
 */

用來自動增加或減少可預(yù)測的buffer的大小。
當(dāng)上一次read操作填滿了所有的可分配的buffer空間,就會按照期望的增加可讀字節(jié)數(shù);當(dāng)之前連續(xù)的兩次read操作都不能填充一點數(shù)量的內(nèi)容,就會對減少可讀的字節(jié)數(shù)目。

2 屬性說明

    //最小值
    static final int DEFAULT_MINIMUM = 64;
    //默認(rèn)值
    static final int DEFAULT_INITIAL = 1024;
    //最大值
    static final int DEFAULT_MAXIMUM = 65536;
   
    //數(shù)組索引增加的步長
    private static final int INDEX_INCREMENT = 4;
    //數(shù)組索引減少的步長
    private static final int INDEX_DECREMENT = 1;

    //存放所有大小值的數(shù)組
    private static final int[] SIZE_TABLE;

3 初始化SIZE_TABLE數(shù)組

    static {
        List<Integer> sizeTable = new ArrayList<Integer>();
        for (int i = 16; i < 512; i += 16) {
            sizeTable.add(i);
        }

        for (int i = 512; i > 0; i <<= 1) {
            sizeTable.add(i);
        }

        SIZE_TABLE = new int[sizeTable.size()];
        for (int i = 0; i < SIZE_TABLE.length; i ++) {
            SIZE_TABLE[i] = sizeTable.get(i);
        }
    }

SIZE_TABLE可以分為兩部分值,前31個元素從16開始,以16的步長遞增;其余的元素從512開始每次擴大一倍,知道Intger的最大值。

4 getSizeTableIndex方法

    private static int getSizeTableIndex(final int size) {
        for (int low = 0, high = SIZE_TABLE.length - 1;;) {
            if (high < low) {
                return low;
            }
            if (high == low) {
                return high;
            }

            int mid = low + high >>> 1;
            int a = SIZE_TABLE[mid];
            int b = SIZE_TABLE[mid + 1];
            if (size > b) {
                low = mid + 1;
            } else if (size < a) {
                high = mid - 1;
            } else if (size == a) {
                return mid;
            } else {
                return mid + 1;
            }
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • https://blog.csdn.net/steven_liwen/article/details/531884...
    SkTj閱讀 2,474評論 0 16
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,259評論 0 38
  • 系統(tǒng)層面(基本不用動,看了下,買的云服務(wù)器基本都已經(jīng)優(yōu)化過了) 內(nèi)核相關(guān)參數(shù)(/etc/sysctl.conf) ...
    神奇大葉子閱讀 2,145評論 0 4
  • ORA-00001: 違反唯一約束條件 (.) 錯誤說明:當(dāng)在唯一索引所對應(yīng)的列上鍵入重復(fù)值時,會觸發(fā)此異常。 O...
    我想起個好名字閱讀 6,017評論 0 9
  • 自從上次春節(jié)回老家,在表妹家用了智能馬桶,一發(fā)不可收拾,人還沒有回到廣州,就在網(wǎng)上進行購買好了,為了節(jié)約成本,先買...
    9b76abe6cf09閱讀 233評論 0 0

友情鏈接更多精彩內(nèi)容