Skip to content

SmallVector for retrieval results

PAN, Myautsai edited this page Feb 4, 2016 · 1 revision

Both mc.get(key) and mc.get_multi(keys) are using the retrieval command, internally, they're using the same method(Client::get), the method may return 0, 1 or multi items. To hold variable-sized items, data type vector is used here.

When calling mc.get(key), it's likely to have 1 item returned. If using std::vector, We have to allocate memory space dynamically for the (nearly known) one item. Dynamic memory allocation(heap allocation) is quite slow. The solve the problem, we need SmallVector, which is optimized for the case when there're always just a few items. It has a few items already allocated in stack, which is faster than the heap allocation. When the size increases, it works like a normal std::vector.

The implementation used in libmc is from llvm::SmallVector.