Java Network Programming-nio and Netty (iv) Bytebuffer data processing with 0 copies __ programming

Source: Internet
Author: User
Tags assert

Related articles
Java Network Programming-nio and Netty (iv)
Java Network Programming-nio and Netty (iii)
Java Network Programming-nio and Netty (ii)
Java Network Programming-nio and Netty (i)
Java Network Programming-Cornerstone: Five IO Models and principles (multiplexing \epoll) data containers--bytebuf

The JDK's NIO uses bytebuffer as a container for network byte throttling, similar to Bytebuffer, where Bytebuff is defined as a data container.
Bytebuff is more convenient to use, more powerful and flexible, better performance.

Bytebuff manages the data by defining two indexes (Readindex, writeindex) and does not need to perform the flip () operation because the read-write index is detached.
bytebuf--Heap Buffer

The most commonly used BYTEBUF store data in a byte array in heap memory, a way called backing array.

    /**
     * Backing array: Use a byte[] array as the underlying data structure
     * * Public
    static void Heapbuffer () {
        Bytebuf heapbuf = Unpooled.buffer (1024);
        if (Heapbuf.hasarray ()) {//Determine if HEAPBUF is backing array type
            byte[] array = Heapbuf.array ()//Get the underlying array
            int offset = Heapbuf.arrayoffset () + Heapbuf.readerindex ();
            int length = Heapbuf.readablebytes ()//Get the number of readable bytes
            handlearray (array, offset, length);}}   **
bytebuf--Direct Buffer

A direct buffer is another bytebuf pattern. The Bytebuffer class introduced by NIO in JDK 1.4 allows the JVM implementation to allocate non-heap direct memory through local invocation, and is not subject to GC management.

Why use Direct memory (directmemory)
For network transport is the ideal choice. Because the JVM typically processes a network program, it shifts the data from the JVM's heap to non-heap direct memory, then to the transport layer, and so on. This means that the heap memory is typically converted to non heap memory for network transport. Then we can use the non heap memory directly to process the network transmission to be more efficient. a 0 copy of the Netty . bytebuf--Composite Buffer

COMPOSITEBYTEBUF implements merging of the two modes

public static void Bytebufcomposite () {
        Compositebytebuf messagebuf = Unpooled.compositebuffer ();
        Bytebuf headerbuf = Unpooled.copiedbuffer ("header". GetBytes ()); Can is backing or direct
        bytebuf bodybuf = Unpooled.directbuffer ();//can be backing or direct
        Bodybuf.writeby TES ("Body". GetBytes ());
        Messagebuf.addcomponents (Headerbuf, bodybuf);
        Messagebuf.removecomponent (0); Remove the header for
        (Bytebuf buf:messagebuf) {
            System.out.println (buf.tostring () + "\n\r");

            while (Buf.isreadable ()) {
                System.out.print ((char) buf.readbyte ());

            }
            System.out.println (New String (Buf.array ()));
        }
    
Bytebuf Distribution

Bytebufallocator is used to complete the BYTEBUF assignment initialization, and realizes the bytebuf of the pool. Pooledbytebufallocator and Unpooledbytebufallocator

Netty provides buffer data reuse mechanism based on memory pool, which can greatly improve performance compared to Bytebuffer.

Netty offers two implementations of Bytebufallocator: Pooledbytebufallocator and Unpooledbytebufallocator. The former pools the instance of Bytebuf to improve performance and minimize memory fragmentation. The implementation of the latter does not pool the Bytebuf instance and returns a new instance each time it is invoked.

Introduction to APIs:
Returns a bytebuf buffer () buffer (int initialcapacity) based on a heap or direct memory storage ; Buffer (int initialcapacity, int maxcapacity);

Returns a Bytebuf Heapbuffer () heapbuffer (int initialcapacity) Heapbuffer based on heap memory storage (int initialcapacity, int maxcapacity)

Returns a Bytebuf Directbuffer () directbuffer (int initialcapacity) Directbuffer based on direct memory storage (int initialcapacity, int maxcapacity)

Returns a Compositebytebu Compositebuffer () Compositebuffer () that can be extended by adding a buffer of up to a specified number of heap-based or direct-memory storage (int maxnumcomponents) Compositedirectbuffer () compositedirectbuffer (int maxnumcomponents); Compositeheapbuffer () compositeheapbuffer (int maxnumcomponents);

returns a bytebuf for the I/O operation for sockets
By default, when a running environment has sun.misc.Unsafe support, it returns the BYTEBUF based on the direct memory storage, otherwise it returns the BYTEBUF based on the heap memory storage; Iobuffer () read-Write access

/** * <pre> * GET () and set () operations, starting with the given index and keeping the index unchanged; * * <pre> * READ () and write () Action, starting at the given index and adjusting the index based on the number of bytes that have been accessed/public static void Bytebufsetget () {Charset UTF8 = Charset.fornam
        E ("UTF-8");
        Bytebuf buf = Unpooled.copiedbuffer ("Netty in Action rocks!", UTF8);
        System.out.println ((char) buf.getbyte (0));
        int readerindex = Buf.readerindex ();
        int writerindex = Buf.writerindex ();
        Buf.setbyte (0, (byte) ' B ');
        System.out.println ((char) buf.getbyte (0));
        Assert Readerindex = = Buf.readerindex ();
    Assert Writerindex = = Buf.writerindex ();
        public static void Bytebufwriteread () {Charset UTF8 = charset.forname ("UTF-8");
        Bytebuf buf = Unpooled.copiedbuffer ("Netty in Action rocks!", UTF8);
        System.out.println ((char) buf.readbyte ());
        int readerindex = Buf.readerindex ();
        int writerindex = Buf.writerindex (); Buf.writebyte (byte) '? ');
        Assert Readerindex = = Buf.readerindex ();
    Assert Writerindex!= buf.writerindex (); }

Netty uses Pooledbytebufallocator to obtain bytebuffallocator references from channel or Channelhandlercontext.

public static void Obtainingbytebufallocatorreference () {
        Channel Channel = channel_from_somewhere;//Get reference Form somewhere
        bytebufallocator allocator = Channel.alloc ();
        // ...
        Channelhandlercontext CTX = Channel_handler_context_from_somewhere; Get reference form somewhere
        bytebufallocator Allocator2 = Ctx.alloc ();
        // ...
    }
0 Copies

Direct memory is used in 1.NIO (directly Memory).
Traditional data sending requires copying data from the JVM heap memory into the system's direct memory, and then writing the data of the direct memory to the socket.
Using directmemory directly, you can reduce the copy of memory data.

2.Netty provides a combination of buffer objects.
The zero-copy of Netty is embodied in the following aspects: Netty provides a compositebytebuf class that can combine multiple bytebuf into one logical bytebuf, avoiding copies between Bytebuf . With the Wrap operation, we can wrap the byte[] array, bytebuf, Bytebuffer, and so on as a Netty Bytebuf object, thus avoiding the copy operation. BYTEBUF supports slice operations, so the BYTEBUF can be decomposed into multiple bytebuf that share the same storage area, avoiding a copy of memory. Through the Filechannel.tranferto of fileregion Packaging, the file transfer can be sent directly to the target Channel to avoid the problem of memory copy caused by the traditional cyclic write method.

Example: Transferto/transferfrom Concrete Example

Related Article

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.