[libgda] Misc. optimizations
- From: Vivien Malerba <vivien src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda] Misc. optimizations
- Date: Thu, 1 Mar 2012 19:54:25 +0000 (UTC)
commit af82cac960495181e22aaefeb1cc20a04819b64e
Author: Vivien Malerba <malerba gnome-db org>
Date: Thu Mar 1 20:42:22 2012 +0100
Misc. optimizations
libgda/gda-data-access-wrapper.c | 31 +++++++++++++-------
libgda/gda-data-model-import.c | 8 ++--
libgda/gda-data-model.c | 57 ++++++++++++++++++++++++++++++--------
libgda/gda-holder.c | 8 ++--
4 files changed, 73 insertions(+), 31 deletions(-)
---
diff --git a/libgda/gda-data-access-wrapper.c b/libgda/gda-data-access-wrapper.c
index abb2c49..bf10698 100644
--- a/libgda/gda-data-access-wrapper.c
+++ b/libgda/gda-data-access-wrapper.c
@@ -42,6 +42,7 @@ struct _GdaDataAccessWrapperPrivate {
gint iter_row;/* current row of @iter, starting at 0 when created */
GHashTable *rows; /* NULL if @model already is random access */
+ gint nb_rows; /* number of rows of @wrapper */
gint nb_cols; /* number of columns of @wrapper */
gint last_row;/* row number of the last row which has been read */
gboolean end_of_data; /* TRUE if the end of the data model has been reached by the iterator */
@@ -190,6 +191,7 @@ gda_data_access_wrapper_init (GdaDataAccessWrapper *model, G_GNUC_UNUSED GdaData
model->priv = g_new0 (GdaDataAccessWrapperPrivate, 1);
model->priv->iter_row = -1; /* because model->priv->iter does not yet exist */
model->priv->rows = NULL;
+ model->priv->nb_rows = -1;
model->priv->end_of_data = FALSE;
model->priv->last_row = -1;
@@ -352,6 +354,8 @@ gda_data_access_wrapper_set_property (GObject *object,
if (! (model->priv->model_access_flags & GDA_DATA_MODEL_ACCESS_RANDOM)) {
model->priv->iter = gda_data_model_create_iter (mod);
g_return_if_fail (model->priv->iter);
+ g_object_set (model->priv->iter, "validate-changes", FALSE,
+ NULL);
g_signal_connect (G_OBJECT (model->priv->iter), "row-changed",
G_CALLBACK (iter_row_changed_cb), model);
g_signal_connect (G_OBJECT (model->priv->iter), "end-of-data",
@@ -498,12 +502,15 @@ gda_data_access_wrapper_get_n_rows (GdaDataModel *model)
{
GdaDataAccessWrapper *imodel;
g_return_val_if_fail (GDA_IS_DATA_ACCESS_WRAPPER (model), 0);
- imodel = GDA_DATA_ACCESS_WRAPPER (model);
+ imodel = (GdaDataAccessWrapper*) model;
g_return_val_if_fail (imodel->priv, 0);
+ if (imodel->priv->nb_rows >= 0)
+ return imodel->priv->nb_rows;
+
if (imodel->priv->model_access_flags & GDA_DATA_MODEL_ACCESS_RANDOM)
/* imodel->priv->mode is a random access model, use it */
- return gda_data_model_get_n_rows (imodel->priv->model);
+ imodel->priv->nb_rows = gda_data_model_get_n_rows (imodel->priv->model);
else {
/* go at the end */
while (!imodel->priv->end_of_data) {
@@ -511,10 +518,12 @@ gda_data_access_wrapper_get_n_rows (GdaDataModel *model)
break;
}
if (imodel->priv->end_of_data)
- return imodel->priv->last_row +1;
+ imodel->priv->nb_rows = imodel->priv->last_row +1;
else
- return -1;
+ imodel->priv->nb_rows = -1;
}
+
+ return imodel->priv->nb_rows;
}
static gint
@@ -522,7 +531,7 @@ gda_data_access_wrapper_get_n_columns (GdaDataModel *model)
{
GdaDataAccessWrapper *imodel;
g_return_val_if_fail (GDA_IS_DATA_ACCESS_WRAPPER (model), 0);
- imodel = GDA_DATA_ACCESS_WRAPPER (model);
+ imodel = (GdaDataAccessWrapper*) model;
g_return_val_if_fail (imodel->priv, 0);
if (imodel->priv->model)
@@ -536,7 +545,7 @@ gda_data_access_wrapper_describe_column (GdaDataModel *model, gint col)
{
GdaDataAccessWrapper *imodel;
g_return_val_if_fail (GDA_IS_DATA_ACCESS_WRAPPER (model), NULL);
- imodel = GDA_DATA_ACCESS_WRAPPER (model);
+ imodel = (GdaDataAccessWrapper*) model;
g_return_val_if_fail (imodel->priv, NULL);
if (imodel->priv->model) {
@@ -555,7 +564,7 @@ gda_data_access_wrapper_get_access_flags (GdaDataModel *model)
GdaDataAccessWrapper *imodel;
g_return_val_if_fail (GDA_IS_DATA_ACCESS_WRAPPER (model), 0);
- imodel = GDA_DATA_ACCESS_WRAPPER (model);
+ imodel = (GdaDataAccessWrapper*) model;
g_return_val_if_fail (imodel->priv, 0);
return GDA_DATA_MODEL_ACCESS_RANDOM;
@@ -573,14 +582,14 @@ create_new_row (GdaDataAccessWrapper *model)
GValue *dest;
dest = gda_row_get_value (row, i);
if (model->priv->rows_mapping)
- holder = gda_data_model_iter_get_holder_for_field (model->priv->iter, model->priv->rows_mapping [i]);
+ holder = gda_set_get_nth_holder ((GdaSet *) model->priv->iter, model->priv->rows_mapping [i]);
else
- holder = gda_data_model_iter_get_holder_for_field (model->priv->iter, i);
+ holder = gda_set_get_nth_holder ((GdaSet *) model->priv->iter, i);
if (holder) {
const GValue *cvalue = gda_holder_get_value (holder);
if (cvalue) {
gda_value_reset_with_type (dest, G_VALUE_TYPE ((GValue *) cvalue));
- gda_value_set_from_value (dest, cvalue);
+ g_value_copy (cvalue, dest);
}
else
gda_value_set_null (dest);
@@ -604,7 +613,7 @@ gda_data_access_wrapper_get_value_at (GdaDataModel *model, gint col, gint row, G
GdaDataAccessWrapper *imodel;
g_return_val_if_fail (GDA_IS_DATA_ACCESS_WRAPPER (model), NULL);
- imodel = GDA_DATA_ACCESS_WRAPPER (model);
+ imodel = (GdaDataAccessWrapper*) model;
g_return_val_if_fail (imodel->priv, NULL);
g_return_val_if_fail (imodel->priv->model, NULL);
g_return_val_if_fail (row >= 0, NULL);
diff --git a/libgda/gda-data-model-import.c b/libgda/gda-data-model-import.c
index 9c88e29..c157393 100644
--- a/libgda/gda-data-model-import.c
+++ b/libgda/gda-data-model-import.c
@@ -329,7 +329,6 @@ gda_data_model_import_data_model_init (GdaDataModelIface *iface)
static void
gda_data_model_import_init (GdaDataModelImport *model, G_GNUC_UNUSED GdaDataModelImportClass *klass)
{
- g_return_if_fail (GDA_IS_DATA_MODEL_IMPORT (model));
model->priv = g_new0 (GdaDataModelImportPrivate, 1);
model->priv->random_access = FALSE; /* cursor mode is the default */
model->priv->columns = NULL;
@@ -1971,9 +1970,10 @@ gda_data_model_import_iter_next (GdaDataModel *model, GdaDataModelIter *iter)
plist && vlist;
plist = plist->next, vlist = vlist->next) {
GError *lerror = NULL;
- if (! gda_holder_set_value (GDA_HOLDER (plist->data),
- (GValue *) vlist->data, &lerror))
+ if (! gda_holder_take_value ((GdaHolder*) plist->data,
+ (GValue *) vlist->data, &lerror))
gda_holder_force_invalid_e (GDA_HOLDER (plist->data), lerror);
+ vlist->data = NULL;
}
if (plist || vlist) {
if (plist) {
@@ -2041,7 +2041,7 @@ gda_data_model_import_iter_prev (GdaDataModel *model, GdaDataModelIter *iter)
plist && vlist;
plist = plist->next, vlist = vlist->next) {
GError *lerror = NULL;
- if (! gda_holder_set_value (GDA_HOLDER (plist->data),
+ if (! gda_holder_set_value ((GdaHolder*) plist->data,
(GValue *) vlist->data, &lerror))
gda_holder_force_invalid_e (GDA_HOLDER (plist->data), lerror);
}
diff --git a/libgda/gda-data-model.c b/libgda/gda-data-model.c
index 0ef691d..7917b30 100644
--- a/libgda/gda-data-model.c
+++ b/libgda/gda-data-model.c
@@ -1648,9 +1648,15 @@ export_to_text_separated (GdaDataModel *model, const gint *cols, gint nb_cols,
else if (null_as_empty && gda_value_is_null (value))
txt = g_strdup ("");
else {
+ gboolean alloc = FALSE;
gchar *tmp;
-
- tmp = gda_value_stringify (value);
+
+ if (value && (G_VALUE_TYPE (value) == G_TYPE_STRING))
+ tmp = (gchar*) g_value_get_string (value);
+ else {
+ tmp = gda_value_stringify (value);
+ alloc = TRUE;
+ }
if (tmp) {
gsize len, size;
len = strlen (tmp);
@@ -1663,6 +1669,8 @@ export_to_text_separated (GdaDataModel *model, const gint *cols, gint nb_cols,
txt [len - 1] = 0;
memmove (txt, txt+1, len);
}
+ if (alloc)
+ g_free (tmp);
}
else {
if (field_quotes) {
@@ -2586,18 +2594,30 @@ real_gda_data_model_dump_as_string (GdaDataModel *model, gboolean dump_attribute
cols_size [i + col_offset] = MAX ((guint)cols_size [i + col_offset], strlen (ERROR_STRING));
}
else {
+ gboolean alloc = FALSE;
str = NULL;
if (null_as_empty) {
if (!value || gda_value_is_null (value))
- str = g_strdup ("");
+ str = "";
+ }
+ if (!str) {
+ if (value) {
+ if (G_VALUE_TYPE (value) == G_TYPE_STRING)
+ str = (gchar*) g_value_get_string (value);
+ else {
+ str = gda_value_stringify ((GValue*)value);
+ alloc = TRUE;
+ }
+ }
+ else
+ str = "_null_";
}
- if (!str)
- str = value ? gda_value_stringify ((GValue*)value) : g_strdup ("_null_");
if (str) {
gint w;
string_get_dimensions (str, &w, NULL);
cols_size [i + col_offset] = MAX (cols_size [i + col_offset], w);
- g_free (str);
+ if (alloc)
+ g_free (str);
}
}
}
@@ -2692,28 +2712,41 @@ real_gda_data_model_dump_as_string (GdaDataModel *model, gboolean dump_attribute
}
for (i = 0; i < n_cols; i++) {
+ gboolean alloc = FALSE;
+ str = NULL;
if (!dump_attributes) {
value = gda_data_model_get_value_at (ramodel, i, j, NULL);
if (!value)
- str = g_strdup (ERROR_STRING);
+ str = ERROR_STRING;
else {
- str = NULL;
if (null_as_empty) {
if (!value || gda_value_is_null (value))
- str = g_strdup ("");
+ str = "";
+ }
+ if (!str) {
+ if (value) {
+ if (G_VALUE_TYPE (value) == G_TYPE_STRING)
+ str = (gchar*) g_value_get_string (value);
+ else {
+ str = gda_value_stringify ((GValue*)value);
+ alloc = TRUE;
+ }
+ }
+ else
+ str = "_null_";
}
- if (!str)
- str = value ? gda_value_stringify ((GValue *)value) : g_strdup ("_null_");
}
}
else {
GdaValueAttribute attrs;
attrs = gda_data_model_get_attributes_at (ramodel, i, j);
str = g_strdup_printf ("%u", attrs);
+ alloc = TRUE;
}
if (str) {
cols_str [i + col_offset] = g_strsplit (str, "\n", -1);
- g_free (str);
+ if (alloc)
+ g_free (str);
cols_height [i + col_offset] = g_strv_length (cols_str [i + col_offset]);
kmax = MAX (kmax, cols_height [i + col_offset]);
}
diff --git a/libgda/gda-holder.c b/libgda/gda-holder.c
index 9bb5fbe..444a8ba 100644
--- a/libgda/gda-holder.c
+++ b/libgda/gda-holder.c
@@ -1097,10 +1097,10 @@ real_gda_holder_set_value (GdaHolder *holder, GValue *value, gboolean do_copy, G
value && (G_VALUE_TYPE (value) == holder->priv->g_type))
holder->priv->default_forced = !gda_value_compare (holder->priv->default_value, value);
}
- GValue *att_value;
- g_value_set_boolean ((att_value = gda_value_new (G_TYPE_BOOLEAN)), holder->priv->default_forced);
- gda_holder_set_attribute_static (holder, GDA_ATTRIBUTE_IS_DEFAULT, att_value);
- gda_value_free (att_value);
+ GValue att_value = {0};
+ g_value_init (&att_value, G_TYPE_BOOLEAN);
+ g_value_set_boolean (&att_value, holder->priv->default_forced);
+ gda_holder_set_attribute_static (holder, GDA_ATTRIBUTE_IS_DEFAULT, &att_value);
/* real setting of the value */
if (holder->priv->full_bind) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]