From patchwork Wed May 6 13:39:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 468935 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 C43D81402BF for ; Wed, 6 May 2015 23:42:51 +1000 (AEST) Received: from localhost ([::1]:45168 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ypzb7-0003ee-OV for incoming@patchwork.ozlabs.org; Wed, 06 May 2015 09:42:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39959) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpzYx-0000Aa-1o for qemu-devel@nongnu.org; Wed, 06 May 2015 09:40:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YpzYs-0001Sg-6P for qemu-devel@nongnu.org; Wed, 06 May 2015 09:40:34 -0400 Received: from smtp3.mundo-r.com ([212.51.32.191]:19241 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpzYr-0001RM-UJ; Wed, 06 May 2015 09:40:30 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtQFAEMZSlVbdWOb/2dsb2JhbABcgwyBL7NtAQEBAwUBgQKYSgKBJUwBAQEBAQGBC4QhAQEEJ1IQUTwbGYgwAcUYAQEIIoYXiicHFoQXBYwJkHSWOCNggSYdgVU7MYJFAQEB X-IPAS-Result: AtQFAEMZSlVbdWOb/2dsb2JhbABcgwyBL7NtAQEBAwUBgQKYSgKBJUwBAQEBAQGBC4QhAQEEJ1IQUTwbGYgwAcUYAQEIIoYXiicHFoQXBYwJkHSWOCNggSYdgVU7MYJFAQEB X-IronPort-AV: E=Sophos;i="5.13,379,1427752800"; d="scan'208";a="354101283" Received: from fanzine.igalia.com ([91.117.99.155]) by smtp4.mundo-r.com with ESMTP; 06 May 2015 15:39:49 +0200 Received: from maestria.local.igalia.com ([192.168.10.14] helo=mail.igalia.com) by fanzine.igalia.com with esmtps (Cipher TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim) id 1YpzYC-0003bS-VR; Wed, 06 May 2015 15:39:49 +0200 Received: from fanzine.local.igalia.com ([192.168.10.13] helo=perseus.local) by mail.igalia.com with esmtps (Cipher TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim) id 1YpzYC-0006RL-08; Wed, 06 May 2015 15:39:48 +0200 Received: from berto by perseus.local with local (Exim 4.85) (envelope-from ) id 1YpzYA-0006uH-Ls; Wed, 06 May 2015 16:39:46 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Wed, 6 May 2015 16:39:29 +0300 Message-Id: <38be38957aff5457fd811db71706a6f645df5c7e.1430919406.git.berto@igalia.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 212.51.32.191 Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 5/7] 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 Reviewed-by: Stefan Hajnoczi --- 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 dffb887..3137ba8 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -248,6 +248,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; @@ -255,7 +256,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 / s->cluster_size * 4) % c->size; + do { const Qcow2CachedTable *t = &c->entries[i]; if (t->offset == offset) { goto found; @@ -264,7 +266,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