Compare commits

...

4 Commits

Author SHA1 Message Date
Matthias Clasen
155899a1e4 Revert "stringlist: Use an array"
This reverts commit bf367b219b.
2020-07-01 14:05:16 -04:00
Matthias Clasen
70cbdd138a Revert "stringlist: Try to reuse objects"
This reverts commit 31404c5d2f.
2020-07-01 14:05:07 -04:00
Matthias Clasen
3e1b9703e7 wip: benchmarks 2020-07-01 14:04:59 -04:00
Matthias Clasen
31404c5d2f stringlist: Try to reuse objects
Try a few nearby positions to see if they have
unused objects, and if so, steal one. This avoids
object growth when filtering, but does not help
for scrolling.
2020-06-30 23:52:10 -04:00
3 changed files with 163 additions and 77 deletions

View File

@@ -145,6 +145,23 @@ gtk_string_object_class_init (GtkStringObjectClass *class)
}
static GtkStringObject *
gtk_string_object_new_take (char *string)
{
GtkStringObject *obj;
obj = g_object_new (GTK_TYPE_STRING_OBJECT, NULL);
obj->string = string;
return obj;
}
static GtkStringObject *
gtk_string_object_new (const char *string)
{
return gtk_string_object_new_take (g_strdup (string));
}
/**
* gtk_string_object_get_string:
* @self: a #GtkStringObject
@@ -165,7 +182,7 @@ struct _GtkStringList
{
GObject parent_instance;
GPtrArray *items;
GSequence *items;
};
struct _GtkStringListClass
@@ -184,25 +201,7 @@ gtk_string_list_get_n_items (GListModel *list)
{
GtkStringList *self = GTK_STRING_LIST (list);
return self->items->len;
}
static inline gboolean
IS_STRING (gpointer item)
{
return GPOINTER_TO_INT (item) & 0x1;
}
static inline gpointer
TO_STRING (gpointer item)
{
return GINT_TO_POINTER (GPOINTER_TO_INT (item) & ~0x1);
}
static inline gpointer
MAKE_STRING (const char *str)
{
return GINT_TO_POINTER (GPOINTER_TO_INT (str) | 0x1);
return g_sequence_get_length (self->items);
}
static gpointer
@@ -210,23 +209,14 @@ gtk_string_list_get_item (GListModel *list,
guint position)
{
GtkStringList *self = GTK_STRING_LIST (list);
gpointer item;
GSequenceIter *iter;
if (position >= self->items->len)
iter = g_sequence_get_iter_at_pos (self->items, position);
if (g_sequence_iter_is_end (iter))
return NULL;
item = g_ptr_array_index (self->items, position);
if (IS_STRING (item))
{
GtkStringObject *obj = g_object_new (GTK_TYPE_STRING_OBJECT, NULL);
obj->string = (char *)TO_STRING (item);
g_assert (!IS_STRING (obj));
g_ptr_array_index (self->items, position) = obj;
item = obj;
}
return g_object_ref (item);
else
return g_object_ref (g_sequence_get (iter));
}
static void
@@ -333,7 +323,7 @@ item_end_element (GtkBuildableParseContext *context,
g_string_assign (data->string, translated);
}
g_ptr_array_add (data->list->items, MAKE_STRING (g_strdup (data->string->str)));
g_sequence_append (data->list->items, gtk_string_object_new (data->string->str));
}
data->translatable = FALSE;
@@ -413,7 +403,7 @@ gtk_string_list_dispose (GObject *object)
{
GtkStringList *self = GTK_STRING_LIST (object);
g_clear_pointer (&self->items, g_ptr_array_unref);
g_clear_pointer (&self->items, g_sequence_free);
G_OBJECT_CLASS (gtk_string_list_parent_class)->dispose (object);
}
@@ -426,19 +416,10 @@ gtk_string_list_class_init (GtkStringListClass *class)
gobject_class->dispose = gtk_string_list_dispose;
}
static void
free_string_or_object (gpointer data)
{
if (IS_STRING (data))
g_free (TO_STRING (data));
else
g_object_unref (data);
}
static void
gtk_string_list_init (GtkStringList *self)
{
self->items = g_ptr_array_new_full (32, free_string_or_object);
self->items = g_sequence_new (g_object_unref);
}
/**
@@ -487,30 +468,39 @@ gtk_string_list_splice (GtkStringList *self,
guint n_removals,
const char * const *additions)
{
guint n_items;
guint i;
gpointer item;
GSequenceIter *it;
guint add, n_items;
g_return_if_fail (GTK_IS_STRING_LIST (self));
g_return_if_fail (position + n_removals >= position); /* overflow */
n_items = self->items->len;
n_items = g_sequence_get_length (self->items);
g_return_if_fail (position + n_removals <= n_items);
for (i = 0; i < n_removals; i++)
g_ptr_array_remove_index (self->items, position);
it = g_sequence_get_iter_at_pos (self->items, position);
if (n_removals)
{
GSequenceIter *end;
end = g_sequence_iter_move (it, n_removals);
g_sequence_remove_range (it, end);
it = end;
}
if (additions)
for (i = 0; additions[i]; i++)
{
item = MAKE_STRING (g_strdup (additions[i]));
g_ptr_array_insert (self->items, position + i, item);
}
{
for (add = 0; additions[add]; add++)
{
g_sequence_insert_before (it, gtk_string_object_new (additions[add]));
}
}
else
i = 0;
add = 0;
if (n_removals || i)
g_list_model_items_changed (G_LIST_MODEL (self), position, n_removals, i);
if (n_removals || add)
g_list_model_items_changed (G_LIST_MODEL (self), position, n_removals, add);
}
/**
@@ -528,13 +518,11 @@ gtk_string_list_append (GtkStringList *self,
const char *string)
{
guint n_items;
gpointer item;
g_return_if_fail (GTK_IS_STRING_LIST (self));
n_items = self->items->len;
item = MAKE_STRING (g_strdup (string));
g_ptr_array_add (self->items, item);
n_items = g_sequence_get_length (self->items);
g_sequence_append (self->items, gtk_string_object_new (string));
g_list_model_items_changed (G_LIST_MODEL (self), n_items, 0, 1);
}
@@ -559,13 +547,11 @@ gtk_string_list_take (GtkStringList *self,
char *string)
{
guint n_items;
gpointer item;
g_return_if_fail (GTK_IS_STRING_LIST (self));
n_items = self->items->len;
item = MAKE_STRING (string);
g_ptr_array_add (self->items, item);
n_items = g_sequence_get_length (self->items);
g_sequence_append (self->items, gtk_string_object_new_take (string));
g_list_model_items_changed (G_LIST_MODEL (self), n_items, 0, 1);
}
@@ -582,12 +568,14 @@ void
gtk_string_list_remove (GtkStringList *self,
guint position)
{
GSequenceIter *iter;
g_return_if_fail (GTK_IS_STRING_LIST (self));
if (position >= self->items->len)
return;
iter = g_sequence_get_iter_at_pos (self->items, position);
g_return_if_fail (!g_sequence_iter_is_end (iter));
g_ptr_array_remove_index (self->items, position);
g_sequence_remove (iter);
g_list_model_items_changed (G_LIST_MODEL (self), position, 1, 0);
}
@@ -609,14 +597,20 @@ const char *
gtk_string_list_get_string (GtkStringList *self,
guint position)
{
gpointer item;
GSequenceIter *iter;
g_return_val_if_fail (GTK_IS_STRING_LIST (self), NULL);
item = g_ptr_array_index (self->items, position);
iter = g_sequence_get_iter_at_pos (self->items, position);
if (IS_STRING (item))
return (const char *)TO_STRING (item);
if (g_sequence_iter_is_end (iter))
{
return NULL;
}
else
return GTK_STRING_OBJECT (item)->string;
{
GtkStringObject *obj = g_sequence_get (iter);
return obj->string;
}
}

View File

@@ -0,0 +1,91 @@
#include <gtk/gtk.h>
static GObject *
get_object (const char *string)
{
GtkStringList *list;
GObject *obj;
list = gtk_string_list_new ((const char *[]){string, NULL});
obj = g_list_model_get_item (G_LIST_MODEL (list), 0);
g_object_unref (list);
return obj;
}
static GListModel *
make_store (guint n_items)
{
GListStore *store;
guint i;
store = g_list_store_new (GTK_TYPE_STRING_OBJECT);
for (i = 0; i < n_items; i++)
{
char *string;
GObject *obj;
string = g_strdup_printf ("item %d", i);
obj = get_object (string);
g_list_store_append (store, obj);
g_object_unref (obj);
g_free (string);
}
return G_LIST_MODEL (store);
}
static void
do_random_access (guint size)
{
GListModel *model;
guint i;
guint position;
GtkStringObject *obj;
gint64 start, end;
guint iterations = 10000000;
model = make_store (size);
start = g_get_monotonic_time ();
for (i = 0; i < iterations; i++)
{
position = g_random_int_range (0, size);
obj = g_list_model_get_item (model, position);
if (g_getenv ("PRINT_ACCESS"))
g_print ("%s", gtk_string_object_get_string (obj));
g_object_unref (obj);
}
end = g_get_monotonic_time ();
g_print ("size %u: %g accesses per second\n",
size,
(iterations / (double) G_TIME_SPAN_SECOND) * ((double)(end - start)));
g_object_unref (model);
}
static void
random_access (void)
{
do_random_access (1e1);
do_random_access (1e2);
do_random_access (1e3);
do_random_access (1e4);
do_random_access (1e5);
do_random_access (1e6);
do_random_access (1e7);
}
int
main (int argc, char *argv[])
{
gtk_test_init (&argc, &argv);
g_test_add_func ("/liststore/random-access", random_access);
return g_test_run ();
}

View File

@@ -76,6 +76,7 @@ tests = [
['revealer-size'],
['widgetorder'],
['widget-refcount'],
['listmodel-performance'],
]
# Tests that are expected to fail