[glib/gobject-speedups4] Apply 6 suggestion(s) to 1 file(s)
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/gobject-speedups4] Apply 6 suggestion(s) to 1 file(s)
- Date: Tue, 7 Jun 2022 12:38:04 +0000 (UTC)
commit 922f9628323e787807783aae810d07bf21862b9b
Author: Philip Withnall <philip tecnocode co uk>
Date: Tue Jun 7 12:38:04 2022 +0000
Apply 6 suggestion(s) to 1 file(s)
gobject/gobject.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
---
diff --git a/gobject/gobject.c b/gobject/gobject.c
index 81d06eba40..e644c73178 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -721,16 +721,28 @@ compare_pspec_entry (const void *a,
return ae->name < be->name ? -1 : (ae->name > be->name ? 1 : 0);
}
+/* This uses pointer comparisons with @property_name, so
+ * will only work with string literals. */
static inline GParamSpec *
find_pspec (GObjectClass *class,
const char *property_name)
{
- PspecEntry *pspecs = (PspecEntry *)class->pspecs;
- guint n_pspecs = class->n_pspecs;
+ const PspecEntry *pspecs = (const PspecEntry *)class->pspecs;
+ gsize n_pspecs = class->n_pspecs;
+ /* The limit for choosing between linear and binary search is
+ * fairly arbitrary.
+ *
+ * Both searches use pointer comparisons against @property_name.
+ * If this function is called with a non-static @property_name,
+ * it will fall through to the g_param_spec_pool_lookup() case.
+ * That’s OK; this is an opportunistic optimisation which relies
+ * on the fact that *most* (but not all) property lookups use
+ * static property names.
+ */
if (n_pspecs < 10)
{
- for (guint i = 0; i < n_pspecs; i++)
+ for (gsize i = 0; i < n_pspecs; i++)
{
if (pspecs[i].name == property_name)
return pspecs[i].pspec;
@@ -868,6 +880,14 @@ g_object_class_install_properties (GObjectClass *oclass,
}
}
+ /* Save a copy of the pspec array inside the class struct. This
+ * makes it faster to look up pspecs for the class in future when
+ * acting on those properties.
+ *
+ * If a pspec is not in this cache array, calling code will fall
+ * back to using g_param_spec_pool_lookup(), so a pspec not being
+ * in this array is a (potential) performance problem but not a
+ * correctness problem. */
if (oclass->pspecs == NULL)
{
PspecEntry *entries = g_new (PspecEntry, n_pspecs - 1);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]