#12826: Use __slots__ for some simple objects.
---------------------------------------------+------------------------------
          Reporter:  sebastian_noack         |         Owner:  nobody
            Status:  new                     |     Milestone:        
         Component:  Uncategorized           |       Version:        
        Resolution:                          |      Keywords:        
             Stage:  Design decision needed  |     Has_patch:  1     
        Needs_docs:  0                       |   Needs_tests:  0     
Needs_better_patch:  0                       |  
---------------------------------------------+------------------------------
Comment (by sebastian_noack):

 I agree that {{{__slots__}}} might be confusing to the user and restrict
 the flexibility of python. But for simple objects as those affected by my
 patch, this is no problem, because of the way they are used is not a
 problem with {{{__slots__}}}. So user's of django do not have to take care
 about it. But they care about django's performance. And other frameworks
 (e.g. jinja2) are using {{{__slots__}}} for some classes to improve
 performance.

 However, I have written a benchmark (see attachment). The result below,
 shows that for PyPy there is even a bigger speed win as for CPython, when
 using {{{__slots__}}}. I have done the benchmark on version 2.4 (which is
 equivalent to PyPy 1.0) and 2.6 of CPython. The speed gain for Python 2.4
 and 2.6 is similar. For CPython there is no difference in speed when the
 attributes defined by {{{__slots__}}} are not set. But usually they are
 set in the {{{__init__}}} method and in this case, classes using
 {{{__slots__}}} are always faster. But note, if {{{__slots__}}} is defined
 the object's {{{__dict__}}} is not created and in addition to the speed
 gain the main advantage is that it needs less memory.

 {{{
 $ pypy --version
 2.4.1 (pypy 1.0.0 build 55235)

 $ pypy benchmark.py
                     | Avg.  | Min.  | Max.
 --------------------+-------+-------+------
 WithSlots           | 0.136 | 0.116 | 0.152
 --------------------+-------+-------+------
 WithSlotsAndCtor    | 0.220 | 0.215 | 0.229
 --------------------+-------+-------+------
 WithoutSlots        | 0.153 | 0.142 | 0.176
 --------------------+-------+-------+------
 WithoutSlotsAndCtor | 0.287 | 0.276 | 0.304

 $ python2.4 benchmark.py
                     | Avg.  | Min.  | Max.
 --------------------+-------+-------+------
 WithSlots           | 0.021 | 0.018 | 0.025
 --------------------+-------+-------+------
 WithSlotsAndCtor    | 0.093 | 0.090 | 0.101
 --------------------+-------+-------+------
 WithoutSlots        | 0.021 | 0.020 | 0.021
 --------------------+-------+-------+------
 WithoutSlotsAndCtor | 0.107 | 0.102 | 0.113

 $ python2.6 benchmark.py
                     | Avg.  | Min.  | Max.
 --------------------+-------+-------+------
 WithSlots           | 0.026 | 0.024 | 0.028
 --------------------+-------+-------+------
 WithSlotsAndCtor    | 0.092 | 0.089 | 0.093
 --------------------+-------+-------+------
 WithoutSlots        | 0.026 | 0.025 | 0.028
 --------------------+-------+-------+------
 WithoutSlotsAndCtor | 0.113 | 0.107 | 0.125
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/12826#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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-updates?hl=en.

Reply via email to