On Aug 15, 1:57 pm, Anssi Kääriäinen <[email protected]> wrote:
> But, maybe these signals do not need to be deprecated to get the speed
> gain. We could check if pre_init or post_init is used at all in the
> Django instance.

I tried this. The result is that you can save 0.03 seconds (or about
30% in the most trivial case) per 10000 objects if there are no
pre_init or post_init signals in the project. However
GenericForeignKey will listen to pre_init signal, and ImageField for
the post_init signal (I missed this one earlier). And if you have a
single listener, you will lose the whole benefit for all models. This
seems to be so common that the optimization is not worth it.

If there is a single listener for post_init, it will add 0.07 seconds
to _all_ model initializations in the project per 10000 objects, no
matter if there is a listener for the _current_ model being
initialized. Same for post_init. So, in the case where you have
GenericForeignKey and ImageField in your project, there is an addition
of 0.16 seconds per 10000 objects created. Remember that without any
overhead the initialization would be 0.08 seconds in the trivial case.
So in the most trivial case almost 2/3 of the time would be used
sending signals, even though nobody is listening. For more realistic
models, the overhead is somewhere around 20%-30%.

It is possible to get rid of this overhead by recording the existence
of signal listeners per model, directly in Options. The class is
passed to the connect method, so it is trivial to set the
has_pre_init_listeners and has_post_init_listeners attributes in the
overridden .connect() method. I am not sure about the Options
availability, is it available whenever the class is available? It
seems to work, and the savings are as expected, with both pre_init and
post_init signals defined for model T2, 10000 T1 initializations take
0.21 seconds before and 0.08 seconds after. Worth fixing, no?

 - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to