From patchwork Thu Apr 30 10:11:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 466464 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 76D0C14033B for ; Thu, 30 Apr 2015 20:15:21 +1000 (AEST) Received: from localhost ([::1]:42913 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YnlV1-0005s5-Bt for incoming@patchwork.ozlabs.org; Thu, 30 Apr 2015 06:15:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41618) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YnlT3-0002AT-I3 for qemu-devel@nongnu.org; Thu, 30 Apr 2015 06:13:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YnlSz-0004Hu-I2 for qemu-devel@nongnu.org; Thu, 30 Apr 2015 06:13:17 -0400 Received: from 111.161.216.87.static.jazztel.es ([87.216.161.111]:43714 helo=fanzine.igalia.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YnlSz-0003lE-4F; Thu, 30 Apr 2015 06:13:13 -0400 Received: from dsl-hkibrasgw4-50df50-128.dhcp.inet.fi ([80.223.80.128] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim) id 1YnlRx-0005Cq-J0; Thu, 30 Apr 2015 12:12:09 +0200 Received: from berto by perseus.local with local (Exim 4.84) (envelope-from ) id 1YnlRk-00047M-Bj; Thu, 30 Apr 2015 13:11:56 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Thu, 30 Apr 2015 13:11:44 +0300 Message-Id: X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 87.216.161.111 Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 5/6] qcow2: use a hash to look for entries in the L2 cache X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The current cache algorithm traverses the array starting always from the beginning, so the average number of comparisons needed to perform a lookup is proportional to the size of the array. By using a hash of the offset as the starting point, lookups are faster and independent from the array size. The hash is computed using the cluster number of the table, multiplied by 4 to make it perform better when there are collisions. In my tests, using a cache with 2048 entries, this reduces the average number of comparisons per lookup from 430 to 2.5. Signed-off-by: Alberto Garcia --- block/qcow2-cache.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index e1bba20..c0e0278 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -237,6 +237,7 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, BDRVQcowState *s = bs->opaque; int i; int ret; + int lookup_index; uint64_t min_lru_counter = UINT64_MAX; int min_lru_index = -1; @@ -244,7 +245,8 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, offset, read_from_disk); /* Check if the table is already cached */ - for (i = 0; i < c->size; i++) { + i = lookup_index = (offset / c->table_size * 4) % c->size; + do { const Qcow2CachedTable *t = &c->entries[i]; if (t->offset == offset) { goto found; @@ -253,7 +255,10 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c, min_lru_counter = t->lru_counter; min_lru_index = i; } - } + if (++i == c->size) { + i = 0; + } + } while (i != lookup_index); if (min_lru_index == -1) { /* This can't happen in current synchronous code, but leave the check