On Tue, Jan 3, 2012 at 3:34 PM, Orit Wasserman <owass...@redhat.com> wrote:
> +static unsigned long cache_get_cache_pos(ram_addr_t address)
> +{
> +    unsigned long pos;
> +
> +    assert(cache_num_buckets);
> +    pos = (address/TARGET_PAGE_SIZE) & (cache_num_buckets - 1);

Where do we guarantee that cache_num_buckets is a power of 2?

> +static void cache_insert(unsigned long addr, uint8_t *pdata)
> +{
> +    unsigned long pos;
> +    int slot = -1;
> +    CacheBucket *bucket;
> +
> +    pos = cache_get_cache_pos(addr);
> +    assert(page_cache);
> +    bucket = &page_cache[pos];
> +    slot = cache_get_oldest(bucket); /* evict LRU */
> +
> +    /* actual update of entry */
> +    CacheItem *it = cache_item_get(pos, slot);
> +    if (!it->it_data) {
> +        cache_num_items++;
> +    }
> +    g_free(it->it_data);
> +
> +    it->it_data = g_malloc0(TARGET_PAGE_SIZE);
> +    memcpy(it->it_data, pdata, TARGET_PAGE_SIZE);

If we're evicting an entry:
1. Free existing data.
2. Allocate new data.
3. Zero new data.
4. Memcpy pdata into new data.

That does a bunch of extra work.  How about:
1. Memcpy pdata over old data.

So:
if (!it->it_data) {
    cache_num_items++;
    it->it_data = g_malloc(TARGET_PAGE_SIZE);
}
memcpy(it->it_data, pdata, TARGET_PAGE_SIZE);

Reply via email to