Merge branch 'th/gdataset-misc' into 'main'

[th/gdataset-misc] minor improvements in gdataset.c (around g_datalist_id_remove_multiple())

See merge request GNOME/glib!4507
This commit is contained in:
Philip Withnall 2025-02-21 12:47:57 +00:00
commit b4224a5d6d

View File

@ -354,10 +354,10 @@ datalist_remove (GData *data, guint32 idx)
g_assert (idx < data->len); g_assert (idx < data->len);
#endif #endif
/* g_data_remove_internal() relies on the fact, that this function removes /* We remove the element similar to g_array_remove_index_fast(). That is, the
* the entry similar to g_array_remove_index_fast(). That is, the entries up * entries up to @idx are left unchanged, and the last entry is moved to
* to @idx are left unchanged, and the last entry is moved to position @idx. * position @idx.
* */ */
index = datalist_index_get (data); index = datalist_index_get (data);
if (G_UNLIKELY (index)) if (G_UNLIKELY (index))
@ -464,8 +464,8 @@ datalist_find (GData *data, GQuark key_id, guint32 *out_idx)
GHashTable *index; GHashTable *index;
guint32 i; guint32 i;
if (!data) if (G_UNLIKELY (!data))
goto out_not_found; return NULL;
index = datalist_index_get (data); index = datalist_index_get (data);
@ -483,12 +483,12 @@ datalist_find (GData *data, GQuark key_id, guint32 *out_idx)
} }
} }
goto out_not_found; return NULL;
} }
data_elt = g_hash_table_lookup (index, &key_id); data_elt = g_hash_table_lookup (index, &key_id);
if (!data_elt) if (!data_elt)
goto out_not_found; return NULL;
#if G_ENABLE_DEBUG #if G_ENABLE_DEBUG
g_assert (data_elt >= data->data && data_elt < &data->data[data->len]); g_assert (data_elt >= data->data && data_elt < &data->data[data->len]);
@ -497,11 +497,6 @@ datalist_find (GData *data, GQuark key_id, guint32 *out_idx)
if (out_idx) if (out_idx)
*out_idx = (data_elt - data->data); *out_idx = (data_elt - data->data);
return data_elt; return data_elt;
out_not_found:
if (out_idx)
*out_idx = G_MAXUINT32;
return NULL;
} }
/** /**
@ -712,8 +707,24 @@ g_data_set_internal (GData **datalist,
} }
static inline void /**
g_data_remove_internal (GData **datalist, * g_datalist_id_remove_multiple:
* @datalist: a datalist
* @keys: (array length=n_keys): keys to remove
* @n_keys: length of @keys.
*
* Removes multiple keys from a datalist.
*
* This is more efficient than calling g_datalist_id_remove_data()
* multiple times in a row.
*
* Before 2.80, @n_keys had to be not larger than 16.
* Since 2.84, performance is improved for larger number of keys.
*
* Since: 2.74
*/
void
g_datalist_id_remove_multiple (GData **datalist,
GQuark *keys, GQuark *keys,
gsize n_keys) gsize n_keys)
{ {
@ -737,17 +748,15 @@ g_data_remove_internal (GData **datalist,
/* Allocate an array of GDataElt to hold copies of the elements /* Allocate an array of GDataElt to hold copies of the elements
* that are removed from the datalist. Allow enough space for all * that are removed from the datalist. Allow enough space for all
* the keys; if a key is not found, the corresponding element of * the keys.
* old is not populated, so we initialize them all to NULL to
* detect that case.
* *
* At most allocate 400 bytes on the stack. Especially since we call * At most allocate 400 bytes on the stack. Especially since we call
* out to external code, we don't know how much stack we can use. */ * out to external code, we don't know how much stack we can use. */
if (n_keys <= 400u / sizeof (GDataElt)) if (n_keys <= 400u / sizeof (GDataElt))
old = g_newa0 (GDataElt, n_keys); old = g_newa (GDataElt, n_keys);
else else
{ {
old_to_free = g_new0 (GDataElt, n_keys); old_to_free = g_new (GDataElt, n_keys);
old = old_to_free; old = old_to_free;
} }
@ -974,30 +983,6 @@ g_datalist_id_set_data_full (GData **datalist,
g_data_set_internal (datalist, key_id, data, destroy_func, NULL); g_data_set_internal (datalist, key_id, data, destroy_func, NULL);
} }
/**
* g_datalist_id_remove_multiple:
* @datalist: a datalist
* @keys: (array length=n_keys): keys to remove
* @n_keys: length of @keys.
*
* Removes multiple keys from a datalist.
*
* This is more efficient than calling g_datalist_id_remove_data()
* multiple times in a row.
*
* Before 2.80, @n_keys had to be not larger than 16.
* Since 2.84, performance is improved for larger number of keys.
*
* Since: 2.74
*/
void
g_datalist_id_remove_multiple (GData **datalist,
GQuark *keys,
gsize n_keys)
{
g_data_remove_internal (datalist, keys, n_keys);
}
/** /**
* g_dataset_id_remove_no_notify: (skip) * g_dataset_id_remove_no_notify: (skip)
* @dataset_location: (not nullable): the location identifying the dataset. * @dataset_location: (not nullable): the location identifying the dataset.