TLS Look-up Reduction Tutorial

This task describes how to reduce Thread Local Storage (TLS) look-ups when allocating MBufs.

If you allocate an MBuf with a simple RMBufChain::Alloc() or RMBufChain::Alloc(size) call, the thread performs a context-switch to retrieve the pointer to the Comms pond from the TLS. The context-switch is a processor-intensive operation. To reduce the number of TLS look-ups, use a RMBufAllocator object, which keeps a local copy of the pointer to the pond.

The RMBufAllocator acts as a cache so that the allocation methods do not need to perform a TLS look-up each time they are called.

The classes that frequently allocate Comms chains define the RMBufAllocator object as a member, instantiate it once, and then pass it to the methods of the RMBuf and RMBufChain classes. For most of the methods that may cause an allocation, such as Alloc(), Append() or Split(), you should call the overloaded version that passes a RMBufAllocator parameter. There is no TLS look-up when you call these functions.

Reduce TLS look-up example

The following example creates an RMBufAllocator object and uses it when allocating MBufs:

class RDataSource {
   private:     
      // [...]
      RMBufAllocator iAllocator;
                        RMBufChain iReceived;

public: 
      // [...]
      TInt InitBuffer();

}

// [...]

TInt InitBuffer()
{
   // Request buffer and provide the allocator
   iReceived.Alloc(1204,iAllocator);
}

See the reference documentation of RMBuf, RMBufChain, RCommsBuf, and RCommsBufChain for the list of methods passing an allocator.

Related tasks
Allocation tutorial