From patchwork Wed Jul 1 20:22:35 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 490349 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id F25C21402AC for ; Thu, 2 Jul 2015 06:41:24 +1000 (AEST) Received: from localhost ([::1]:60942 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAOos-0005JO-Sd for incoming@patchwork.ozlabs.org; Wed, 01 Jul 2015 16:41:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44548) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAOX2-0003hT-MF for qemu-devel@nongnu.org; Wed, 01 Jul 2015 16:23:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZAOWx-0006El-4b for qemu-devel@nongnu.org; Wed, 01 Jul 2015 16:22:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46960) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAOWw-0006E7-S0 for qemu-devel@nongnu.org; Wed, 01 Jul 2015 16:22:51 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 5E2EBC797E; Wed, 1 Jul 2015 20:22:50 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-37.ams2.redhat.com [10.36.116.37]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t61KMkOW026448 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 1 Jul 2015 16:22:49 -0400 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 0B61B305B74E; Wed, 1 Jul 2015 22:22:39 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Wed, 1 Jul 2015 22:22:35 +0200 Message-Id: <1435782155-31412-48-git-send-email-armbru@redhat.com> In-Reply-To: <1435782155-31412-1-git-send-email-armbru@redhat.com> References: <1435782155-31412-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, berto@igalia.com, mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH RFC v2 47/47] qapi-introspect: Hide type names X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org To eliminate the temptation for clients to look up types by name (which are not ABI), replace all type names by meaningless strings. Reduces output of query-schema by 9 out of 80KiB. Signed-off-by: Markus Armbruster --- scripts/qapi-introspect.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py index 961fe88..efb34ff 100644 --- a/scripts/qapi-introspect.py +++ b/scripts/qapi-introspect.py @@ -9,13 +9,18 @@ # This work is licensed under the terms of the GNU GPL, version 2. # See the COPYING file in the top-level directory. +import string from qapi import * +def _b32digit(num): + return (string.lowercase + string.digits[2:])[num] + class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor): def __init__(self): self.schema = None self.jsons = None self.used_types = None + self.name_map = None self.defn = None self.decl = None @@ -23,13 +28,17 @@ class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor): self.schema = schema self.jsons = [] self.used_types = [] + self.name_map = {} return QAPISchemaType # don't visit types for now def visit_end(self): # visit the types that are actually used + jsons = self.jsons + self.jsons = [] for typ in self.used_types: typ.visit(self) self.jsons.sort() + jsons.extend(self.jsons) name = prefix + 'qmp_schema_json' self.decl = mcgen(''' extern char %(c_name)s[]; @@ -40,10 +49,19 @@ char %(c_name)s[] = "[" "%(c_jsons)s]"; ''', c_name=c_name(name), - c_jsons=', "\n "'.join(self.jsons)) + c_jsons=', "\n "'.join(jsons)) self.schema = None self.jsons = None self.used_types = None + self.name_map = None + + def _name(self, name): + if name not in self.name_map: + n = len(self.name_map) + self.name_map[name] = ':' + _b32digit(n / 32 / 32) \ + + _b32digit(n / 32 % 32) \ + + _b32digit(n % 32) + return self.name_map[name] def _use_type(self, typ): # Map the various integer types to plain int @@ -55,9 +73,14 @@ char %(c_name)s[] = "[" # Add type to work queue if new if typ not in self.used_types: self.used_types.append(typ) - return typ.name + # Clients should examine commands and events, not types. Hide + # type names to reduce the temptation. Also saves a few + # characters. + return self._name(typ.name) def _gen_json(self, name, mtype, extra): + if mtype != 'command' and mtype != 'event': + name = self._name(name) self.jsons.append("{ 'name': '%s', 'meta-type': '%s', %s }" % (name, mtype, extra))