diff mbox

[v10,05/13] qapi-visit: Simplify how we visit common union members

Message ID 1455582057-27565-6-git-send-email-eblake@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Eric Blake Feb. 16, 2016, 12:20 a.m. UTC
From: Markus Armbruster <armbru@redhat.com>

For a simple union SU, gen_visit_union() generates a visit of its
single tag member, like this:

    visit_type_SUKind(v, "type", &(*obj)->type, &err);

For a flat union FU with base B, it generates a visit of its base
fields:

    visit_type_B_fields(v, (B **)obj, &err);

Instead, we can simply visit the common members using the same fields
visit function we use for structs, generated with
gen_visit_struct_fields().  This function visits the base if any, then
the local members.

For a simple union SU, visit_type_SU_fields() contains exactly the old
tag member visit, because there is no base, and the tag member is the
only member.  For instance, the code generated for qapi-schema.json's
KeyValue changes like this:

    +static void visit_type_KeyValue_fields(Visitor *v, KeyValue **obj, Error **errp)
    +{
    +    Error *err = NULL;
    +
    +    visit_type_KeyValueKind(v, "type", &(*obj)->type, &err);
    +    error_propagate(errp, err);
    +}
    +
     void visit_type_KeyValue(Visitor *v, const char *name, KeyValue **obj, Error **errp)
     {
         Error *err = NULL;
    @@ -4863,7 +4911,7 @@ void visit_type_KeyValue(Visitor *v, con
         if (!*obj) {
             goto out_obj;
         }
    -    visit_type_KeyValueKind(v, "type", &(*obj)->type, &err);
    +    visit_type_KeyValue_fields(v, obj, &err);
         if (err) {
             goto out_obj;
         }

For a flat union FU, visit_type_FU_fields() contains exactly the old
base fields visit, because there is a base, but no members.  For
instance, the code generated for qapi-schema.json's CpuInfo changes
like this:

     static void visit_type_CpuInfoBase_fields(Visitor *v, CpuInfoBase **obj, Error **errp);

    +static void visit_type_CpuInfo_fields(Visitor *v, CpuInfo **obj, Error **errp)
    +{
    +    Error *err = NULL;
    +
    +    visit_type_CpuInfoBase_fields(v, (CpuInfoBase **)obj, &err);
    +    error_propagate(errp, err);
    +}
    +
     static void visit_type_CpuInfoX86_fields(Visitor *v, CpuInfoX86 **obj, Error **errp)
...
    @@ -3485,7 +3509,7 @@ void visit_type_CpuInfo(Visitor *v, cons
         if (!*obj) {
             goto out_obj;
         }
    -    visit_type_CpuInfoBase_fields(v, (CpuInfoBase **)obj, &err);
    +    visit_type_CpuInfo_fields(v, obj, &err);
         if (err) {
             goto out_obj;
         }

As you see, the generated code grows a bit, but in practice, it's lost
in the noise: qapi-schema.json's qapi-visit.c gains roughly 1%.

This simplification became possible with commit 441cbac "qapi-visit:
Convert to QAPISchemaVisitor, fixing bugs".  It's a step towards
unifying gen_struct() and gen_union().

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <1453902888-20457-2-git-send-email-armbru@redhat.com>
[rebase to avoid pointless gotos in new code]
Signed-off-by: Eric Blake <eblake@redhat.com>

---
v10: new patch, but effectively split out from 31/37
---
 scripts/qapi-visit.py | 27 ++++-----------------------
 1 file changed, 4 insertions(+), 23 deletions(-)

Comments

Markus Armbruster Feb. 16, 2016, 4:31 p.m. UTC | #1
Eric Blake <eblake@redhat.com> writes:

> From: Markus Armbruster <armbru@redhat.com>
>
> For a simple union SU, gen_visit_union() generates a visit of its
> single tag member, like this:
>
>     visit_type_SUKind(v, "type", &(*obj)->type, &err);
>
> For a flat union FU with base B, it generates a visit of its base
> fields:
>
>     visit_type_B_fields(v, (B **)obj, &err);
>
> Instead, we can simply visit the common members using the same fields
> visit function we use for structs, generated with
> gen_visit_struct_fields().  This function visits the base if any, then
> the local members.
>
> For a simple union SU, visit_type_SU_fields() contains exactly the old
> tag member visit, because there is no base, and the tag member is the
> only member.  For instance, the code generated for qapi-schema.json's
> KeyValue changes like this:
>
>     +static void visit_type_KeyValue_fields(Visitor *v, KeyValue **obj, Error **errp)
>     +{
>     +    Error *err = NULL;
>     +
>     +    visit_type_KeyValueKind(v, "type", &(*obj)->type, &err);
>     +    error_propagate(errp, err);
>     +}
>     +
>      void visit_type_KeyValue(Visitor *v, const char *name, KeyValue **obj, Error **errp)
>      {
>          Error *err = NULL;
>     @@ -4863,7 +4911,7 @@ void visit_type_KeyValue(Visitor *v, con
>          if (!*obj) {
>              goto out_obj;
>          }
>     -    visit_type_KeyValueKind(v, "type", &(*obj)->type, &err);
>     +    visit_type_KeyValue_fields(v, obj, &err);
>          if (err) {
>              goto out_obj;
>          }
>
> For a flat union FU, visit_type_FU_fields() contains exactly the old
> base fields visit, because there is a base, but no members.  For
> instance, the code generated for qapi-schema.json's CpuInfo changes
> like this:
>
>      static void visit_type_CpuInfoBase_fields(Visitor *v, CpuInfoBase **obj, Error **errp);
>
>     +static void visit_type_CpuInfo_fields(Visitor *v, CpuInfo **obj, Error **errp)
>     +{
>     +    Error *err = NULL;
>     +
>     +    visit_type_CpuInfoBase_fields(v, (CpuInfoBase **)obj, &err);
>     +    error_propagate(errp, err);
>     +}
>     +
>      static void visit_type_CpuInfoX86_fields(Visitor *v, CpuInfoX86 **obj, Error **errp)
> ...
>     @@ -3485,7 +3509,7 @@ void visit_type_CpuInfo(Visitor *v, cons
>          if (!*obj) {
>              goto out_obj;
>          }
>     -    visit_type_CpuInfoBase_fields(v, (CpuInfoBase **)obj, &err);
>     +    visit_type_CpuInfo_fields(v, obj, &err);
>          if (err) {
>              goto out_obj;
>          }
>
> As you see, the generated code grows a bit, but in practice, it's lost
> in the noise: qapi-schema.json's qapi-visit.c gains roughly 1%.
>
> This simplification became possible with commit 441cbac "qapi-visit:
> Convert to QAPISchemaVisitor, fixing bugs".  It's a step towards
> unifying gen_struct() and gen_union().
>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> Message-Id: <1453902888-20457-2-git-send-email-armbru@redhat.com>
> [rebase to avoid pointless gotos in new code]

Also: examples in commit message replaced by better ones :)

> Signed-off-by: Eric Blake <eblake@redhat.com>
Eric Blake Feb. 16, 2016, 5:17 p.m. UTC | #2
On 02/16/2016 09:31 AM, Markus Armbruster wrote:
> Eric Blake <eblake@redhat.com> writes:
> 
>> From: Markus Armbruster <armbru@redhat.com>
>>
>> For a simple union SU, gen_visit_union() generates a visit of its
>> single tag member, like this:
>>
>>     visit_type_SUKind(v, "type", &(*obj)->type, &err);
>>
>> For a flat union FU with base B, it generates a visit of its base
>> fields:
>>
>>     visit_type_B_fields(v, (B **)obj, &err);
>>
>> Instead, we can simply visit the common members using the same fields
>> visit function we use for structs, generated with
>> gen_visit_struct_fields().  This function visits the base if any, then
>> the local members.
>>
>> For a simple union SU, visit_type_SU_fields() contains exactly the old
>> tag member visit, because there is no base, and the tag member is the
>> only member.  For instance, the code generated for qapi-schema.json's
>> KeyValue changes like this:
>>
>>     +static void visit_type_KeyValue_fields(Visitor *v, KeyValue **obj, Error **errp)
>>     +{
>>     +    Error *err = NULL;
>>     +
>>     +    visit_type_KeyValueKind(v, "type", &(*obj)->type, &err);
>>     +    error_propagate(errp, err);
>>     +}
>>     +

This is one of the places that is several lines longer if we always emit
the goto and label.  If we drop 3 and 4, then the commit message needs a
rebase.

>>      void visit_type_KeyValue(Visitor *v, const char *name, KeyValue **obj, Error **errp)
>>      {
>>          Error *err = NULL;
>>     @@ -4863,7 +4911,7 @@ void visit_type_KeyValue(Visitor *v, con
>>          if (!*obj) {
>>              goto out_obj;
>>          }
>>     -    visit_type_KeyValueKind(v, "type", &(*obj)->type, &err);
>>     +    visit_type_KeyValue_fields(v, obj, &err);
>>          if (err) {
>>              goto out_obj;
>>          }
>>
>> For a flat union FU, visit_type_FU_fields() contains exactly the old
>> base fields visit, because there is a base, but no members.  For
>> instance, the code generated for qapi-schema.json's CpuInfo changes
>> like this:
>>
>>      static void visit_type_CpuInfoBase_fields(Visitor *v, CpuInfoBase **obj, Error **errp);
>>
>>     +static void visit_type_CpuInfo_fields(Visitor *v, CpuInfo **obj, Error **errp)
>>     +{
>>     +    Error *err = NULL;
>>     +
>>     +    visit_type_CpuInfoBase_fields(v, (CpuInfoBase **)obj, &err);
>>     +    error_propagate(errp, err);
>>     +}

And another beneficiary of the omitted goto that gets longer.

>>     +
>>      static void visit_type_CpuInfoX86_fields(Visitor *v, CpuInfoX86 **obj, Error **errp)
>> ...
>>     @@ -3485,7 +3509,7 @@ void visit_type_CpuInfo(Visitor *v, cons
>>          if (!*obj) {
>>              goto out_obj;
>>          }
>>     -    visit_type_CpuInfoBase_fields(v, (CpuInfoBase **)obj, &err);
>>     +    visit_type_CpuInfo_fields(v, obj, &err);
>>          if (err) {
>>              goto out_obj;
>>          }
>>
>> As you see, the generated code grows a bit, but in practice, it's lost
>> in the noise: qapi-schema.json's qapi-visit.c gains roughly 1%.
>>
>> This simplification became possible with commit 441cbac "qapi-visit:
>> Convert to QAPISchemaVisitor, fixing bugs".  It's a step towards
>> unifying gen_struct() and gen_union().
>>
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> Message-Id: <1453902888-20457-2-git-send-email-armbru@redhat.com>
>> [rebase to avoid pointless gotos in new code]
> 
> Also: examples in commit message replaced by better ones :)

I picked the shortest names possible, to minimize the length of the
commit message, and favoring qapi-schema.json over the testsuite.  You
noticed :)

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

Patch

diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index 0be396b..0530f2b 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -240,11 +240,8 @@  out:
     return ret


-def gen_visit_union(name, base, variants):
-    ret = ''
-
-    if base:
-        ret += gen_visit_fields_decl(base)
+def gen_visit_union(name, base, members, variants):
+    ret = gen_visit_struct_fields(name, base, members)

     for var in variants.variants:
         # Ugly special case for simple union TODO get rid of it
@@ -264,21 +261,9 @@  void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
     if (!*obj) {
         goto out_obj;
     }
+    visit_type_%(c_name)s_fields(v, obj, &err);
 ''',
                  c_name=c_name(name))
-
-    if base:
-        ret += mcgen('''
-    visit_type_%(c_name)s_fields(v, (%(c_name)s **)obj, &err);
-''',
-                     c_name=base.c_name())
-    else:
-        ret += mcgen('''
-    visit_type_%(c_type)s(v, "%(name)s", &(*obj)->%(c_name)s, &err);
-''',
-                     c_type=variants.tag_member.type.c_name(),
-                     c_name=c_name(variants.tag_member.name),
-                     name=variants.tag_member.name)
     ret += gen_err_check(label='out_obj')
     ret += mcgen('''
     if (!visit_start_union(v, !!(*obj)->u.data, &err) || err) {
@@ -378,11 +363,7 @@  class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
     def visit_object_type(self, name, info, base, members, variants):
         self.decl += gen_visit_decl(name)
         if variants:
-            if members:
-                # Members other than variants.tag_member not implemented
-                assert len(members) == 1
-                assert members[0] == variants.tag_member
-            self.defn += gen_visit_union(name, base, variants)
+            self.defn += gen_visit_union(name, base, members, variants)
         else:
             self.defn += gen_visit_struct(name, base, members)