On 2/8/19 11:26 AM, Peng Yu wrote: >> Come on. If you're not going to read the code, at least read the comments. > > This is what I don't understand. Why caching pointers not caching the > actual memory allocated to the string can improve the performance? > >> /* Create an object cache C of N pointers to OTYPE. */ >> >> /* Free all cached items, which are pointers to OTYPE, in object cache C. */
A cache of pointers means that the next time you need a fresh pointer to the same object type, you can reuse the allocated memory still pointed to by an older pointer already found in the cache (fast) rather than having to call malloc() (slow). Since all of the pointers are pointing to memory of the same size (namely, sizeof(OTYPE)), you don't have to worry about whether your reuse of the area is going to overwrite more memory than the previous use of that area. Apparently, you are confused on the data type being cached - it is NOT a cache of the user-supplied strings, but of the WORD_DESC and WORD_LIST structures used to manage groups of user-supplied strings. While we have to eat the cost of malloc()ing user-supplied strings of unknown lengths, the cache at least lets us avoid even more malloc()s of the fixed-size management data structures. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature