Django

Code

Ticket #4561 (closed: duplicate)

Opened 1 year ago

Last modified 3 weeks ago

dispatch refactoring

Reported by: Brian Harring <ferringb@gmail.com> Assigned to: nobody
Milestone: Component: Core framework
Version: SVN Keywords: performance
Cc: jarek.zgoda@gmail.com Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Original ml discussion is here.

Short version; dispatch is a slow beast- basic design flaws follow

* signals are sent regardless of whether or not something is listening

* every emit/send requires searching for any matching sender, and matching signal receiver- additionally, requires looking up Any. The listeners change only when a new connection is added/goes out of scope, doing it every time at send is inefficient

* signals, while singleton instances, aren't used for anything more then effectively a constant; meaning all of the logic is shoved into dispatcher; due to this, there is no easy place to define the set of args sent to listeners, it's completely dependant on each send invocation using an adhoc agreed to standard. Due to a signal being just a constant, there is no easy space to make signals faster via calculating up front the receivers to notify (it could be implemented in dispatcher.*, but that code is already ugly/messy, and feels a bit fragile).

Attached is a patch adding the basic intelligence to signals; this folds in ticket #4521 (basic signal class with str/repr). Roughly, adds the following logic:

* for 'smart signals' (signals that are more then just constants), adds (enable|disable)_sender(sender) methods. In dispatcher.connect, if the signal is 'smart', signal.enable_sender(sender) is invoked- basically notifies the signal that something is actually listening, and what specifically is listening. In disconnect, a amtching disable_sender(sender) is added- same thing (notify that a receiver is going away).

* Tracking of all known 'smart signals'; this is used by Any, to multiplex enable_sender calls down into each individual signal

* a pre/post signal class, and func to generate the paired signals. This one is a bit more complex, and is the initial gain (aside from cleanup )from this refactoring. Basically, it modifies the sender on the fly, injecting pre/post dispatches if anything is listening- if nothing is listening, it leaves the target alone. Via this, I'm getting >25% faster Model instance instantiation for when nothing is listening for that specific model/signal.

pre_init, post_init, pre_save, post_save are updated to use the more efficient pre/post signal classes; the remaining django.db.models.signals.* signals are updated to use the new simple signal class (easier for debugging mainly). At this point, there should be no break in API either; basically is just shifting things around, and adding in a bit of delegation.

Patch is production usable at this point, although test coverage isn't yet 100%.

Thoughts/comments/nits welcome.

Attachments

dispatch-smart-signals.patch (24.8 kB) - added by Brian Harring <ferringb@gmail.com> on 06/14/07 09:40:14.
smart signals v1
dispatch-smart-signals-fix-bitrot.patch (24.8 kB) - added by Brian Harring <ferringb@gmail.com> on 07/12/07 03:45:42.
dispatch-smart-signals, correct for bitrot.

Change History

06/14/07 09:40:14 changed by Brian Harring <ferringb@gmail.com>

  • attachment dispatch-smart-signals.patch added.

smart signals v1

06/15/07 04:47:37 changed by Simon G. <dev@simon.net.nz>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Ready for checkin.
  • needs_tests changed.
  • needs_docs changed.

Hmm.. looks good, I'll mark it as RTC as the comments on the list suggest that everyone's most interested in refactorisation along these lines, and this seems to do that. Committers can drop it back to Accepted/DDN if they want to.

07/12/07 03:45:42 changed by Brian Harring <ferringb@gmail.com>

  • attachment dispatch-smart-signals-fix-bitrot.patch added.

dispatch-smart-signals, correct for bitrot.

03/27/08 07:01:40 changed by zgoda

  • cc set to jarek.zgoda@gmail.com.

06/15/08 15:05:28 changed by telenieko

  • status changed from new to closed.
  • resolution set to duplicate.

See #6814 for more, I'll mark this one as dup as the other one figures in the Roadmap to 1.0


Add/Change #4561 (dispatch refactoring)




Change Properties
Action