qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] Stick to loops (was: [PATCH v3 1/5] qom: introduce obje


From: Daniel P. Berrange
Subject: Re: [Qemu-devel] Stick to loops (was: [PATCH v3 1/5] qom: introduce object_property_foreach method)
Date: Mon, 12 Oct 2015 11:00:04 +0100
User-agent: Mutt/1.5.24 (2015-08-30)

On Fri, Oct 09, 2015 at 10:31:26AM +0200, Markus Armbruster wrote:
> Eric Blake <address@hidden> writes:
> 
> > On 10/08/2015 08:09 AM, Daniel P. Berrange wrote:
> >> Some users of QOM need to be able to iterate over properties
> >> defined against an object instance. Currently they are just
> >> directly using the QTAIL macros against the object properties
> >> data structure.
> >> 
> >> This is bad because it exposes them to changes in the data
> >> structure used to store properties, as well as changes in
> >> functionality such as ability to register properties against
> >> the class.
> >> 
> >> Providing an explicit object_property_foreach method provides
> >> a layer of insulation between the QOM user and the QOM internal
> >> implementation.
> >> 
> >> Signed-off-by: Daniel P. Berrange <address@hidden>
> >> ---
> >>  include/qom/object.h | 23 +++++++++++++++++++++++
> >>  qom/object.c         | 17 +++++++++++++++++
> >>  2 files changed, 40 insertions(+)
> >> 
> >> diff --git a/include/qom/object.h b/include/qom/object.h
> >> index be7280c..71503af 100644
> >> --- a/include/qom/object.h
> >> +++ b/include/qom/object.h
> >> @@ -960,6 +960,29 @@ void object_property_del(Object *obj, const char 
> >> *name, Error **errp);
> >>  ObjectProperty *object_property_find(Object *obj, const char *name,
> >>                                       Error **errp);
> >>  
> >> +typedef void (*ObjectPropertyIterator)(Object *obj,
> >> +                                       ObjectProperty *prop,
> >> +                                       Error **errp,
> >> +                                       void *opaque);
> >
> > Do we want the iterator to be able to return a value, and possibly allow
> > a non-zero value to abort iteration? [1]
> >
> >> +
> >> +/**
> >> + * object_property_foreach:
> >> + * @obj: the object
> >> + * @iter: the iterator callback function
> >> + * @errp: returns an error if iterator function fails
> >> + * @opaque: opaque data to pass to @iter
> >> + *
> >> + * Iterates over all properties defined against the object
> >> + * instance calling @iter for each property.
> >
> > Probably should mention that there is an early exit if error gets set [2]
> >
> >> + *
> >> + * It is forbidden to modify the property list from @iter
> >> + * whether removing or adding properties.
> >> + */
> >> +void object_property_foreach(Object *obj,
> >> +                             ObjectPropertyIterator iter,
> >> +                             Error **errp,
> >> +                             void *opaque);
> >
> > [1] if we allow the iterator to return a non-zero value to abort
> > iteration (particularly if it wants to abort iteration without setting
> > an error, just to save CPU cycles), should this foreach() function
> > return that value?
> >
> > Of course, a caller can always use opaque to achieve the same purpose,
> > but it gets more verbose (the caller has to reserve space in their
> > opaque for tracking whether an interesting exit value is needed, as well
> > as having an early check on whether the value was already set in a
> > previous visit) and burns more CPU cycles (the iterator runs to
> > completion, even though the later callbacks are doing nothing).
> >
> >> +++ b/qom/object.c
> >> @@ -917,6 +917,23 @@ ObjectProperty *object_property_find(Object *obj, 
> >> const char *name,
> >>      return NULL;
> >>  }
> >>  
> >> +void object_property_foreach(Object *obj,
> >> +                             ObjectPropertyIterator iter,
> >> +                             Error **errp,
> >> +                             void *opaque)
> >> +{
> >> +    ObjectProperty *prop;
> >> +    Error *local_err = NULL;
> >> +
> >> +    QTAILQ_FOREACH(prop, &obj->properties, node) {
> >> +        iter(obj, prop, &local_err, opaque);
> >> +        if (local_err) {
> >> +            error_propagate(errp, local_err);
> >> +            return;
> >
> > [2] there's the early exit if an error is set.
> >
> > The code looks fine, but I think we need a documentation improvement for
> > issue [2], and we may want a design change for issue [1] if a non-void
> > return would be useful to any client later in the series.
> 
> We have quite a few _foreach-functions to help iterate over various
> things.  They are easy enough to write, but I find them awkward to use.
> 
> Implementing bdrv_next() is no harder than bdrv_iterate().  Compare:
> 
>     BlockDriverState *bdrv_next(BlockDriverState *bs)
>     {
>         if (!bs) {
>             return QTAILQ_FIRST(&bdrv_states);
>         }
>         return QTAILQ_NEXT(bs, device_list);
>     }
> 
>     void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void 
> *opaque)
>     {
>         BlockDriverState *bs;
> 
>         QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
>             it(opaque, bs);
>         }
>     }

I don't think your example here is a reasonable comparison when you consider
the full extent of this patch series. You are only having to iterate over a
single data structure here. At the end of this patch series we have to
iterate over multiple data structures spread across the object instance
and class hierarchy, so writing a 'next' like method is not as trivial
as you suggest with this comparison.

> Higher-order functions are a wonderful tool if the language is equipped
> for them.  In Lisp, for instance, you'd have everything in one place and
> no need for the awkward marshalling and unmarshalling of arguments,
> thanks to nested functions.
> 
> In C, stick to loops.  That's what the language supports.

You're really arguing against use of function callbacks in general with
this comparison to LISP. I don't think that's really practical in the
real world, as any integration with event loop needs callbacks, which
share al the same limitations as callbacks used with this foreach()
style iterator. Given this I don't think banning use of callbacks in
one specific scenario is really beneficial in general - its really
just a personal style choice.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|



reply via email to

[Prev in Thread] Current Thread [Next in Thread]