[Checkins] SVN: Products.ZCatalog/trunk/ Optimize brain instantiation, by creating underlying record items in a single step, instead of creation and three update calls.

Hano Schlichting cvs-admin at zope.org
Sun Aug 12 18:34:08 UTC 2012


Log message for revision 127497:
  Optimize brain instantiation, by creating underlying record items in a single step, instead of creation and three update calls.
  

Changed:
  U   Products.ZCatalog/trunk/CHANGES.txt
  U   Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py

-=-
Modified: Products.ZCatalog/trunk/CHANGES.txt
===================================================================
--- Products.ZCatalog/trunk/CHANGES.txt	2012-08-12 09:48:46 UTC (rev 127496)
+++ Products.ZCatalog/trunk/CHANGES.txt	2012-08-12 18:34:04 UTC (rev 127497)
@@ -4,6 +4,8 @@
 3.0b2 (unreleased)
 ------------------
 
+- Optimize brain instantiation, by creating underlying record items in a
+  single step, instead of creation and three update calls.
 
 3.0b1 (2012-07-19)
 ------------------

Modified: Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py	2012-08-12 09:48:46 UTC (rev 127496)
+++ Products.ZCatalog/trunk/src/Products/ZCatalog/Catalog.py	2012-08-12 18:34:04 UTC (rev 127497)
@@ -115,16 +115,22 @@
         if isinstance(index, tuple):
             # then it contains a score...
             normalized_score, score, key = index
-            r = self._v_result_class(self.data[key]).__of__(aq_parent(self))
+        else:
+            # otherwise no score, set all scores to 1
+            normalized_score, score, key = (1, 1, index)
+
+        data = self.data[key]
+        klass = self._v_result_class
+        schema_len = len(klass.__record_schema__)
+        if schema_len == len(data) + 3:
+            # if we have complete data, create in a single pass
+            r = klass(tuple(data) + (key, score, normalized_score))
+        else:
+            r = klass(data)
             r.data_record_id_ = key
             r.data_record_score_ = score
             r.data_record_normalized_score_ = normalized_score
-        else:
-            # otherwise no score, set all scores to 1
-            r = self._v_result_class(self.data[index]).__of__(aq_parent(self))
-            r.data_record_id_ = index
-            r.data_record_score_ = 1
-            r.data_record_normalized_score_ = 1
+        r = r.__of__(aq_parent(self))
         return r
 
     def __setstate__(self, state):
@@ -638,12 +644,18 @@
                         passed into self.useBrains.
                         """
                         score, key = item
-                        r = self._v_result_class(
-                            self.data[key]).__of__(aq_parent(self))
-                        r.data_record_id_ = key
-                        r.data_record_score_ = score
-                        r.data_record_normalized_score_ = \
-                            int(100.0 * score / max)
+                        data = self.data[key]
+                        klass = self._v_result_class
+                        schema_len = len(klass.__record_schema__)
+                        norm_score = int(100.0 * score / max)
+                        if schema_len == len(data) + 3:
+                            r = klass(tuple(data) + (key, score, norm_score))
+                        else:
+                            r = klass(data)
+                            r.data_record_id_ = key
+                            r.data_record_score_ = score
+                            r.data_record_normalized_score_ = norm_score
+                        r = r.__of__(aq_parent(self))
                         return r
 
                     sequence, slen = self._limit_sequence(rs, rlen, b_start,



More information about the checkins mailing list