前言
理解零拷貝或悲,零拷貝是Netty的重要特性之一孙咪,而究竟什么是零拷貝呢堪唐?WIKI中對(duì)其有如下定義:
“Zero-copy” describes computer operations in which the CPU does not perform the task of copying data from one memory area to another.
從WIKI的定義中,我們看到“零拷貝”是指計(jì)算機(jī)操作的過程中翎蹈,CPU不需要為數(shù)據(jù)在內(nèi)存之間的拷貝消耗資源淮菠。而它通常是指計(jì)算機(jī)在網(wǎng)絡(luò)上發(fā)送文件時(shí),不需要將文件內(nèi)容拷貝到用戶空間(User Space)而直接在內(nèi)核空間(Kernel Space)中傳輸?shù)骄W(wǎng)絡(luò)的方式荤堪。
Non-Zero Copy方式:
Zero Copy方式:
從上圖中可以清楚的看到合陵,Zero Copy的模式中,避免了數(shù)據(jù)在用戶空間和內(nèi)存空間之間的拷貝澄阳,從而提高了系統(tǒng)的整體性能拥知。Linux中的sendfile()以及Java NIO中的FileChannel.transferTo()方法都實(shí)現(xiàn)了零拷貝的功能,而在Netty中也通過在FileRegion中包裝了NIO的FileChannel.transferTo()方法實(shí)現(xiàn)了零拷貝碎赢。
而在Netty中還有另一種形式的零拷貝低剔,即Netty允許我們將多段數(shù)據(jù)合并為一整段虛擬數(shù)據(jù)供用戶使用,而過程中不需要對(duì)數(shù)據(jù)進(jìn)行拷貝操作肮塞,這也是我們今天要講的重點(diǎn)户侥。我們都知道在stream-based transport(如TCP/IP)的傳輸過程中,數(shù)據(jù)包有可能會(huì)被重新封裝在不同的數(shù)據(jù)包中峦嗤,例如當(dāng)你發(fā)送如下數(shù)據(jù)時(shí):
有可能實(shí)際收到的數(shù)據(jù)如下:
因此在實(shí)際應(yīng)用中蕊唐,很有可能一條完整的消息被分割為多個(gè)數(shù)據(jù)包進(jìn)行網(wǎng)絡(luò)傳輸,而單個(gè)的數(shù)據(jù)包對(duì)你而言是沒有意義的烁设,只有當(dāng)這些數(shù)據(jù)包組成一條完整的消息時(shí)你才能做出正確的處理替梨,而Netty可以通過零拷貝的方式將這些數(shù)據(jù)包組合成一條完整的消息供你來使用。而此時(shí)装黑,零拷貝的作用范圍僅在用戶空間中副瀑。
以Netty 3.8.0.Final的源代碼來進(jìn)行說明 ###ChannelBuffer接口 Netty為需要傳輸?shù)臄?shù)據(jù)制定了統(tǒng)一的ChannelBuffer接口。該接口的主要設(shè)計(jì)思路如下:
1.使用getByte(int index)方法來實(shí)現(xiàn)隨機(jī)訪問
2.使用雙指針的方式實(shí)現(xiàn)順序訪問
每個(gè)Buffer都有一個(gè)讀指針(readIndex)和寫指針(writeIndex)
在讀取數(shù)據(jù)時(shí)讀指針后移恋谭,在寫入數(shù)據(jù)時(shí)寫指針后移
定義了統(tǒng)一的接口之后糠睡,就是來做各種實(shí)現(xiàn)了。Netty主要實(shí)現(xiàn)了HeapChannelBuffer,ByteBufferBackedChannelBuffer等等疚颊,下面我們就來講講與Zero Copy直接相關(guān)的CompositeChannelBuffer類狈孔。###CompositeChannelBuffer類 CompositeChannelBuffer類的作用是將多個(gè)ChannelBuffer組成一個(gè)虛擬的ChannelBuffer來進(jìn)行操作材义。
為什么說是虛擬的呢均抽,因?yàn)镃ompositeChannelBuffer并沒有將多個(gè)ChannelBuffer真正的組合起來,而只是保存了他們的引用其掂,這樣就避免了數(shù)據(jù)的拷貝堤魁,實(shí)現(xiàn)了Zero Copy刽沾。下面我們來看看具體的代碼實(shí)現(xiàn)排拷,首先是成員變量
private int readerIndex; private int writerIndex; private ChannelBuffer[] components; private int[] indices; private int lastAccessedComponentId;
以上這里列出了幾個(gè)比較重要的成員變量侧漓。其中readerIndex既讀指針和writerIndex既寫指針是從AbstractChannelBuffer繼承而來的;然后components是一個(gè)ChannelBuffer的數(shù)組监氢,他保存了組成這個(gè)虛擬Buffer的所有子Buffer布蔗,indices是一個(gè)int類型的數(shù)組,它保存的是各個(gè)Buffer的索引值浪腐;最后的lastAccessedComponentId是一個(gè)int值纵揍,它記錄了最后一次訪問時(shí)的子Buffer ID。
從這個(gè)數(shù)據(jù)結(jié)構(gòu)议街,我們不難發(fā)現(xiàn)所謂的CompositeChannelBuffer實(shí)際上就是將一系列的Buffer通過數(shù)組保存起來泽谨,然后實(shí)現(xiàn)了ChannelBuffer 的接口,使得在上層看來特漩,操作這些Buffer就像是操作一個(gè)單獨(dú)的Buffer一樣吧雹。
創(chuàng)建 接下來,我們?cè)倏匆幌翪ompositeChannelBuffer.setComponents方法拾稳,它會(huì)在初始化
CompositeChannelBuffer時(shí)被調(diào)用吮炕。
/**
* Setup this ChannelBuffer from the list
*/
private void setComponents(List<ChannelBuffer> newComponents) {
assert !newComponents.isEmpty();
// Clear the cache.
lastAccessedComponentId = 0;
// Build the component array.
components = new ChannelBuffer[newComponents.size()];
for (int i = 0; i < components.length; i ++) {
ChannelBuffer c = newComponents.get(i);
if (c.order() != order()) {
throw new IllegalArgumentException(
"All buffers must have the same endianness.");
}
assert c.readerIndex() == 0;
assert c.writerIndex() == c.capacity();
components[i] = c;
}
// Build the component lookup table.
indices = new int[components.length + 1];
indices[0] = 0;
for (int i = 1; i <= components.length; i ++) {
indices[i] = indices[i - 1] + components[i - 1].capacity();
}
// Reset the indexes.
setIndex(0, capacity());
}
通過代碼可以看到該方法的功能就是將一個(gè)ChannelBuffer的List給組合起來。它首先將List中得元素放入到components數(shù)組中访得,然后創(chuàng)建indices用于數(shù)據(jù)的查找,最后使用setIndex來重置指針。這里需要注意的是setIndex(0, capacity())會(huì)將讀指針設(shè)置為0悍抑,寫指針設(shè)置為當(dāng)前Buffer的長度鳄炉,這也就是前面需要做assert c.readerIndex() == 0和assert c.writerIndex() == c.capacity()這兩個(gè)判斷的原因,否則很容易會(huì)造成數(shù)據(jù)重復(fù)讀寫的問題搜骡。
所以Netty推薦我們使用ChannelBuffers.wrappedBuffer方法來進(jìn)行Buffer的合并拂盯,因?yàn)樵谠摲椒ㄖ蠳etty會(huì)通過slice()方法來確保構(gòu)建CompositeChannelBuffer是傳入的所有子Buffer都是符合要求的。
數(shù)據(jù)訪問 CompositeChannelBuffer.getByte(int index)的實(shí)現(xiàn)如下:
public byte getByte(int index) {
int componentId = componentId(index);
return components[componentId].getByte(index - indices[componentId]);
}
`
從代碼我們可以看到记靡,在隨機(jī)查找時(shí)會(huì)首先通過index獲取這個(gè)字節(jié)所在的componentId既字節(jié)所在的子Buffer序列谈竿,然后通過index - indices[componentId]計(jì)算出它在這個(gè)子Buffer中的第幾個(gè)字節(jié),然后返回結(jié)果摸吠。
下面再來看一下componentId(int index) 的實(shí)現(xiàn):
private int componentId(int index) {
int lastComponentId = lastAccessedComponentId;
if (index >= indices[lastComponentId]) {
if (index < indices[lastComponentId + 1]) {
return lastComponentId;
}
// Search right
for (int i = lastComponentId + 1; i < components.length; i ++) {
if (index < indices[i + 1]) {
lastAccessedComponentId = i;
return i;
}
}
} else {
// Search left
for (int i = lastComponentId - 1; i >= 0; i --) {
if (index >= indices[i]) {
lastAccessedComponentId = i;
return i;
}
}
}
throw new IndexOutOfBoundsException("Invalid index: " + index + ", maximum: " + indices.length);
}
從代碼中我們發(fā)現(xiàn)空凸,Netty以lastComponentId既上次訪問的子Buffer序號(hào)為中心,向左右兩邊進(jìn)行搜索寸痢,這樣做的目的是呀洲,當(dāng)我們兩次隨機(jī)查找的字符序列相近時(shí)(大部分情況下都是這樣),可以最快的搜索到目標(biāo)索引的componentId啼止。