From 4ce492b9e58ba5df37322c09283f01e07ecd33f6 Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Tue, 22 Mar 2022 23:19:31 +0200 Subject: [PATCH] (core) Compute sample_record lazily using a property Summary: This changes Table.sample_record from a regular attribute to a property that's only computed when it's needed, which is only for autocompletion. This means it's not cached any more, but it's also not recomputed every time the schema changes. Profiling showed that _make_sample_record took a signification portion of time, and this change makes the tests 2 or 3 seconds faster. Test Plan: existing tests Reviewers: paulfitz Reviewed By: paulfitz Subscribers: paulfitz Differential Revision: https://phab.getgrist.com/D3334 --- sandbox/grist/table.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sandbox/grist/table.py b/sandbox/grist/table.py index ca5b965e..0bec5eb1 100644 --- a/sandbox/grist/table.py +++ b/sandbox/grist/table.py @@ -248,6 +248,16 @@ class Table(object): """ return len(self._empty_lookup_column._do_fast_empty_lookup()) + @property + def sample_record(self): + """ + Used for auto-completion as a record with correct properties of correct types. + """ + return _make_sample_record( + self.table_id, + [col for col_id, col in self.all_columns.items() if col_id not in self._special_cols], + ) + def _rebuild_model(self, user_table): """ Sets class-wide properties from a new Model class for the table (inner class within the table @@ -267,9 +277,6 @@ class Table(object): default_func = self.Model.__dict__.get(get_default_func_name(col_id)) new_cols[col_id] = self._create_or_update_col(col_id, col_model, default_func) - # Used for auto-completion as a record with correct properties of correct types. - self.sample_record = _make_sample_record(self.table_id, six.itervalues(new_cols)) - # Note that we reuse previous special columns like lookup maps, since those not affected by # column changes should stay the same. These get removed when unneeded using other means. new_cols.update(sorted(six.iteritems(self._special_cols)))