From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>, John Snow <jsnow@redhat.com>,
Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
Markus Armbruster <armbru@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
qemu-devel@nongnu.org,
Emanuele Giuseppe Esposito <eesposit@redhat.com>
Subject: [PATCH v3 03/10] bdrv_change_aio_context: use hash table instead of list of visited nodes
Date: Tue, 25 Oct 2022 04:49:45 -0400 [thread overview]
Message-ID: <20221025084952.2139888-4-eesposit@redhat.com> (raw)
In-Reply-To: <20221025084952.2139888-1-eesposit@redhat.com>
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 <hreitz@redhat.com>
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
---
block.c | 28 ++++++++++++++++------------
include/block/block-global-state.h | 2 +-
include/block/block_int-common.h | 3 ++-
3 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/block.c b/block.c
index 4da7526674..56c57d07fa 100644
--- a/block.c
+++ b/block.c
@@ -105,7 +105,7 @@ static void bdrv_reopen_abort(BDRVReopenState *reopen_state);
static bool bdrv_backing_overridden(BlockDriverState *bs);
static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx,
- GSList **visited, Transaction *tran,
+ GHashTable *visited, Transaction *tran,
Error **errp);
/* If non-zero, use only whitelisted block drivers */
@@ -7315,14 +7315,15 @@ typedef struct BdrvStateSetAioContext {
} BdrvStateSetAioContext;
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 = g_slist_prepend(*visited, c);
+ g_hash_table_add(visited, c);
/*
* A BdrvChildClass that doesn't handle AioContext changes cannot
@@ -7353,14 +7354,14 @@ bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx,
}
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 = g_slist_prepend(*visited, c);
+ g_hash_table_add(visited, c);
return bdrv_change_aio_context(c->bs, ctx, visited, tran, errp);
}
@@ -7445,7 +7446,7 @@ static TransactionActionDrv set_aio_context = {
* 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(BlockDriverState *bs, AioContext *ctx,
BdrvChild *ignore_child, Error **errp)
{
Transaction *tran;
- GSList *visited;
+ GHashTable *visited;
int ret;
AioContext *old_context = bdrv_get_aio_context(bs);
GLOBAL_STATE_CODE();
@@ -7536,9 +7537,12 @@ int bdrv_child_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,
* is successful (the transaction itself).
*/
tran = tran_new();
- visited = ignore_child ? g_slist_prepend(NULL, ignore_child) : NULL;
- ret = bdrv_change_aio_context(bs, ctx, &visited, tran, errp);
- g_slist_free(visited);
+ visited = g_hash_table_new(NULL, NULL);
+ if (ignore_child) {
+ g_hash_table_add(visited, ignore_child);
+ }
+ ret = bdrv_change_aio_context(bs, ctx, visited, tran, errp);
+ g_hash_table_destroy(visited);
/*
* Linear phase: go through all callbacks collected in the transaction.
diff --git a/include/block/block-global-state.h b/include/block/block-global-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, AioContext *ctx,
AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c);
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 *ctx,
BdrvChild *ignore_child, Error **errp);
diff --git a/include/block/block_int-common.h b/include/block/block_int-common.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);
bool (*change_aio_ctx)(BdrvChild *child, AioContext *ctx,
- GSList **visited, Transaction *tran, Error **errp);
+ GHashTable *visited, Transaction *tran,
+ Error **errp);
AioContext *(*get_parent_aio_context)(BdrvChild *child);
--
2.31.1
next prev parent reply other threads:[~2022-10-25 9:13 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-25 8:49 [PATCH v3 00/10] Refactor bdrv_try_set_aio_context using transactions Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 01/10] block.c: assert bs->aio_context is written under BQL and drains Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 02/10] block: use transactions as a replacement of ->{can_}set_aio_context() Emanuele Giuseppe Esposito
2022-10-25 8:49 ` Emanuele Giuseppe Esposito [this message]
2022-10-25 8:49 ` [PATCH v3 04/10] blockjob: implement .change_aio_ctx in child_job Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 05/10] block: implement .change_aio_ctx in child_of_bds Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 06/10] block-backend: implement .change_aio_ctx in child_root Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 07/10] block: use the new _change_ API instead of _can_set_ and _set_ Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 08/10] block: remove all unused ->can_set_aio_ctx and ->set_aio_ctx callbacks Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 09/10] block: rename bdrv_child_try_change_aio_context in bdrv_try_change_aio_context Emanuele Giuseppe Esposito
2022-10-25 8:49 ` [PATCH v3 10/10] block: remove bdrv_try_set_aio_context and replace it with bdrv_try_change_aio_context Emanuele Giuseppe Esposito
2022-10-25 10:18 ` [PATCH v3 00/10] Refactor bdrv_try_set_aio_context using transactions Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221025084952.2139888-4-eesposit@redhat.com \
--to=eesposit@redhat.com \
--cc=armbru@redhat.com \
--cc=hreitz@redhat.com \
--cc=jsnow@redhat.com \
--cc=kwolf@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).