aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/libshiboken/sbkstring.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2019-09-20 09:50:01 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2019-09-30 17:57:33 +0200
commit12300111a002926d38afb69e5addb60146f2b2a5 (patch)
treef87784e2ad8db5bd49155e91bf787833aa0f1522 /sources/shiboken2/libshiboken/sbkstring.cpp
parentb4989b9c2b33772f23dd5f9763622e6fd3cf41a8 (diff)
Speed: Avoid some repeated string constant initialization
This patch introduces a basic concept to avoid the repetition of string constant creation in Python by using helper functions returning static instances. There is currently no real shiboken finalization, so we postpone finalization until shiboken has one: - call the finalize_strings() function - build finalization for other stuff (all signatures etc) Initial-patch-by: Christian Tismer <tismer@stackless.com> Task-number: PYSIDE-1087 Change-Id: If3483ba91f719ee0472eb53583460ba35163bc9d Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken2/libshiboken/sbkstring.cpp')
-rw-r--r--sources/shiboken2/libshiboken/sbkstring.cpp64
1 files changed, 62 insertions, 2 deletions
diff --git a/sources/shiboken2/libshiboken/sbkstring.cpp b/sources/shiboken2/libshiboken/sbkstring.cpp
index 9ba5be281..2e638a413 100644
--- a/sources/shiboken2/libshiboken/sbkstring.cpp
+++ b/sources/shiboken2/libshiboken/sbkstring.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2018 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt for Python.
@@ -40,6 +40,8 @@
#include "sbkstring.h"
#include "autodecref.h"
+#include <vector>
+
namespace Shiboken
{
@@ -200,6 +202,64 @@ Py_ssize_t len(PyObject *str)
return 0;
}
-} // namespace String
+///////////////////////////////////////////////////////////////////////
+//
+// Implementation of efficient Python strings
+// ------------------------------------------
+//
+// Instead of repetitively executing
+//
+// PyObject *attr = PyObject_GetAttrString(obj, "__name__");
+//
+// a helper of the form
+//
+// PyObject *name()
+// {
+// static PyObject *const s = Shiboken::String::createStaticString("__name__");
+// return result;
+// }
+//
+// can now be implemented, which registers the string into a static set avoiding
+// repetitive string creation. The resulting code looks like:
+//
+// PyObject *attr = PyObject_GetAttr(obj, name());
+//
+// Missing:
+// There is no finalization for the string structures, yet.
+// But this is a global fault in shiboken. We are missing a true
+// finalization like in all other modules.
+
+using StaticStrings = std::vector<PyObject *>;
+
+static StaticStrings &staticStrings()
+{
+ static StaticStrings result;
+ return result;
+}
+
+PyObject *createStaticString(const char *str)
+{
+#if PY_VERSION_HEX >= 0x03000000
+ PyObject *result = PyUnicode_InternFromString(str);
+#else
+ PyObject *result = PyString_InternFromString(str);
+#endif
+ if (result == nullptr) {
+ // This error is never checked, but also very unlikely. Report and exit.
+ PyErr_Print();
+ Py_FatalError("unexpected error in createStaticString()");
+ }
+ staticStrings().push_back(result);
+ return result;
+}
+void finalizeStaticStrings() // Currently unused
+{
+ auto &list = staticStrings();
+ for (auto s : list)
+ Py_DECREF(s);
+ list.clear();
+}
+
+} // namespace String
} // namespace Shiboken