From patchwork Mon May 13 20:31:45 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 243528 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 71DAF2C009F for ; Tue, 14 May 2013 06:32:29 +1000 (EST) Received: from localhost ([::1]:52361 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UbzQ3-0008M3-KO for incoming@patchwork.ozlabs.org; Mon, 13 May 2013 16:32:27 -0400 Received: from eggs.gnu.org ([208.118.235.92]:42725) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UbzPk-0008Kc-CF for qemu-devel@nongnu.org; Mon, 13 May 2013 16:32:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UbzPj-00013V-3X for qemu-devel@nongnu.org; Mon, 13 May 2013 16:32:08 -0400 Received: from e23smtp06.au.ibm.com ([202.81.31.148]:51801) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UbzPi-000137-HJ for qemu-devel@nongnu.org; Mon, 13 May 2013 16:32:07 -0400 Received: from /spool/local by e23smtp06.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 14 May 2013 06:25:42 +1000 Received: from d23dlp03.au.ibm.com (202.81.31.214) by e23smtp06.au.ibm.com (202.81.31.212) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 14 May 2013 06:25:40 +1000 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [9.190.235.21]) by d23dlp03.au.ibm.com (Postfix) with ESMTP id 4A63A3578018 for ; Tue, 14 May 2013 06:31:58 +1000 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r4DKVpvt20250836 for ; Tue, 14 May 2013 06:31:51 +1000 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r4DKVvDD032632 for ; Tue, 14 May 2013 06:31:57 +1000 Received: from titi.austin.rr.com (sig-9-65-228-90.mts.ibm.com [9.65.228.90]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r4DKVtG3032615; Tue, 14 May 2013 06:31:55 +1000 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Mon, 13 May 2013 15:31:45 -0500 Message-Id: <1368477106-7579-1-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.8.0 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13051320-7014-0000-0000-000003038F1C X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.148 Cc: Paolo Bonzini , Anthony Liguori , Andreas Faerber , Aurelien Jarno Subject: [Qemu-devel] [PATCH for-1.5 1/2] qom: aggressively optimize qom casting 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 This patch adds a small typename cache to ObjectClass. This allows caching positive casts within each ObjectClass. Benchmarking a PPC workload provided by Aurelien, this patch eliminates every single g_hash_table_lookup() happening during the benchmark (which was about 2 million per-second). With this patch applied, I get exactly the same performance (within the margin of error) as with --disable-qom-cast-debug. N.B. it's safe to cache typenames only from the _assert() macros because they are always called with string literals. Signed-off-by: Anthony Liguori Reviewed-by: Paolo Bonzini --- include/qom/object.h | 4 ++++ qom/object.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/include/qom/object.h b/include/qom/object.h index 63e2a40..23fc048 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -344,6 +344,8 @@ typedef void (ObjectUnparent)(Object *obj); */ typedef void (ObjectFree)(void *obj); +#define OBJECT_CLASS_CAST_CACHE 4 + /** * ObjectClass: * @@ -356,6 +358,8 @@ struct ObjectClass Type type; GSList *interfaces; + const char *cast_cache[OBJECT_CLASS_CAST_CACHE]; + ObjectUnparent *unparent; }; diff --git a/qom/object.c b/qom/object.c index f5f416b..ec88231 100644 --- a/qom/object.c +++ b/qom/object.c @@ -439,7 +439,16 @@ Object *object_dynamic_cast_assert(Object *obj, const char *typename, typename, file, line, func); #ifdef CONFIG_QOM_CAST_DEBUG - Object *inst = object_dynamic_cast(obj, typename); + int i; + Object *inst; + + for (i = 0; i < OBJECT_CLASS_CAST_CACHE; i++) { + if (obj->class->cast_cache[i] == typename) { + goto out; + } + } + + inst = object_dynamic_cast(obj, typename); if (!inst && obj) { fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", @@ -448,6 +457,15 @@ Object *object_dynamic_cast_assert(Object *obj, const char *typename, } assert(obj == inst); + + if (obj == inst) { + for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { + obj->class->cast_cache[i - 1] = obj->class->cast_cache[i]; + } + obj->class->cast_cache[i - 1] = typename; + } + +out: #endif return obj; } @@ -510,7 +528,16 @@ ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", typename, file, line, func); -#ifndef CONFIG_QOM_CAST_DEBUG +#ifdef CONFIG_QOM_CAST_DEBUG + int i; + + for (i = 0; i < OBJECT_CLASS_CAST_CACHE; i++) { + if (class->cast_cache[i] == typename) { + ret = class; + goto out; + } + } +#else if (!class->interfaces) { return class; } @@ -523,6 +550,15 @@ ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, abort(); } +#ifdef CONFIG_QOM_CAST_DEBUG + if (ret == class) { + for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { + class->cast_cache[i - 1] = class->cast_cache[i]; + } + class->cast_cache[i - 1] = typename; + } +out: +#endif return ret; }