Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Zero copy Mechanism in Netty

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to understand the zero-copy mechanism in Netty". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Understand Zero copy

Zero copy is one of the important features of Netty, but what exactly is zero copy?

It is defined in WIKI as follows:

"Zero-copy" describes computer operations in which the CPU does not perform the task of copying data from one memory area to another.

From the definition of WIKI, we see that "zero copy" means that during the operation of a computer, CPU does not need to consume resources for copying data between memory. It usually refers to the way that when a computer sends a file on the network, it does not need to copy the contents of the file to the user space (User Space) but directly transmits it to the network in the kernel space (Kernel Space).

Non-Zero Copy mode:

In Zero Copy mode, the copy of data between user space and memory space is avoided, thus improving the overall performance of the system. Both the sendfile () in Linux and the FileChannel.transferTo () method in Java NIO implement the zero-copy function, and in Netty, zero-copy is also achieved by wrapping the FileChannel.transferTo () method of NIO in FileRegion.

There is another form of zero copy in Netty, that is, Netty allows us to merge multiple pieces of data into a whole piece of virtual data for users to use, and there is no need to copy the data in the process, which is what we are going to talk about today. We all know that during the transmission of stream-based transport (such as TCP/IP), packets may be re-encapsulated in different packets, such as when you send the following data:

It is possible that the actual data received is incomplete.

Therefore, in practical application, it is very possible that a complete message is divided into multiple data packets for network transmission, and a single data packet is meaningless to you. Only when these data packets form a complete message can you make correct processing, and Netty can combine these data packets into a complete message for you to use. At this point, the scope of zero copy is only in user space.

Implementation Mechanism of Zero copy in Netty3

The following is illustrated by the source code of Netty 3.8.0.Final

ChannelBuffer interface

Netty establishes a unified ChannelBuffer interface for the data that needs to be transmitted. The main design ideas of this interface are as follows:

Use the getByte (int index) method to achieve random access

Using double pointers to achieve sequential access

Each Buffer has a read pointer (readIndex) and a write pointer (writeIndex)

Move the read pointer backward when reading data and write pointer backward when writing data

在此输入图片描述

After defining a unified interface, it is time to do a variety of implementations. Netty mainly implements HeapChannelBuffer,ByteBufferBackedChannelBuffer and so on, so let's talk about the CompositeChannelBuffer class that is directly related to Zero Copy.

CompositeChannelBuffer class

The function of the CompositeChannelBuffer class is to combine multiple ChannelBuffer into a virtual ChannelBuffer for operation. Why is it virtual? because CompositeChannelBuffer doesn't really combine multiple ChannelBuffer, but just saves their references, so it avoids copying data and implements Zero Copy.

Let's take a look at the specific code implementation, starting with member variables.

Private int readerIndex;private int writerIndex;private ChannelBuffer [] components;private int [] indices;private int lastAccessedComponentId

Some of the more important member variables are listed above. Where the readerIndex read pointer and the writerIndex write pointer are inherited from AbstractChannelBuffer; then components is an array of ChannelBuffer, which holds that all the child Buffer,indices that makes up the virtual Buffer is an array of int type, which holds the index value of each Buffer; and the final lastAccessedComponentId is an int value, which records the child Buffer ID on the last access. From this data structure, it is not difficult to find that the so-called CompositeChannelBuffer actually stores a series of Buffer through an array, and then implements the interface of ChannelBuffer, so that in the upper layer, operating these Buffer is like operating a single Buffer.

Create

Next, let's take a look at the CompositeChannelBuffer.setComponents method, which is called when the CompositeChannelBuffer is initialized.

/ * Setup this ChannelBuffer from the list * / private void setComponents (List 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 = 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);}

From the code, we find that Netty searches the left and right sides with the sequence number of the sub-Buffer visited by lastComponentId as the center, so that when the character sequence we randomly look for is similar (in most cases), we can search the componentId of the target index as quickly as possible.

This is the end of "how to understand the Zero copy Mechanism in Netty". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report