Netty CompositeByteBuf 原理

Netty CompositeByteBuf 簡介

CompositeByteBuf 在聚合時使用,多個buffer合并時鬼悠,不需要copy,通過
CompositeByteBuf 可以把需要合并的bytebuf 組合起來,對外提供統(tǒng)一的readindex和writerindex

CompositeByteBuf 里面有個ComponentList,繼承ArrayList粗井,聚合的bytebuf都放在ComponentList里面,最小容量為16街图。

Component

compositeByteBuf 的ComponentList 里存的是byteBuf的一個包裝類Component,Component 如下:

private static final class Component {
    final ByteBuf buf;
    final int length;
    int offset;
    int endOffset;

    Component(ByteBuf buf) {
        this.buf = buf;
        length = buf.readableBytes();
    }

    void freeIfNecessary() {
        buf.release(); // We should not get a NPE here. If so, it must be a bug.
    }
}

Component 的offset是上一個的endoffset浇衬,endoffset是offset+length;

創(chuàng)建

CompositeByteBuf content = ctx.alloc().compositeBuffer(maxCumulationBufferComponents);

maxCumulationBufferComponents 是指定ComponentList的大小餐济,這樣初始化的CompositeByteBuf的是空的
,即writerindex=readerindx=0

添加ByteBuf

        content.addComponent(true, byteBuf);

添加到CompositeByteBuf 的ComponentList如下耘擂,increaseWriterIndex為true,代表更新writerIndex,cIndex為components.size()絮姆。

private int addComponent0(boolean increaseWriterIndex, int cIndex, ByteBuf buffer)    {
    assert buffer != null;
    boolean wasAdded = false;
    try {
        //檢查cindex是否超過ComponentList的容量
        checkComponentIndex(cIndex);

        int readableBytes = buffer.readableBytes();

        // No need to consolidate - just add a component to the list.
        @SuppressWarnings("deprecation")
        //封裝為Component醉冤,采用大端序
        Component c = new Component(buffer.order(ByteOrder.BIG_ENDIAN).slice());
        //比如第一次聚合的時候,cIndex=0篙悯,components.size() = 0冤灾;
        if (cIndex == components.size()) {
            wasAdded = components.add(c);
            if (cIndex == 0) {
                //如果是第一個buffer,則該Component的endOffset為buffer的大小辕近。
                c.endOffset = readableBytes;
            } else {
                //如果不是第一個buffer,則該buffer的offset為上一個的endO
                Component prev = components.get(cIndex - 1);
                c.offset = prev.endOffset;
                c.endOffset = c.offset + readableBytes;
            }
        } else {
            components.add(cIndex, c);
            wasAdded = true;
            if (readableBytes != 0) {
                updateComponentOffsets(cIndex);
            }
        }
        if (increaseWriterIndex) {
            writerIndex(writerIndex() + buffer.readableBytes());
        }
        return cIndex;
    } finally {
        if (!wasAdded) {
            buffer.release();
        }
    }
}

CompositeByteBuf 擴(kuò)容

CompositeByteBuf 在增加新的bytebuf后匿垄,會檢查CompositeByteBuf的數(shù)組components 大小移宅,默認(rèn)大小是16归粉,即在
組合了16個bytebuf后,就要擴(kuò)容了漏峰。

先說下是CompositeByteBuf怎么擴(kuò)容的糠悼,CompositeByteBuf是創(chuàng)建一個新的bytebuf,把數(shù)組里的16個bytebuf 寫到這個新創(chuàng)建bytebuf里浅乔,然后吧數(shù)組清空掉倔喂,并把新創(chuàng)建的一個大的bytebuf添加到數(shù)組里,類似歸并的做法靖苇,涉及到多次copy席噩,所以盡量不需要擴(kuò)容。

擴(kuò)容的源碼如下:

    final int numComponents = components.size();
    if (numComponents > maxNumComponents) {
        final int capacity = components.get(numComponents - 1).endOffset;
        //創(chuàng)建一個大的bytebuf贤壁,容量會所有bytebuf的總和悼枢。
        ByteBuf consolidated = allocBuffer(capacity);

        // We're not using foreach to avoid creating an iterator.
        for (int i = 0; i < numComponents; i ++) {
            Component c = components.get(i);
            ByteBuf b = c.buf;
            //copy c的數(shù)據(jù)到consolidated
            consolidated.writeBytes(b);
            // 釋放c的資源。
            c.freeIfNecessary();
        }
        Component c = new Component(consolidated);
        c.endOffset = c.length;
        components.clear();
        components.add(c);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末脾拆,一起剝皮案震驚了整個濱河市馒索,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌名船,老刑警劉巖绰上,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異渠驼,居然都是意外死亡蜈块,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門渴邦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疯趟,“玉大人,你說我怎么就攤上這事谋梭⌒啪” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵瓮床,是天一觀的道長盹舞。 經(jīng)常有香客問我,道長隘庄,這世上最難降的妖魔是什么踢步? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮丑掺,結(jié)果婚禮上获印,老公的妹妹穿的比我還像新娘。我一直安慰自己街州,他們只是感情好兼丰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布玻孟。 她就那樣靜靜地躺著,像睡著了一般鳍征。 火紅的嫁衣襯著肌膚如雪黍翎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天艳丛,我揣著相機(jī)與錄音匣掸,去河邊找鬼。 笑死氮双,一個胖子當(dāng)著我的面吹牛碰酝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播眶蕉,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼砰粹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了造挽?” 一聲冷哼從身側(cè)響起碱璃,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎饭入,沒想到半個月后嵌器,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡谐丢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年爽航,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乾忱。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡讥珍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出窄瘟,到底是詐尸還是另有隱情衷佃,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布蹄葱,位于F島的核電站氏义,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏图云。R本人自食惡果不足惜惯悠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望竣况。 院中可真熱鬧克婶,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至紫岩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間睬塌,已是汗流浹背泉蝌。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留揩晴,地道東北人勋陪。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像硫兰,于是被迫代替她去往敵國和親诅愚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容