qom: optimize casting to leaf class and parent class
PATCH 1/1 qom: optimize casting to leaf class and parent class
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.
---
Aurelien, could you please try this patch with your PPC test case?
--- qom/object.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
qom/object.c +14 -2
@@ -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 ;
Signature · 1 line
1.8.0
Quoted 52 lines · Anthony Liguori
Most QOM types use
type_register_staticbut 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 <[email protected]>Cc Aurelien Jarno <[email protected]>Cc Andreas Färber <[email protected]>Reported-by Aurelien Jarno <[email protected]>Signed-off-by Anthony Liguori <[email protected]>---
Aurelien, could you please try this patch with your PPC test case?--- qom/object.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)qom/object.c +8
-2@@ -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 ;
qom/object.c Object *object_dynamic_cast_assert(Object *obj, const char *typename) · line 470
Quoted 10 lines
qom/object.c +6
-0+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.
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Quoted 54 lines · Aurelien Jarno
Quoted 53 lines · Anthony Liguori
Most QOM types use
type_register_staticbut 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 <[email protected]>Cc Aurelien Jarno <[email protected]>Cc Andreas Färber <[email protected]>Reported-by Aurelien Jarno <[email protected]>Signed-off-by Anthony Liguori <[email protected]>---
Aurelien, could you please try this patch with your PPC test case?--- qom/object.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)qom/object.c +8
-2@@ -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 ;
Quoted 13 lines
qom/object.c Object *object_dynamic_cast_assert(Object *obj, const char *typename) · line 470
Quoted 9 lines
qom/object.c +6
-0+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_lookupandg_str_hashquite 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.
Quoted 1 line
From
269442decf0f137cf7d09c32442fcd16dd319901Mon Sep 17 00:00:00 2001
From: Anthony Liguori <[email protected]>
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.
---
v1 -> v2:
- use strcmp() instead of ==
--- qom/object.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
qom/object.c +15 -2
@@ -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 ;
Signature · 1 line
1.8.0
Regards,
Anthony Liguori
Quoted 4 lines
Signature · 3 lines
-- Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Quoted 70 lines · Anthony Liguori
Quoted 54 lines · Aurelien Jarno
Quoted 53 lines · Anthony Liguori
Most QOM types use
type_register_staticbut 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 <[email protected]>Cc Aurelien Jarno <[email protected]>Cc Andreas Färber <[email protected]>Reported-by Aurelien Jarno <[email protected]>Signed-off-by Anthony Liguori <[email protected]>---
Aurelien, could you please try this patch with your PPC test case?--- qom/object.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)qom/object.c +8
-2@@ -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 ;Quoted 13 lines
qom/object.c Object *object_dynamic_cast_assert(Object *obj, const char *typename) · line 471
Quoted 9 lines
qom/object.c +6
-0+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_lookupandg_str_hashquite high in perf top.
Quoted 6 lines
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.
It doesn't fix the issue, I don't really see any improvement, and g_hash_table_lookup + g_str_hash + __strcmp_sse42 are still high in perf top.
I have uploaded an image to reproduce the issue:
http://temp.aurel32.net/debian_squeeze_powerpc_qom_casts.qcow2
You can run it with:
qemu-system-ppc -snapshot -hda debian_squeeze_powerpc_qom_casts.qcow2
You can also add -nographic if you don't want the graphical interface, the boot process won't show up, but after half a minute, you should get to the login prompt. You can then login as user/user and run bench.sh, though if you are using perf top, the libglib-2.0 functions are visible even during the boot process.
It's a different image than the one I am using to test your patches, as I didn't have a small ppc64 image ready. That said the issue is also visible there.
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Il 11/05/2013 00:58, Anthony Liguori ha scritto:
Quoted 34 lines
Quoted 32 lines · Aurelien Jarno
Quoted 31 lines · Anthony Liguori
Most QOM types use
type_register_staticbut 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 <[email protected]>Cc Aurelien Jarno <[email protected]>Cc Andreas Färber <[email protected]>Reported-by Aurelien Jarno <[email protected]>Signed-off-by Anthony Liguori <[email protected]>---
Aurelien, could you please try this patch with your PPC test case?--- qom/object.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)qom/object.c +0
-0@@ -132,7 +132,13 @@ TypeImpl *type_register(const TypeInfo *info)
Quoted 11 lines
Quoted 11 lines
qom/object.c TypeImpl *type_register(const TypeInfo *info) · line 142
Quoted 11 lines
qom/object.c +7
-1TypeImpl *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; }
This is ok with a comment.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
qom/object.c +1
-1static 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 ;
Quoted 3 lines
Quoted 3 lines
qom/object.c Object *object_dynamic_cast_assert(Object *obj, const char *typename) · line 465
Quoted 3 lines
qom/object.c +3
-0+if (type->name == typename || type->parent == typename) { +return class; + }
I prefer my patch 3/9. With the hunk above, it works fine for the simple case of casts in a device model's callbacks (testing type->parent would almost always fail, so it is not worthwhile).
Unfortunately, strcmp is just as bad as a hashtable lookup (both are O(n) in the size the string, instead of O(1)).
Paolo
Quoted 27 lines
Quoted 9 lines
qom/object.c Object *object_dynamic_cast_assert(Object *obj, const char *typename) · line 471
Quoted 5 lines
qom/object.c +2
-0+ 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_lookupandg_str_hashquite 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.
Regards,
Anthony Liguori
Quoted 4 lines
Signature · 3 lines
-- Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Quoted 67 lines · Paolo Bonzini
Il 11/05/2013 00:58, Anthony Liguori ha scritto:
Quoted 34 lines
Quoted 32 lines · Aurelien Jarno
Quoted 31 lines · Anthony Liguori
Most QOM types use
type_register_staticbut 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 <[email protected]>Cc Aurelien Jarno <[email protected]>Cc Andreas Färber <[email protected]>Reported-by Aurelien Jarno <[email protected]>Signed-off-by Anthony Liguori <[email protected]>---
Aurelien, could you please try this patch with your PPC test case?--- qom/object.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)qom/object.c +0
-0@@ -132,7 +132,13 @@ TypeImpl *type_register(const TypeInfo *info)Quoted 11 lines
Quoted 11 lines
qom/object.c TypeImpl *type_register(const TypeInfo *info) · line 142
Quoted 11 lines
qom/object.c +7
-1TypeImpl *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; }This is ok with a comment.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
qom/object.c +1
-1static 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 ;Quoted 3 lines
Quoted 3 lines
qom/object.c Object *object_dynamic_cast_assert(Object *obj, const char *typename) · line 465
Quoted 3 lines
qom/object.c +3
-0+if (type->name == typename || type->parent == typename) { +return class; + }I prefer my patch 3/9. With the hunk above, it works fine for the simple case of casts in a device model's callbacks (testing
type->parentwould almost always fail, so it is not worthwhile).
Quoted 2 lines
Unfortunately, strcmp is just as bad as a hashtable lookup (both are O(n) in the size the string, instead of O(1)).
FWIW, I've been experimenting with Aurelien's image.
The issue here is that the PPC CPUs are 4 levels deep consisting of the leaf model, a PPC base model, the PPC CPU model, then the CPU object.
So these checks (regardless of how they're done) will never catch because we're attempting to cast the object to either the PPC CPU model or CPU object model.
I've been experimenting with a front side cache on top of the hash table. I see a nice improvement but still not as good as your patch.
I'll continue experimenting. I fear that the only way to get parity is
to expose the TypeImpl to the cast macro to avoid the hash lookup entirely. Unfortunately that's a touch every change.
Regards,
Anthony Liguori
Quoted 30 lines
Paolo
Quoted 27 lines
Quoted 9 lines
qom/object.c Object *object_dynamic_cast_assert(Object *obj, const char *typename) · line 471
Quoted 5 lines
qom/object.c +2
-0+ 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_lookupandg_str_hashquite 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.
Regards,
Anthony Liguori
Quoted 4 lines
Signature · 3 lines
-- Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net