Use of buffer in "NIO" Dawn

Source: Internet
Author: User

In network programming, buffer is used to send the application data to the network of the broker, its important line is self-evident. When it comes to buffer, it's impossible to say 0 copies, buffer memory management or something, in Dawn, a 0-copy buffer can be automatically expanded based on Directbuffer. Its principle is a list of memory blocks, data are added at the end, removed from the head, or in the middle to get, set. All location information is obtained directly from the calculation, so the access efficiency of various situations is ensured. When the data increases, if the current memory is not enough, it will expand a block of memory, write to the new block, if the previous data consumption, you can use the Compact method, compressed storage space, discard the most previously consumed data, and put the discarded memory block back into the memory pool. This allows for maximum access efficiency with minimal data movement costs.


So, as long as you use the dawn buffer in the right posture, you are using buffer efficiently at the same time, and the bottom of dawn does its best to reduce the cost of data replication. Note: The current implementation of SCALABLEDIRECTBUF can only support Intel and AMD's CPUs, and it may crash on other CPUs, but most of the CPU in real life is Intel, so we don't have to worry about it.


Speaking a half-way, let me go back to the scalabledirectbuf of the use of the method, scalablebuf maximum capacity is unlimited, limited to physical memory, it has several member variables we must understand:

RI (abbreviation of Readindex), WI (Writeindex), Limitpos (current capacity)

RI is the location of the next readable data

WI is the next writable position

Limitpos is the current capacity.


Their size relationship: Ri<=wi<=limitpos


Scalabledirectbuf There are several types of methods:

1. Write the class method, starting with W, for example Wint is writing an integer, WI moves forward 4 bytes, and Wbyte,wlong, and so on, similar.

2. Read the class method, starting with R, such as Rlong, reading a long,ri from buffer, moving forward 8 bytes, if the buffer's readable data is less than 8 bytes, the Indexoutofbound exception is thrown.

3. Set the method to start with S, such as Sshort, starting with an offset, set a short data, RI and WI do not move.

4. Get method, start with G, such as GByte, read a byte at a location, RI and wi do not move.

5. Capacity-related functions, readable, the size of the data available for consumption in the current buffer.

6. The function associated with buffer recovery, compact, discards the data already consumed and puts the empty block back into the memory pool. Release, discard all data, and put all blocks back into the memory pool.


For examples, you can refer to Unit tests:


Https://github.com/zhmt/dawn/blob/master/examples/zhmt/dawn/nio/buffer/ScalableDirectBufTest.java


In the next article, we use a simple example to synthesize what we have learned before: we connect to the homepage of Baidu and print all the content that you get, and you will see that it is very simple.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"NIO" Dawn in buffer use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.