From patchwork Wed Jan 14 06:38:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yichun Zhang (agentzh)" X-Patchwork-Id: 4659 Received: (qmail 20020 invoked by alias); 14 Jan 2015 06:38:10 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 20006 invoked by uid 89); 14 Jan 2015 06:38:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pd0-f174.google.com Received: from mail-pd0-f174.google.com (HELO mail-pd0-f174.google.com) (209.85.192.174) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 14 Jan 2015 06:38:06 +0000 Received: by mail-pd0-f174.google.com with SMTP id fp1so8003995pdb.5 for ; Tue, 13 Jan 2015 22:38:05 -0800 (PST) X-Received: by 10.70.92.100 with SMTP id cl4mr3070639pdb.151.1421217484941; Tue, 13 Jan 2015 22:38:04 -0800 (PST) Received: from localhost.localdomain (c-24-5-132-69.hsd1.ca.comcast.net. [24.5.132.69]) by mx.google.com with ESMTPSA id w8sm18653307pbt.71.2015.01.13.22.38.03 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Jan 2015 22:38:04 -0800 (PST) From: "Yichun Zhang (agentzh)" To: gdb-patches Cc: "Yichun Zhang (agentzh)" Subject: [PATCH] [python] Optimize python_string_to_host_string() for Python 2. Date: Tue, 13 Jan 2015 22:38:01 -0800 Message-Id: <1421217481-18204-1-git-send-email-agentzh@gmail.com> X-IsSubscribed: yes Hi, I have been writing quite some advanced GDB Python tools for LuaJIT and NGINX in the nginx-gdb-utils project [1] and have been suffering from serious performance issues even for relatively small working sets. I have noticed from a typical on-CPU C-land Flame Graph [2] for my tools that python_string_to_host_string() is painfully slow when using Python 2 (2.7 to be more specific) due to the expensive and meaningless unicode conversions there (involved with hot function calls like utf_8_decode). With the following patch, my most complicated Python tools finally have comparable performance between Python 2 and Python 3. In the case of Python 2, some real-world Python scripts' overall speedup can be as big as 34% (for my "lgcpath" command [3]) or even 57% (for my "lgcstat" command [4]). And from the new Flame Graph [5], we can see that the corresponding function frames are indeed gone. Comments are welcome! [1] https://github.com/openresty/nginx-gdb-utils#readme [2] http://agentzh.org/misc/flamegraph/gdb-py-lgcstat-2015-0113.svg [3] https://github.com/openresty/nginx-gdb-utils#lgcpath [4] https://github.com/openresty/nginx-gdb-utils#lgcstat [5] http://agentzh.org/misc/flamegraph/patched-gdb-py-lgcstat-2015-0113.svg gdb/ChangeLog: * python/py-utils.c (python_string_to_host_string): use xstrdup directly for Python 2. * python/py-utils.c (gdbpy_obj_to_string): use python_string_to_host_string for both Python 2 and 3. --- gdb/python/py-utils.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 58a5934..ed7be3b 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -209,6 +209,7 @@ python_string_to_target_python_string (PyObject *obj) char * python_string_to_host_string (PyObject *obj) { +#ifdef IS_PY3K PyObject *str; char *result; @@ -219,6 +220,9 @@ python_string_to_host_string (PyObject *obj) result = unicode_to_encoded_string (str, host_charset ()); Py_DECREF (str); return result; +#else + return xstrdup (PyString_AsString (obj)); +#endif } /* Return true if OBJ is a Python string or unicode object, false @@ -245,12 +249,7 @@ gdbpy_obj_to_string (PyObject *obj) if (str_obj != NULL) { -#ifdef IS_PY3K char *msg = python_string_to_host_string (str_obj); -#else - char *msg = xstrdup (PyString_AsString (str_obj)); -#endif - Py_DECREF (str_obj); return msg; }