[gnumeric] Workbook: cache computed sheet size.



commit ab6b3176f3f0cb8fa62a21bc240700b868af5280
Author: Morten Welinder <terra gnome org>
Date:   Sun Jul 5 18:36:04 2020 -0400

    Workbook: cache computed sheet size.
    
    We now also use the bounding box of all sheets' sizes.

 src/gnumeric.h      |  8 +++++---
 src/sheet.c         |  2 ++
 src/sheet.h         |  3 ---
 src/workbook-priv.h |  3 +++
 src/workbook.c      | 41 ++++++++++++++++++++++-------------------
 5 files changed, 32 insertions(+), 25 deletions(-)
---
diff --git a/src/gnumeric.h b/src/gnumeric.h
index 0c57adf46..99e585578 100644
--- a/src/gnumeric.h
+++ b/src/gnumeric.h
@@ -19,9 +19,11 @@ G_BEGIN_DECLS
 #define GNM_MIN_ROWS 0x80
 #define GNM_MIN_COLS 0x80
 
-/*
- * Note: more than 364238 columns will introduce a column named TRUE.
- */
+// Note: more than 364238 columns will introduce a column named TRUE.
+
+struct _GnmSheetSize {
+       int max_cols, max_rows;
+};
 
 typedef enum {
        GNM_SHEET_VISIBILITY_VISIBLE,
diff --git a/src/sheet.c b/src/sheet.c
index 1b9a7089c..cda9af481 100644
--- a/src/sheet.c
+++ b/src/sheet.c
@@ -1247,6 +1247,8 @@ gnm_sheet_resize_main (Sheet *sheet, int cols, int rows,
        if (old_cols == cols && old_rows == rows)
                return;
 
+       sheet->workbook->sheet_size_cached = FALSE;
+
        /* ---------------------------------------- */
        /* Gather styles we want to copy into new areas.  */
 
diff --git a/src/sheet.h b/src/sheet.h
index f74c86d49..77e0f08b2 100644
--- a/src/sheet.h
+++ b/src/sheet.h
@@ -13,9 +13,6 @@ G_BEGIN_DECLS
 
 GNM_VAR_DECL Sheet *invalid_sheet;
 
-struct _GnmSheetSize {
-       int max_cols, max_rows;
-};
 GType gnm_sheet_size_get_type (void);
 
 struct _ColRowCollection {
diff --git a/src/workbook-priv.h b/src/workbook-priv.h
index ba278f354..42a6a4b51 100644
--- a/src/workbook-priv.h
+++ b/src/workbook-priv.h
@@ -16,6 +16,9 @@ struct _Workbook {
        GHashTable *sheet_order_dependents;
        GHashTable *sheet_local_functions;
 
+       gboolean sheet_size_cached;
+       GnmSheetSize sheet_size;
+
        gboolean is_placeholder;
 
        GOFileFormatLevel  file_format_level;
diff --git a/src/workbook.c b/src/workbook.c
index d58dd1b62..6fb473574 100644
--- a/src/workbook.c
+++ b/src/workbook.c
@@ -224,6 +224,7 @@ workbook_init (GObject *object)
        wb->is_placeholder = FALSE;
        wb->wb_views = NULL;
        wb->sheets = g_ptr_array_new ();
+       wb->sheet_size_cached = FALSE;
        wb->sheet_hash_private = g_hash_table_new (g_str_hash, g_str_equal);
        wb->sheet_order_dependents = NULL;
        wb->sheet_local_functions = NULL;
@@ -1013,6 +1014,7 @@ workbook_sheet_attach_at_pos (Workbook *wb, Sheet *new_sheet, int pos)
        g_hash_table_insert (wb->sheet_hash_private,
                             new_sheet->name_case_insensitive,
                             new_sheet);
+       wb->sheet_size_cached = FALSE;
 
        WORKBOOK_FOREACH_VIEW (wb, view,
                wb_view_sheet_add (view, new_sheet););
@@ -1135,6 +1137,7 @@ workbook_sheet_delete (Sheet *sheet)
        /* All is fine, remove the sheet */
        pre_sheet_index_change (wb);
        g_ptr_array_remove_index (wb->sheets, sheet_index);
+       wb->sheet_size_cached = FALSE;
        workbook_sheet_index_update (wb, sheet_index);
        sheet->index_in_wb = -1;
        g_hash_table_remove (wb->sheet_hash_private, sheet->name_case_insensitive);
@@ -1393,40 +1396,40 @@ workbook_set_1904 (Workbook *wb, gboolean base1904)
 
 /**
  * workbook_get_sheet_size:
- * @wb: #Workbook
+ * @wb: (nullable): #Workbook
  *
- * Returns: (transfer none): the current sheet size for @wb.
+ * Returns: (transfer none): the current sheet size for @wb.  If sheets are
+ * not of uniform size, this will be some size that is big enough in both
+ * directions for all sheets.  That size isn't necessarily one that could
+ * be used to create a new sheet.
  **/
 GnmSheetSize const *
 workbook_get_sheet_size (Workbook const *wb)
 {
-       GnmSheetSize res;
        static const GnmSheetSize max_size = {
                GNM_MAX_COLS, GNM_MAX_ROWS
        };
+       int n = wb ? workbook_sheet_count (wb) : 0;
 
-       int i, n;
-       gboolean uniform = TRUE;
-
-       n = wb ? workbook_sheet_count (wb) : 0;
        if (n == 0)
                return &max_size;
 
-       res = *gnm_sheet_get_size (workbook_sheet_by_index (wb, 0));
-       for (i = 1; i < n; i++) {
-               Sheet *sheet = workbook_sheet_by_index (wb, i);
-               GnmSheetSize const *ss = gnm_sheet_get_size (sheet);
-               if (ss->max_cols != res.max_cols ||
-                   ss->max_rows != res.max_rows) {
-                       uniform = FALSE;
-                       break;
+       if (!wb->sheet_size_cached) {
+               Workbook *wb1 = (Workbook *)wb;
+               int i;
+
+               wb1->sheet_size = *gnm_sheet_get_size (workbook_sheet_by_index (wb, 0));
+               for (i = 1; i < n; i++) {
+                       Sheet *sheet = workbook_sheet_by_index (wb, i);
+                       GnmSheetSize const *ss = gnm_sheet_get_size (sheet);
+                       wb1->sheet_size.max_cols = MAX (wb->sheet_size.max_cols, ss->max_cols);
+                       wb1->sheet_size.max_rows = MAX (wb->sheet_size.max_rows, ss->max_rows);
                }
+
+               wb1->sheet_size_cached = TRUE;
        }
 
-       if (uniform)
-               return gnm_sheet_get_size (workbook_sheet_by_index (wb, 0));
-       else
-               return &max_size; // It's unclear what to do
+       return &wb->sheet_size;
 }
 
 /* ------------------------------------------------------------------------- */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]