Thrift Ttransport Layer Cache transport class Tbufferedtransport and buffer base class Tbufferbase

Source: Internet
Author: User
Tags execution throw exception

This section focuses on buffering-related transport classes, and caching is designed to improve the efficiency of reading and writing. Thrift a cached base class is first built when implementing cache transfers, and classes that need to implement caching can inherit directly from the base class. The following is a detailed analysis of the base class and a specific implementation class.

Cache base class Tbufferbase

The caching base class provides caching for all read and write functions of the transport class to improve performance. It typically uses memcpy to design and implement fast-path read-write access operations, which are usually small, non-virtual, and inline functions. Tbufferbase is an abstract base class, the subclass must implement the slow path of reading and writing functions, such as the slow path of reading and writing operations, such as the cache is full or empty in the case of execution. First look at the definition of the cache base class, the code is as follows:

Class Tbufferbase:public tvirtualtransport<tbufferbase> {public:uint32_t read (uint8_t* buf, uint32_t len {//Read function uint8_t* new_rbase = rbase_ + len;//get cache bounds to read if (tdb_likely (new_rbase <= rbound_)) {//To determine if the cache has enough Data readable, using branch prediction Technology std::memcpy (BUF, Rbase_, Len);//Direct Memory Copy rbase_ = new_rbase;//Update new cache read base site return len; Returns the read length} return Readslow (buf, Len);//If the cache has not been able to meet the read length need to perform slow read} uint32_t readall (uint8_t* buf, UInt32 _t len) {uint8_t* new_rbase = rbase_ + len;//The same read function if (tdb_likely (new_rbase <= rbound_)) {std::  
      memcpy (buf, Rbase_, Len);  
      Rbase_ = New_rbase;  
    return Len; Return Apache::thrift::transport::readall (*this, buf, Len);//Invoke the parent class's} void write (const uint8_t* BUF, UInt32 _t len) {//fast write function uint8_t* new_wbase = wbase_ + len;//write new cache base address if (tdb_likely (new_wbase <= wbound_)) {//Judge  
    Does the cache have enough space to write to std::memcpy (Wbase_, buf, Len);//Memory copy  Wbase_ = new_wbase;//Update Base site return;  } writeslow (buf, Len)//Low cache space call slow write function} const uint8_t* Borrow (uint8_t* buf, uint32_t* len) {//fast path Debit If (Tdb_likely (static_cast<ptrdiff_t> (*len) <= Rbound_-Rbase_))  
      {//Judge whether the length of the loan is sufficient *len = Static_cast<uint32_t> (Rbound_-Rbase_);   
    Return rbase_;//returns a borrowed base address Borrowslow (BUF, Len);//Insufficient on slow path borrow} void consume (uint32_t len) {//Consumption function if (tdb_likely (static_cast<ptrdiff_t> (len) <= Rbound_-Rbase_)) {//To determine if the cache is enough to consume rbase_ + = len;//Update has Consumed length} else {throw ttransportexception (Ttransportexception::bad_args, "C Onsume did not follow a borrow. "); /Insufficient Throw exception}} protected:virtual uint32_t Readslow (uint8_t* buf, uint32_t len) = 0;//slow function virtual void WR  
  Iteslow (const uint8_t* buf, uint32_t len) = 0;  
  Virtual Const uint8_t* Borrowslow (uint8_t* buf, uint32_t* len) = 0; Tbufferbase (): RBAse_ (NULL), Rbound_ (null), Wbase_ (null), Wbound_ (null) {}//constructor, set all cache space to NULL void setreadb Uffer (uint8_t* buf, uint32_t len) {//Set read cache space Address Rbase_ = buf;//Read cache start Address Rbound_ = buf+len;//Read cache address boundary} VO ID Setwritebuffer (uint8_t* buf, uint32_t len) {//Set write cache address space Wbase_ = buf;//Wbound_ = buf+len;//boundary} V Irtual ~tbufferbase () {} uint8_t* rbase_;//read from here uint8_t* rbound_;//read bounds uint8_t* wbase_;//write start address uint8_t* wbound_;//write Bounds};

From the tbufferbase definition can be seen, it is also inherited from the virtual class, the main use of the memcpy function to achieve fast cache reading, in determining whether there is enough cache space to operate using branch prediction technology to provide the execution efficiency of the code, and all the fast path functions are non-virtual, Inline small code measure function. Let's continue to look at a subclass of a concrete implementation cache base Class!

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.