diff options
author | Christian Tismer <tismer@stackless.com> | 2020-07-16 11:19:33 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2020-07-16 15:26:28 +0200 |
commit | e6c2b3bc12080615e80627ead75c0d9446631061 (patch) | |
tree | 81a57d19098976eb85181c54c2b93f83e281504e | |
parent | c0ac97fe6822d783b2e92afa27cf5fbaa6eb64ca (diff) |
PyName: finalize static strings, finally
Static strings are used in PyName and PyMagicName very much.
With the introduction of name mangling and the __feature__
selector, many thousands of static strings are created.
The destruction of static strings was activated and the
implementation was corrected. The principle was reverted to
a simple vector, because interned strings behave like normal
strings. The sharing of references happens transparently.
Change-Id: I915ab6585fe4f81e0d05b9da82842bb33e0bc938
Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r-- | sources/shiboken2/libshiboken/sbkstring.cpp | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/sources/shiboken2/libshiboken/sbkstring.cpp b/sources/shiboken2/libshiboken/sbkstring.cpp index 7fb7d5730..092745d3d 100644 --- a/sources/shiboken2/libshiboken/sbkstring.cpp +++ b/sources/shiboken2/libshiboken/sbkstring.cpp @@ -41,7 +41,7 @@ #include "sbkstaticstrings_p.h" #include "autodecref.h" -#include <set> +#include <vector> namespace Shiboken { @@ -231,12 +231,10 @@ Py_ssize_t len(PyObject *str) // // 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::set<PyObject *>; +using StaticStrings = std::vector<PyObject *>; + +static void finalizeStaticStrings(); // forward static StaticStrings &staticStrings() { @@ -244,8 +242,21 @@ static StaticStrings &staticStrings() return result; } +static void finalizeStaticStrings() +{ + auto &list = staticStrings(); + for (PyObject *ob : list) + Py_DECREF(ob); + list.clear(); +} + PyObject *createStaticString(const char *str) { + static bool initialized = false; + if (!initialized) { + Py_AtExit(finalizeStaticStrings); + initialized = true; + } #if PY_VERSION_HEX >= 0x03000000 PyObject *result = PyUnicode_InternFromString(str); #else @@ -256,17 +267,9 @@ PyObject *createStaticString(const char *str) PyErr_Print(); Py_FatalError("unexpected error in createStaticString()"); } - staticStrings().insert(result); + 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 |