From nobody Tue Apr 15 13:26:17 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1666902926; cv=none; d=zohomail.com; s=zohoarc; b=Mgce/ftFUCWWnGd70g7Hd0LO9oWHNiaz2Pv87mizBIjrvryXAdVlZpHHvxqm5YbDepTiXDEktjj6D1rfk77aXwcP19tAKFY5wKCz4IQHxHlmZNaQQz3UMq41c4HMqyYbw6H9MYut32BzOmZttQEYtuA4SXQ2ZZuTfDPDgLYv3HQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666902926; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5OF9Eb81K7rIUOcViapjPkejE5ftvTUvO9fRnfffb/w=; b=a9P+ejb7rxV4JJ3Q7R7/q+p5+RL3r2DC4I5mfYbIlcn7ZWMk0WGUA5t4buimRfeVBS6CwvSOtIMjVsWruIQY09vWym7RTCzXyNyR6hI8FxPusPaFKMDccINZWc61Mhm695mO0QB8fmnZHgJaOTGxwss51X+ZYRzTljA7yrsnbVo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666902926316624.7696221924687; Thu, 27 Oct 2022 13:35:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oo7hV-0003z2-8s; Thu, 27 Oct 2022 14:33:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oo7gQ-0001rW-O5 for qemu-devel@nongnu.org; Thu, 27 Oct 2022 14:32:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oo7gG-0002UD-CP for qemu-devel@nongnu.org; Thu, 27 Oct 2022 14:32:50 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-215-07a4KgPQPVCZH3V5BuvOGg-1; Thu, 27 Oct 2022 14:32:36 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C7615858F17; Thu, 27 Oct 2022 18:32:35 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1059F1121320; Thu, 27 Oct 2022 18:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666895559; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5OF9Eb81K7rIUOcViapjPkejE5ftvTUvO9fRnfffb/w=; b=AKhj1lZkxr1LiUCy9XbJcVgnos9ZSLb2WSwwIhsngcK9FKGd/hOto2pDMliZAKi86pzGm2 AnmnKA8H8b52VtdWc/99WRvHp+2CGHxM8M+mR32YkBG8TCinTV0YYyPkmecxY/zO/ThR6q nXI08OC7jtqIRcMkm4eRsP3H7SILEfk= X-MC-Unique: 07a4KgPQPVCZH3V5BuvOGg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, stefanha@redhat.com, qemu-devel@nongnu.org Subject: [PULL 25/58] bdrv_change_aio_context: use hash table instead of list of visited nodes Date: Thu, 27 Oct 2022 20:31:13 +0200 Message-Id: <20221027183146.463129-26-kwolf@redhat.com> In-Reply-To: <20221027183146.463129-1-kwolf@redhat.com> References: <20221027183146.463129-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.515, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1666902928275100003 Content-Type: text/plain; charset="utf-8" From: Emanuele Giuseppe Esposito Minor performance improvement, but given that we have hash tables available, avoid iterating in the visited nodes list every time just to check if a node has been already visited. The data structure is not actually a proper hash map, but an hash set, as we are just adding nodes and not key,value pairs. Suggested-by: Hanna Reitz Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Kevin Wolf Message-Id: <20221025084952.2139888-4-eesposit@redhat.com> Signed-off-by: Kevin Wolf --- include/block/block-global-state.h | 2 +- include/block/block_int-common.h | 3 ++- block.c | 28 ++++++++++++++++------------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 7b0095b419..e7372ec541 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -233,7 +233,7 @@ bool bdrv_can_set_aio_context(BlockDriverState *bs, Aio= Context *ctx, AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c); =20 bool bdrv_child_change_aio_context(BdrvChild *c, AioContext *ctx, - GSList **visited, Transaction *tran, + GHashTable *visited, Transaction *tran, Error **errp); int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ct= x, BdrvChild *ignore_child, Error **err= p); diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 9067a99249..7ccbbdae05 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -911,7 +911,8 @@ struct BdrvChildClass { void (*set_aio_ctx)(BdrvChild *child, AioContext *ctx, GSList **ignore= ); =20 bool (*change_aio_ctx)(BdrvChild *child, AioContext *ctx, - GSList **visited, Transaction *tran, Error **er= rp); + GHashTable *visited, Transaction *tran, + Error **errp); =20 AioContext *(*get_parent_aio_context)(BdrvChild *child); =20 diff --git a/block.c b/block.c index 38e5d831ca..59319d9b0f 100644 --- a/block.c +++ b/block.c @@ -105,7 +105,7 @@ static void bdrv_reopen_abort(BDRVReopenState *reopen_s= tate); static bool bdrv_backing_overridden(BlockDriverState *bs); =20 static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx, - GSList **visited, Transaction *tran, + GHashTable *visited, Transaction *tran, Error **errp); =20 /* If non-zero, use only whitelisted block drivers */ @@ -7315,14 +7315,15 @@ typedef struct BdrvStateSetAioContext { } BdrvStateSetAioContext; =20 static bool bdrv_parent_change_aio_context(BdrvChild *c, AioContext *ctx, - GSList **visited, Transaction *= tran, + GHashTable *visited, + Transaction *tran, Error **errp) { GLOBAL_STATE_CODE(); - if (g_slist_find(*visited, c)) { + if (g_hash_table_contains(visited, c)) { return true; } - *visited =3D g_slist_prepend(*visited, c); + g_hash_table_add(visited, c); =20 /* * A BdrvChildClass that doesn't handle AioContext changes cannot @@ -7353,14 +7354,14 @@ bool bdrv_child_can_set_aio_context(BdrvChild *c, A= ioContext *ctx, } =20 bool bdrv_child_change_aio_context(BdrvChild *c, AioContext *ctx, - GSList **visited, Transaction *tran, + GHashTable *visited, Transaction *tran, Error **errp) { GLOBAL_STATE_CODE(); - if (g_slist_find(*visited, c)) { + if (g_hash_table_contains(visited, c)) { return true; } - *visited =3D g_slist_prepend(*visited, c); + g_hash_table_add(visited, c); return bdrv_change_aio_context(c->bs, ctx, visited, tran, errp); } =20 @@ -7445,7 +7446,7 @@ static TransactionActionDrv set_aio_context =3D { * responsible for freeing the list afterwards. */ static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx, - GSList **visited, Transaction *tran, + GHashTable *visited, Transaction *tran, Error **errp) { BdrvChild *c; @@ -7524,7 +7525,7 @@ int bdrv_child_try_change_aio_context(BlockDriverStat= e *bs, AioContext *ctx, BdrvChild *ignore_child, Error **err= p) { Transaction *tran; - GSList *visited; + GHashTable *visited; int ret; AioContext *old_context =3D bdrv_get_aio_context(bs); GLOBAL_STATE_CODE(); @@ -7536,9 +7537,12 @@ int bdrv_child_try_change_aio_context(BlockDriverSta= te *bs, AioContext *ctx, * is successful (the transaction itself). */ tran =3D tran_new(); - visited =3D ignore_child ? g_slist_prepend(NULL, ignore_child) : NULL; - ret =3D bdrv_change_aio_context(bs, ctx, &visited, tran, errp); - g_slist_free(visited); + visited =3D g_hash_table_new(NULL, NULL); + if (ignore_child) { + g_hash_table_add(visited, ignore_child); + } + ret =3D bdrv_change_aio_context(bs, ctx, visited, tran, errp); + g_hash_table_destroy(visited); =20 /* * Linear phase: go through all callbacks collected in the transaction. --=20 2.37.3