Skip to content

Pre allocation in BufferReader

PAN, Myautsai edited this page Feb 4, 2016 · 2 revisions

BufferReader is the helper of where memcached response buffer are stored. Each BufferReader contains a list of DataBlocks. The length of the list is 0 after initialization of a BufferReader.

During each request(mc.get / mc.set/ ...), the DataBlock list will stretch if essential. The minimum capacity of each DataBlock is 8192. When free space of a DataBlock is too small to storage the response buffer, the free space will be used first, and a new DataBlock will be created and appended to the DataBlock list. The capacity of the newly created DataBlock depends on whether the required buffer space is known. e.g.: When processing the response of a GET command, we can get the rest buffer size after read first few bytes. If the rest space is not large enough to storage the rest buffer, we'll create a DataBlock whose capacity = rest buffer size - rest space.

After each request, the DataBlock list will be resized to 1, which contains only 1 DataBlock of capacity 8192. In most conditions, 8192 bytes is large enough to storage the response of a single key retrieval from memcached server, and no memory allocation or memory copying will happen.