Fix #20486: blender hangs upon import attempt of an .obj with >40k polys.
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Sun, 14 Mar 2010 22:30:57 +0000 (22:30 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Sun, 14 Mar 2010 22:30:57 +0000 (22:30 +0000)
Added automatic generation of lookup_int callbacks for collections, for
quicker lookup by index instead of looping over the whole thing. Import
is still quite slow, though now it only takes a few seconds.

The next bottleneck seems to be running update (depsgraph, notifiers, ..)
on setting every property. I fixed part of that by avoiding a notifier
to be added each time, now it checks for duplicates.

source/blender/makesrna/intern/makesrna.c
source/blender/makesrna/intern/rna_access.c
source/blender/makesrna/intern/rna_internal.h
source/blender/windowmanager/intern/wm_event_system.c

index 15d5658..6d3d789 100644 (file)
@@ -846,6 +846,98 @@ static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *
        return func;
 }
 
+static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc, char *nextfunc)
+{
+       char *func;
+
+       if(prop->flag & PROP_IDPROPERTY)
+               return NULL;
+
+       if(!manualfunc) {
+               if(!dp->dnastructname || !dp->dnaname)
+                       return NULL;
+
+               /* only supported in case of standard next functions */
+               if(strcmp(nextfunc, "rna_iterator_array_next") == 0);
+               else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0);
+               else return NULL;
+       }
+
+       func= rna_alloc_function_name(srna->identifier, prop->identifier, "lookup_int");
+
+       fprintf(f, "PointerRNA %s(PointerRNA *ptr, int index)\n", func);
+       fprintf(f, "{\n");
+
+       if(manualfunc) {
+               fprintf(f, "\n  return %s(ptr, index);\n", manualfunc);
+               fprintf(f, "}\n\n");
+               return func;
+       }
+
+       fprintf(f, "    PointerRNA r_ptr;\n");
+       fprintf(f, "    CollectionPropertyIterator iter;\n\n");
+
+       fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, prop->identifier);
+       fprintf(f, "    {\n");
+
+       if(strcmp(nextfunc, "rna_iterator_array_next") == 0) {
+               fprintf(f, "            ArrayIterator *internal= iter.internal;\n");
+               fprintf(f, "            if(internal->skip) {\n");
+               fprintf(f, "                    while(index-- > 0) {\n");
+               fprintf(f, "                            do {\n");
+               fprintf(f, "                                    internal->ptr += internal->itemsize;\n");
+               fprintf(f, "                            } while(internal->skip(&iter, internal->ptr));\n");
+               fprintf(f, "                    }\n");
+               fprintf(f, "            }\n");
+               fprintf(f, "            else {\n");
+               fprintf(f, "                    internal->ptr += internal->itemsize*index;\n");
+               fprintf(f, "            }\n");
+       }
+       else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
+               fprintf(f, "            ListBaseIterator *internal= iter.internal;\n");
+               fprintf(f, "            if(internal->skip) {\n");
+               fprintf(f, "                    while(index-- > 0) {\n");
+               fprintf(f, "                            do {\n");
+               fprintf(f, "                                    internal->link= internal->link->next;\n");
+               fprintf(f, "                            } while(internal->skip(&iter, internal->link));\n");
+               fprintf(f, "                    }\n");
+               fprintf(f, "            }\n");
+               fprintf(f, "            else {\n");
+               fprintf(f, "                    while(index-- > 0)\n");
+               fprintf(f, "                            internal->link= internal->link->next;\n");
+               fprintf(f, "            }\n");
+       }
+
+       fprintf(f, "    }\n\n");
+
+       fprintf(f, "    r_ptr = %s_%s_get(&iter);\n", srna->identifier, prop->identifier);
+       fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, prop->identifier);
+
+       fprintf(f, "    return r_ptr;\n");
+
+#if 0
+       rna_print_data_get(f, dp);
+       item_type= (cprop->item_type)? (char*)cprop->item_type: "UnknownType";
+
+       if(dp->dnalengthname || dp->dnalengthfixed) {
+               if(dp->dnalengthname)
+                       fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), data->%s, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthname);
+               else
+                       fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), %d, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthfixed);
+       }
+       else {
+               if(dp->dnapointerlevel == 0)
+                       fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n", item_type, dp->dnaname);
+               else
+                       fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
+       }
+#endif
+
+       fprintf(f, "}\n\n");
+
+       return func;
+}
+
 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, char *manualfunc)
 {
        char *func, *getfunc;
@@ -1015,6 +1107,7 @@ static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
                }
                case PROP_COLLECTION: {
                        CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;
+                       char *nextfunc= (char*)cprop->next;
 
                        if(dp->dnatype && strcmp(dp->dnatype, "ListBase")==0);
                        else if(dp->dnalengthname || dp->dnalengthfixed)
@@ -1031,6 +1124,7 @@ static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
                        cprop->begin= (void*)rna_def_property_begin_func(f, srna, prop, dp, (char*)cprop->begin);
                        cprop->next= (void*)rna_def_property_next_func(f, srna, prop, dp, (char*)cprop->next);
                        cprop->end= (void*)rna_def_property_end_func(f, srna, prop, dp, (char*)cprop->end);
+                       cprop->lookupint= (void*)rna_def_property_lookup_int_func(f, srna, prop, dp, (char*)cprop->lookupint, nextfunc);
 
                        if(!(prop->flag & PROP_IDPROPERTY)) {
                                if(!cprop->begin) {
index 6bd9979..672ca5d 100644 (file)
@@ -2736,6 +2736,12 @@ void rna_iterator_listbase_end(CollectionPropertyIterator *iter)
        iter->internal= NULL;
 }
 
+PointerRNA rna_listbase_lookup_int(PointerRNA *ptr, StructRNA *type, struct ListBase *lb, int index)
+{
+       void *data= BLI_findlink(lb, index);
+       return rna_pointer_inherit_refine(ptr, type, data);
+}
+
 void rna_iterator_array_begin(CollectionPropertyIterator *iter, void *ptr, int itemsize, int length, int free_ptr, IteratorSkipFunc skip)
 {
        ArrayIterator *internal;
@@ -2800,6 +2806,14 @@ void rna_iterator_array_end(CollectionPropertyIterator *iter)
        iter->internal= NULL;
 }
 
+PointerRNA rna_array_lookup_int(PointerRNA *ptr, StructRNA *type, void *data, int itemsize, int length, int index)
+{
+       if(index < 0 || index >= length)
+               return PointerRNA_NULL;
+
+       return rna_pointer_inherit_refine(ptr, type, ((char*)data) + index*itemsize);
+}
+
 /* RNA Path - Experiment */
 
 static char *rna_path_token(const char **path, char *fixedbuf, int fixedlen, int bracket)
index 06e0a96..68a7ca2 100644 (file)
@@ -297,6 +297,7 @@ void rna_iterator_listbase_begin(struct CollectionPropertyIterator *iter, struct
 void rna_iterator_listbase_next(struct CollectionPropertyIterator *iter);
 void *rna_iterator_listbase_get(struct CollectionPropertyIterator *iter);
 void rna_iterator_listbase_end(struct CollectionPropertyIterator *iter);
+PointerRNA rna_listbase_lookup_int(PointerRNA *ptr, StructRNA *type, struct ListBase *lb, int index);
 
 typedef struct ArrayIterator {
        char *ptr;
@@ -311,6 +312,7 @@ void rna_iterator_array_next(struct CollectionPropertyIterator *iter);
 void *rna_iterator_array_get(struct CollectionPropertyIterator *iter);
 void *rna_iterator_array_dereference_get(struct CollectionPropertyIterator *iter);
 void rna_iterator_array_end(struct CollectionPropertyIterator *iter);
+PointerRNA rna_array_lookup_int(PointerRNA *ptr, StructRNA *type, void *data, int itemsize, int length, int index);
 
 /* Duplicated code since we can't link in blenlib */
 
index 9ef94b5..8cfd456 100644 (file)
@@ -108,6 +108,17 @@ void wm_event_free_all(wmWindow *win)
 
 /* ********************* notifiers, listeners *************** */
 
+static int wm_test_duplicate_notifier(wmWindowManager *wm, unsigned int type, void *reference)
+{
+       wmNotifier *note;
+
+       for(note=wm->queue.first; note; note=note->next)
+               if((note->category|note->data|note->subtype|note->action) == type && note->reference == reference)
+                       return 1;
+       
+       return 0;
+}
+
 /* XXX: in future, which notifiers to send to other windows? */
 void WM_event_add_notifier(const bContext *C, unsigned int type, void *reference)
 {
@@ -134,7 +145,7 @@ void WM_main_add_notifier(unsigned int type, void *reference)
        Main *bmain= G.main;
        wmWindowManager *wm= bmain->wm.first;
 
-       if(wm) {
+       if(wm && !wm_test_duplicate_notifier(wm, type, reference)) {
                wmNotifier *note= MEM_callocN(sizeof(wmNotifier), "notifier");
                
                note->wm= wm;