Openrtmfp/Cumulus performance optimization (1) packetreader Optimization

Source: Internet
Author: User

1. Description of packetreader methods:

1. The packetreader class inherits some methods of the POCO: binaryreader, cumulus: binaryreader class and also defines some methods. Its functions are as follows,

Poco: uint8_buff [packetrecv_size];

(1) packetreader: read32 (): reads 4 bytes of data from the current pointer position of _ buff to form a uint32 type data.

(2) packetreader: reset (INT size): locates the current pointer to the _ buff [size] position.

(3) packetreader: available (): returns the number of bytes from the byte indicated by the current pointer to the end of _ buff.

(4) packetreader: Current (): The current pointer is returned. For example, if the current pointer is _ buff [5], the pointer to _ buff [5] is returned.

(5) packetreader: Next (INT size): Skip the size byte directly from the current pointer. For example, there are 10 bytes A, B, C, D, E, F, G, h, I, J, K, the current Pointer Points to d, then after next (5), the current Pointer Points

(6) packetreader: Position (): returns the difference between the current pointer and the start pointer.

Some other functions are not listed here, but these functions are implemented based on the underlying STD: istream, STD: steambuf methods, this implementation is not efficient, so

It needs to be rewritten. After studying the functions of these functions, we found that two variables _ gpos and _ PPOs are used to directly locate the memory read pointer and write pointer locations, and the bytes read these bytes, the efficiency has been greatly improved

Some implementations are as follows:

II. The definition of packetreader. H is as follows:

# Pragma once

# Include "Cumulus. H"
# Include "memorystream. H"
# Include "binaryreader. H"
# Include "address. H"

# Define packetrecv_size 2048

Namespace cumulus {

Class packetreader: Public binaryreader {
Public:
Packetreader (const poco: uint8 * buffer, Poco: uint32 size );
Packetreader (packetreader &);
Virtual ~ Packetreader ();

Const poco: uint32 fragments;

Poco: uint32 available ();
Poco: uint8 * Current ();
Poco: uint32 position ();

Void reset (POCO: uint32 newpos = 0 );
Void shrink (POCO: uint32 rest );
Void next (POCO: uint32 size );
Public: // The following method overwrites the relevant methods of the parent class. Use _ gpos, _ PPOs to locate the read/write location and directly read bytes.
Poco: uint8 read8 ();
Poco: uint16 read16 ();
Poco: uint32 read32 ();
Poco: uint32 read7bitvalue ();
Poco: uint32 read7bitencoded ();
Poco: uint64 read7bitlongvalue ();
Bool readaddress (address & Address );
Void readraw (POCO: uint8 * value, Poco: uint32 size );
Void readraw (char * value, Poco: uint32 size );
Void readraw (POCO: uint32 size, STD: string & value );
Void readstring8 (STD: string & value );
Void readstring16 (STD: string & value );
Public:
Memoryinputstream _ memory;
 
};

Inline poco: uint32 packetreader: available (){
Return (uint32) (_ memory. _ Buf. _ buffersize-_ gpos );
}

Inline poco: uint32 packetreader: Position (){
Return _ gpos; // _ gpos is used to locate the position of the read pointer.
}

Inline void packetreader: reset (POCO: uint32 newpos ){
_ Memory. _ Buf. setggpos (newpos );
_ Memory. _ Buf. setpppos (newpos );
_ Gpos = newpos;
_ PPOs = newpos;
}

Inline void packetreader: Next (POCO: uint32 size ){
_ Gpos + = size;
_ PPOs + = size;
Return _ memory. Next (size );
}

Inline poco: uint8 * packetreader: Current (){
Return (POCO: uint8 *) _ memory. _ Buf. gcurrent ();
}

} // Namespace cumulus
Iii. Implementation of some functions of packetreader. cpp:

Poco: uint8 packetreader: read8 (){
Uint8 * PTR = (uint8 *) (_ memory. _ Buf. _ pbuffer + _ gpos );
Uint8 c = * PTR;
_ Gpos + = 1;
_ Memory. _ Buf. setggpos (_ gpos );
Return C;
}

Poco: uint16 packetreader: read16 (){
Uint8 * PTR = (uint8 *) (_ memory. _ Buf. _ pbuffer + _ gpos); // read two bytes for bit operation.
Uint16 c = * PTR;
C = C <8;
C = C | * (PTR + 1 );
_ Gpos + = 2;
_ Memory. _ Buf. setggpos (_ gpos );
Return C;
}

Poco: uint32 packetreader: read32 (){
Uint8 * PTR = (uint8 *) (_ memory. _ Buf. _ pbuffer + _ gpos); // read 4 bytes for bit operation
Uint32 C = 0;
Memcpy (& C, PTR, 4 );

_ Gpos + = 4;
_ Memory. _ Buf. setggpos (_ gpos );
Return ntohl (c); // convert to native byte order
}

Uint32 packetreader: read7bitvalue (){
Uint8 n = 0;
Uint8 B = read8 (); // call the read8 () implemented above ()
Uint32 result = 0;
While (B & 0x80) & n <3 ){
Result <= 7;
Result | = (B & 0x7f );
B = read8 ();
++ N;
}
Result <= (n <3 )? 7: 8); // use all 8 bits from the 4th byte
Result | = B;
Return result;
}

4., as follows:

1. When handling upstream packets, the rtmfp: readcrc () method packetreader: read8 (), read16 (), read32 () is called in a large number. before optimization,

Rtmfp: readcrc () function call percentage of CPU time, for example:

2. After optimization, the rtmfp: readcrc () function call percentage of CPU time is as follows:

 

Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8627430

 

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.