qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: Aurelien Jarno <aurelien@aurel32.net>
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
	qemu-devel@nongnu.org, "Andreas Färber" <afaerber@suse.de>
Subject: Re: [Qemu-devel] [PATCH for-1.5] qom: optimize casting to leaf class and parent class
Date: Fri, 10 May 2013 17:58:23 -0500	[thread overview]
Message-ID: <877gj65to0.fsf@codemonkey.ws> (raw)
In-Reply-To: <20130510214740.GN31148@hall.aurel32.net>

[-- Attachment #1: Type: text/plain, Size: 2837 bytes --]

Aurelien Jarno <aurelien@aurel32.net> writes:

> On Fri, May 10, 2013 at 01:47:55PM -0500, Anthony Liguori wrote:
>> Most QOM types use type_register_static but we still strdup the
>> passed data.  However, the original pointers are useful because
>> GCC is pretty good about collapsing strings so its very likely any
>> use of the pointer will end up being that same address.
>> 
>> IOW, with a little trickery, we can compare types by just comparing
>> strings and in fact that's what we do here.
>> 
>> We do this for the two most common cases, casting to a leaf class
>> or to the parent class.
>> 
>> With these two changes, I see a decrease from around 2 hash table
>> lookups to only a thousand with no run time lookups at all.
>> 
>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>> Cc: Aurelien Jarno <aurelien@aurel32.net>
>> Cc: Andreas Färber <afaerber@suse.de>
>> Reported-by: Aurelien Jarno <aurelien@aurel32.net>
>> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
>> ---
>> Aurelien, could you please try this patch with your PPC test case?
>> ---
>>  qom/object.c | 16 ++++++++++++++--
>>  1 file changed, 14 insertions(+), 2 deletions(-)
>> 
>> diff --git a/qom/object.c b/qom/object.c
>> index 75e6aac..5ecfd28 100644
>> --- a/qom/object.c
>> +++ b/qom/object.c
>> @@ -132,7 +132,13 @@ TypeImpl *type_register(const TypeInfo *info)
>>  
>>  TypeImpl *type_register_static(const TypeInfo *info)
>>  {
>> -    return type_register(info);
>> +    TypeImpl *impl;
>> +
>> +    impl = type_register(info);
>> +    impl->name = info->name;
>> +    impl->parent = info->parent;
>> +
>> +    return impl;
>>  }
>>  
>>  static TypeImpl *type_get_by_name(const char *name)
>> @@ -449,10 +455,16 @@ Object *object_dynamic_cast_assert(Object *obj, const char *typename)
>>  ObjectClass *object_class_dynamic_cast(ObjectClass *class,
>>                                         const char *typename)
>>  {
>> -    TypeImpl *target_type = type_get_by_name(typename);
>> +    TypeImpl *target_type;
>>      TypeImpl *type = class->type;
>>      ObjectClass *ret = NULL;
>>  
>> +    if (type->name == typename || type->parent == typename) {
>> +        return class;
>> +    }
>> +
>> +    target_type = type_get_by_name(typename);
>> +
>>      if (!target_type) {
>>          /* target class type unknown, so fail the cast */
>>          return NULL;
>
> Unfortunately it doesn't fix the problem. I only see a 0.5% improvement,
> which might be in the noise. I still see g_hash_table_lookup and
> g_str_hash quite high in perf top.

I was afraid of this.  I assume the cast comes somewhere other than
where the type was registered.

This patch should address that.  Could you post an image too?  Then I
don't have to keep bugging you with updated patches.


[-- Attachment #2: 0001-qom-optimize-casting-to-leaf-class-and-parent-class-.patch --]
[-- Type: text/x-diff, Size: 2466 bytes --]

>From 269442decf0f137cf7d09c32442fcd16dd319901 Mon Sep 17 00:00:00 2001
From: Anthony Liguori <aliguori@us.ibm.com>
Date: Fri, 10 May 2013 13:43:11 -0500
Subject: [PATCH] qom: optimize casting to leaf class and parent class (v2)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Most QOM types use type_register_static but we still strdup the
passed data.  However, the original pointers are useful because
GCC is pretty good about collapsing strings so its very likely any
use of the pointer will end up being that same address.

IOW, with a little trickery, we can compare types by just comparing
strings and in fact that's what we do here.

We do this for the two most common cases, casting to a leaf class
or to the parent class.

With these two changes, I see a decrease from around 2 hash table
lookups to only a thousand with no run time lookups at all.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Aurelien Jarno <aurelien@aurel32.net>
Cc: Andreas Färber <afaerber@suse.de>
Reported-by: Aurelien Jarno <aurelien@aurel32.net>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
v1 -> v2:
 - use strcmp() instead of ==
---
 qom/object.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/qom/object.c b/qom/object.c
index 75e6aac..99b5b33 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -132,7 +132,13 @@ TypeImpl *type_register(const TypeInfo *info)
 
 TypeImpl *type_register_static(const TypeInfo *info)
 {
-    return type_register(info);
+    TypeImpl *impl;
+
+    impl = type_register(info);
+    impl->name = info->name;
+    impl->parent = info->parent;
+
+    return impl;
 }
 
 static TypeImpl *type_get_by_name(const char *name)
@@ -449,10 +455,17 @@ Object *object_dynamic_cast_assert(Object *obj, const char *typename)
 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
                                        const char *typename)
 {
-    TypeImpl *target_type = type_get_by_name(typename);
+    TypeImpl *target_type;
     TypeImpl *type = class->type;
     ObjectClass *ret = NULL;
 
+    if (strcmp(type->name, typename) == 0 ||
+        strcmp(type->parent, typename) == 0) {
+        return class;
+    }
+
+    target_type = type_get_by_name(typename);
+
     if (!target_type) {
         /* target class type unknown, so fail the cast */
         return NULL;
-- 
1.8.0


[-- Attachment #3: Type: text/plain, Size: 159 bytes --]


Regards,

Anthony Liguori

>
> -- 
> Aurelien Jarno	                        GPG: 1024D/F1BCDB73
> aurelien@aurel32.net                 http://www.aurel32.net

  reply	other threads:[~2013-05-10 22:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-10 18:47 [Qemu-devel] [PATCH for-1.5] qom: optimize casting to leaf class and parent class Anthony Liguori
2013-05-10 21:47 ` Aurelien Jarno
2013-05-10 22:58   ` Anthony Liguori [this message]
2013-05-11  7:21     ` Aurelien Jarno
2013-05-11 18:35     ` Paolo Bonzini
2013-05-11 20:45       ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877gj65to0.fsf@codemonkey.ws \
    --to=aliguori@us.ibm.com \
    --cc=afaerber@suse.de \
    --cc=aurelien@aurel32.net \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).