Let the C generated bits of QAPI be generated only once when identical includes are done
PATCH v3 1/1 Let the C generated bits of QAPI be generated only once when identical includes are done
in V3:
change commit title [Eric]
s/spitted/emitted/
change tests names [Eric]
Benoît Canet (1):
qapi: skip redundant includes
scripts/qapi.py | 14 +++++++++++--- tests/Makefile | 3 ++- tests/qapi-schema/include-repetition-sub.json | 2 ++ tests/qapi-schema/include-repetition.err | 0 tests/qapi-schema/include-repetition.exit | 1 + tests/qapi-schema/include-repetition.json | 3 +++ tests/qapi-schema/include-repetition.out | 3 +++ 7 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/qapi-schema/include-repetition-sub.json create mode 100644 tests/qapi-schema/include-repetition.err create mode 100644 tests/qapi-schema/include-repetition.exit create mode 100644 tests/qapi-schema/include-repetition.json create mode 100644 tests/qapi-schema/include-repetition.out
Signature · 1 line
2.0.0.rc2
PATCH v3 1/1 qapi: skip redundant includes
The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be emitted only one time.
A second history global to all QAPISchema instances has been added to detect when a file is included more than one time and skip these includes.
It does not act as a stack and the changes made to it by the __init__ function are propagated back to the caller so it's really a global state.
--- scripts/qapi.py | 14 +++++++++++--- tests/Makefile | 3 ++- tests/qapi-schema/include-repetition-sub.json | 2 ++ tests/qapi-schema/include-repetition.err | 0 tests/qapi-schema/include-repetition.exit | 1 + tests/qapi-schema/include-repetition.json | 3 +++ tests/qapi-schema/include-repetition.out | 3 +++ 7 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/qapi-schema/include-repetition-sub.json create mode 100644 tests/qapi-schema/include-repetition.err create mode 100644 tests/qapi-schema/include-repetition.exit create mode 100644 tests/qapi-schema/include-repetition.json create mode 100644 tests/qapi-schema/include-repetition.out
scripts/qapi.py +11 -3
@@ -73,13 +73,18 @@ class QAPIExprError(Exception): class QAPISchema: - def __init__(self, fp, input_relname=None, include_hist=[], parent_info=None): + def __init__(self, fp, input_relname=None, include_hist=[], + previously_included=[], parent_info=None): + """ include_hist is a stack used to detect inclusion cycles + previously_included is a global state used to avoid multiple + inclusions of the same file""" input_fname = os.path.abspath(fp.name) if input_relname is None: input_relname = fp.name self.input_dir = os.path.dirname(input_fname) self.input_file = input_relname self.include_hist = include_hist + [(input_relname, input_fname)] + previously_included.append(input_fname) self.parent_info = parent_info self.src = fp.read() if self.src == '' or self.src[-1] != '\n':
@@ -106,13 +111,16 @@ class QAPISchema: for elem in self.include_hist): raise QAPIExprError(expr_info, "Inclusion loop for %s" % include) + # skip multiple include of the same file + if include_path in previously_included: + continue try: fobj = open(include_path, 'r') except IOError as e: raise QAPIExprError(expr_info, '%s: %s' % (e.strerror, include)) - exprs_include = QAPISchema(fobj, include, - self.include_hist, expr_info) + exprs_include = QAPISchema(fobj, include, self.include_hist, + previously_included, expr_info) self.exprs.extend(exprs_include.exprs) else: expr_elem = {'expr': expr,
tests/Makefile +2 -1
@@ -182,7 +182,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \ flat-union-string-discriminator.json \ include-simple.json include-relpath.json include-format-err.json \ include-non-file.json include-no-file.json include-before-err.json \ - include-nested-err.json include-self-cycle.json include-cycle.json) + include-nested-err.json include-self-cycle.json include-cycle.json \ + include-repetition.json) GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
tests/qapi-schema/include-repetition-sub.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'include': 'comments.json' } +{ 'include': 'comments.json' }
tests/qapi-schema/include-repetition.err new file +0 -0
tests/qapi-schema/include-repetition.exit new file +1 -0
@@ -0,0 +1 @@ +0
tests/qapi-schema/include-repetition.json new file +3 -0
@@ -0,0 +1,3 @@ +{ 'include': 'comments.json' } +{ 'include': 'include-repetition-sub.json' } +{ 'include': 'comments.json' }
tests/qapi-schema/include-repetition.out new file +3 -0
@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}] +[]
Signature · 1 line
2.0.0.rc2
Re: [Qemu-devel] [PATCH v3] qapi: skip redundant includes
Quoted 12 lines · Benoît Canet
The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be emitted only one time.
A second history global to all QAPISchema instances has been added to detect when a file is included more than one time and skip these includes.
It does not act as a stack and the changes made to it by the__init__function are propagated back to the caller so it's really a global state.Signed-off-by Benoit Canet <[email protected]>
Quoted 15 lines
--- scripts/qapi.py | 14 +++++++++++--- tests/Makefile | 3 ++- tests/qapi-schema/include-repetition-sub.json | 2 ++ tests/qapi-schema/include-repetition.err | 0 tests/qapi-schema/include-repetition.exit | 1 + tests/qapi-schema/include-repetition.json | 3 +++ tests/qapi-schema/include-repetition.out | 3 +++ 7 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/qapi-schema/include-repetition-sub.json create mode 100644 tests/qapi-schema/include-repetition.err create mode 100644 tests/qapi-schema/include-repetition.exit create mode 100644 tests/qapi-schema/include-repetition.json create mode 100644 tests/qapi-schema/include-repetition.out
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v3] qapi: skip redundant includes
In reply to Benoît CanetQuoted 10 lines · On Wed, 14 May 2014 16:07:49 +0200 Benoît Canet <[email protected]>…
The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be emitted only one time.
A second history global to all QAPISchema instances has been added to detect when a file is included more than one time and skip these includes.
It does not act as a stack and the changes made to it by the__init__function are propagated back to the caller so it's really a global state.
Shouldn't this behavior be noted somewhere? Eg. in writing-qmp-commands.txt or qapi-code-gen.txt?
Quoted 110 lines
Signed-off-by Benoit Canet <[email protected]>--- scripts/qapi.py | 14 +++++++++++--- tests/Makefile | 3 ++- tests/qapi-schema/include-repetition-sub.json | 2 ++ tests/qapi-schema/include-repetition.err | 0 tests/qapi-schema/include-repetition.exit | 1 + tests/qapi-schema/include-repetition.json | 3 +++ tests/qapi-schema/include-repetition.out | 3 +++ 7 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/qapi-schema/include-repetition-sub.json create mode 100644 tests/qapi-schema/include-repetition.err create mode 100644 tests/qapi-schema/include-repetition.exit create mode 100644 tests/qapi-schema/include-repetition.json create mode 100644 tests/qapi-schema/include-repetition.outscripts/qapi.py +11
-3@@ -73,13 +73,18 @@ class QAPIExprError(Exception): class QAPISchema: - def __init__(self, fp, input_relname=None, include_hist=[], parent_info=None): + def __init__(self, fp, input_relname=None, include_hist=[], + previously_included=[], parent_info=None): + """ include_hist is a stack used to detect inclusion cycles + previously_included is a global state used to avoid multiple + inclusions of the same file""" input_fname = os.path.abspath(fp.name) if input_relname is None: input_relname = fp.name self.input_dir = os.path.dirname(input_fname) self.input_file = input_relname self.include_hist = include_hist + [(input_relname, input_fname)] + previously_included.append(input_fname) self.parent_info = parent_info self.src = fp.read() if self.src == '' or self.src[-1] != '\n':@@ -106,13 +111,16 @@ class QAPISchema: for elem in self.include_hist): raise QAPIExprError(expr_info, "Inclusion loop for %s" % include) + # skip multiple include of the same file + if include_path in previously_included: + continue try: fobj = open(include_path, 'r') except IOError as e: raise QAPIExprError(expr_info, '%s: %s' % (e.strerror, include)) - exprs_include = QAPISchema(fobj, include, - self.include_hist, expr_info) + exprs_include = QAPISchema(fobj, include, self.include_hist, + previously_included, expr_info) self.exprs.extend(exprs_include.exprs) else: expr_elem = {'expr': expr,tests/Makefile +2
-1@@ -182,7 +182,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \ flat-union-string-discriminator.json \ include-simple.json include-relpath.json include-format-err.json \ include-non-file.json include-no-file.json include-before-err.json \ - include-nested-err.json include-self-cycle.json include-cycle.json) + include-nested-err.json include-self-cycle.json include-cycle.json \ + include-repetition.json) GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.htests/qapi-schema/include-repetition-sub.json new file +2
-0@@ -0,0 +1,2 @@ +{ 'include': 'comments.json' } +{ 'include': 'comments.json' }tests/qapi-schema/include-repetition.err new file +0
-0tests/qapi-schema/include-repetition.exit new file +1
-0@@ -0,0 +1 @@ +0tests/qapi-schema/include-repetition.json new file +3
-0@@ -0,0 +1,3 @@ +{ 'include': 'comments.json' } +{ 'include': 'include-repetition-sub.json' } +{ 'include': 'comments.json' }tests/qapi-schema/include-repetition.out new file +3
-0@@ -0,0 +1,3 @@ +[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])] +[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}] +[]
Re: [Qemu-devel] [PATCH v3] qapi: skip redundant includes
Quoted 14 lines · Luiz Capitulino
Quoted 10 lines · On Wed, 14 May 2014 16:07:49 +0200 Benoît Canet <[email protected]>…
The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be emitted only one time.
A second history global to all QAPISchema instances has been added to detect when a file is included more than one time and skip these includes.
It does not act as a stack and the changes made to it by the__init__function are propagated back to the caller so it's really a global state.
Quoted 2 lines
Shouldn't this behavior be noted somewhere? Eg. in writing-qmp-commands.txt or qapi-code-gen.txt?
dosc/qapi-code-gen.txt already covers the {'include':'file'} syntax, so yes, that should be updated to mention that multiple includes of the same file are now safe.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v3] qapi: skip redundant includes
Quoted 15 lines · Eric Blake
Quoted 14 lines · Luiz Capitulino
Quoted 10 lines · On Wed, 14 May 2014 16:07:49 +0200 Benoît Canet <[email protected]>…
The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be emitted only one time.
A second history global to all QAPISchema instances has been added to detect when a file is included more than one time and skip these includes.
It does not act as a stack and the changes made to it by the__init__function are propagated back to the caller so it's really a global state.
Quoted 6 lines
Quoted 2 lines
Shouldn't this behavior be noted somewhere? Eg. in writing-qmp-commands.txt or qapi-code-gen.txt?
dosc/qapi-code-gen.txtalready covers the {'include':'file'} syntax, so yes, that should be updated to mention that multiple includes of the same file are now safe.
ok, I will do this.
Best regards
Benoît
Quoted 5 lines
Signature · 3 lines
-- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org