From patchwork Tue Jul 14 09:39:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Fedin X-Patchwork-Id: 494952 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 A17E0140761 for ; Tue, 14 Jul 2015 19:39:43 +1000 (AEST) Received: from localhost ([::1]:58707 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEwgf-0001J3-SE for incoming@patchwork.ozlabs.org; Tue, 14 Jul 2015 05:39:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54046) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEwgA-0000Nf-Tg for qemu-devel@nongnu.org; Tue, 14 Jul 2015 05:39:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZEwg5-0001J0-TC for qemu-devel@nongnu.org; Tue, 14 Jul 2015 05:39:10 -0400 Received: from mailout4.w1.samsung.com ([210.118.77.14]:15718) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEwg5-0001Im-LV for qemu-devel@nongnu.org; Tue, 14 Jul 2015 05:39:05 -0400 Received: from eucpsbgm2.samsung.com (unknown [203.254.199.245]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0NRH00F2G1H3AS50@mailout4.w1.samsung.com> for qemu-devel@nongnu.org; Tue, 14 Jul 2015 10:39:03 +0100 (BST) X-AuditID: cbfec7f5-f794b6d000001495-b3-55a4d8b72fc5 Received: from eusync4.samsung.com ( [203.254.199.214]) by eucpsbgm2.samsung.com (EUCPMTA) with SMTP id 0C.A8.05269.7B8D4A55; Tue, 14 Jul 2015 10:39:03 +0100 (BST) Received: from localhost ([106.109.131.169]) by eusync4.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0NRH00GES1H31R40@eusync4.samsung.com>; Tue, 14 Jul 2015 10:39:03 +0100 (BST) From: Pavel Fedin To: qemu-devel@nongnu.org Date: Tue, 14 Jul 2015 12:39:01 +0300 Message-id: <507c11db2c97eef33de0e4f7168076d5c39f0867.1436866326.git.p.fedin@samsung.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-reply-to: References: In-reply-to: References: X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupmluLIzCtJLcpLzFFi42I5/e/4Nd3tN5aEGuzeJ2XRvKnY4ubVo0wW x3t3sDgwezy5tpnJY/Ppao+9n3+zBDBHcdmkpOZklqUW6dslcGVsuzmbveC8bMXdhU2sDYwL RLsYOTkkBEwklp7fwgxhi0lcuLeeDcQWEljKKPHwZQ2E/Y1RYuMaORCbTUBd4vTXDywgtoiA pMTvrtNgvcwCORI9E3aA9QoLeEl8O7MHrIZFQFXi3rP17CA2r0C0xPYnG1kgdmlILPoyB6ye U8BcYtqSw4wQu8wkvv1+yI5LfAIj/wJGhlWMoqmlyQXFSem5RnrFibnFpXnpesn5uZsYIUH0 dQfj0mNWhxgFOBiVeHgnWC4JFWJNLCuuzD3EKMHBrCTCa7ERKMSbklhZlVqUH19UmpNafIhR moNFSZx35q73IUIC6YklqdmpqQWpRTBZJg5OqQZG26jflZa1842CZ03oul17fhajLLu76JFH b/SSp7z1NZ+032Xn+m/3D7Xd+8S2OW7tOg57Yd1HYi/X/z7uxaOo9TDPiu/0ZxcG3845G0sS FrszcIvez/Hpy/9faGsiofCiz9Y++2/K3Us3PA88Ss0+pWCcldrkfL/ny8W7Gt8cvh/hL8j9 LXZeiaU4I9FQi7moOBEA2VP1YB4CAAA= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 210.118.77.14 Cc: Peter Crosthwaite , =?UTF-8?q?Andreas=20F=C3=A4rber?= Subject: [Qemu-devel] [PATCH v4 2/2] QOM: object_property_add() performance improvement 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 Avoid repetitive lookup of every property in array starting from 0 by adding one more property which caches last used index. Every time an array is expanded the index is picked up from this cache. The property is a uint32_t and its name is name of the array plus '#' ('name#'). It has getter function in order to allow to inspect it from within monitor. Another optimization includes avoiding reallocation of 'full_name' every iteration. Instead, name_no_array buffer is extended and reused. Signed-off-by: Pavel Fedin Reviewed-by: Peter Crosthwaite --- qom/object.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/qom/object.c b/qom/object.c index ba63777..5820df2 100644 --- a/qom/object.c +++ b/qom/object.c @@ -10,6 +10,8 @@ * See the COPYING file in the top-level directory. */ +#include + #include "qom/object.h" #include "qom/object_interfaces.h" #include "qemu-common.h" @@ -862,6 +864,14 @@ object_property_add_single(Object *obj, const char *name, const char *type, return prop; } +static void +property_get_uint32_opaque(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + uint32_t value = (uintptr_t)opaque; + visit_type_uint32(v, &value, name, errp); +} + ObjectProperty * object_property_add(Object *obj, const char *name, const char *type, ObjectPropertyAccessor *get, @@ -871,27 +881,46 @@ object_property_add(Object *obj, const char *name, const char *type, { size_t name_len = strlen(name); char *name_no_array; - ObjectProperty *ret; - int i; + ObjectProperty *ret, *count; + uintptr_t i; if (name_len < 3 || memcmp(&name[name_len - 3], "[*]", 4)) { return object_property_add_single(obj, name, type, get, set, release, opaque, errp); } - name_no_array = g_strdup(name); - - name_no_array[name_len - 3] = '\0'; - for (i = 0; ; ++i) { - char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); - - ret = object_property_add(obj, full_name, type, get, set, - release, opaque, NULL); - g_free(full_name); + /* 20 characters for maximum possible uintptr_t (64-bit) */ + name_no_array = g_malloc(name_len + 20); + name_len -= 3; + memcpy(name_no_array, name, name_len); + + name_no_array[name_len] = '#'; + name_no_array[name_len + 1] = '\0'; + count = object_property_find(obj, name_no_array, NULL); + if (count == NULL) { + /* This is very similar to object_property_add_uint32_ptr(), but: + * - Returns pointer + * - Will not recurse here, avoiding one condition check + * - Allows us to use 'opaque' pointer itself as a storage, because + * we want to store only a single integer which should never be + * modified from outside. + */ + count = object_property_add_single(obj, name_no_array, "uint32", + property_get_uint32_opaque, NULL, + NULL, NULL, &error_abort); + } + + for (i = (uintptr_t)count->opaque; ; ++i) { + g_sprintf(&name_no_array[name_len], "[%zu]", i); + + ret = object_property_add_single(obj, name_no_array, type, get, set, + release, opaque, NULL); if (ret) { break; } } + + count->opaque = (void *)i; g_free(name_no_array); return ret; }