[PATCH v2 05/12] qio: Minor optimization when callback function is unchanged

Eric Blake posted 12 patches 1 week ago
Maintainers: Eric Blake <eblake@redhat.com>, Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>
[PATCH v2 05/12] qio: Minor optimization when callback function is unchanged
Posted by Eric Blake 1 week ago
In qemu-nbd and other NBD server setups where parallel clients are
supported, it is common that the caller will re-register the same
callback function as long as it has not reached its limit on
simultaneous clients.  In that case, there is no need to tear down and
reinstall GSource watches in the GMainContext.

In practice, all callers currently pass NULL for notify, and no caller
ever changes context across calls (for async uses, either the caller
consistently uses qio_net_listener_set_client_func_full with the same
context, or the caller consistently uses only
qio_net_listener_set_client_func which always uses the global
context); but the time spent checking these two fields in addition to
the more important func and data is still less than the savings of not
churning through extra GSource manipulations when the result will be
unchanged.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v2: move later in series, also ensure notify and context are the same
---
 io/net-listener.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/io/net-listener.c b/io/net-listener.c
index 0f16b78fbbd..a9da8e4e069 100644
--- a/io/net-listener.c
+++ b/io/net-listener.c
@@ -148,6 +148,11 @@ void qio_net_listener_set_client_func_full(QIONetListener *listener,
 {
     size_t i;

+    if (listener->io_func == func && listener->io_data == data &&
+        listener->io_notify == notify && listener->context == context) {
+        return;
+    }
+
     trace_qio_net_listener_unwatch(listener, listener->io_func,
                                    listener->context, "set_client_func");

-- 
2.51.1
Re: [PATCH v2 05/12] qio: Minor optimization when callback function is unchanged
Posted by Daniel P. Berrangé 5 days, 8 hours ago
On Sat, Nov 08, 2025 at 04:59:26PM -0600, Eric Blake wrote:
> In qemu-nbd and other NBD server setups where parallel clients are
> supported, it is common that the caller will re-register the same
> callback function as long as it has not reached its limit on
> simultaneous clients.  In that case, there is no need to tear down and
> reinstall GSource watches in the GMainContext.
> 
> In practice, all callers currently pass NULL for notify, and no caller
> ever changes context across calls (for async uses, either the caller
> consistently uses qio_net_listener_set_client_func_full with the same
> context, or the caller consistently uses only
> qio_net_listener_set_client_func which always uses the global
> context); but the time spent checking these two fields in addition to
> the more important func and data is still less than the savings of not
> churning through extra GSource manipulations when the result will be
> unchanged.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
> ---
> v2: move later in series, also ensure notify and context are the same
> ---
>  io/net-listener.c | 5 +++++
>  1 file changed, 5 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|