Opened 3 years ago

Last modified 3 years ago

#21348 new defect

problems with __slots__

Reported by: vdelecroix Owned by:
Priority: major Milestone: sage-7.4
Component: performance Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by vdelecroix)

Python __slots__ allows to create Python classes without __dict__ attribute (that is there is a fixed set of attributes). This is done with declaring this list as in

class A(object):
    __slots__ = ["attr1", "attr2"]

Using slots reduces the memory footprint of Python objects and consequently allocation time

sage: class A(object): pass
sage: class B(object): __slots__ = []
sage: time l = [A() for _ in range(50000000)]  # ~4G of RAM
CPU times: user 26.3 s, sys: 708 ms, total: 27 s
Wall time: 27 s
sage: time l = [B() for _ in range(50000000)]  # ~1G of RAM
CPU times: user 5.26 s, sys: 376 ms, total: 5.64 s
Wall time: 5.64 s

However using this feature breaks a certain number of Sage features

  1. The function is_extension_type from sage.structure.misc. Such object would be recognized as "extension type". It might be fine but should be documented.
  1. cached_method does not work properly
    class A(object):
        __slots__ = []
        @cached_method
        def f(self):
            print "compute"
            return 1
    
    gives
    sage: a = A()
    sage: a.f()
    compute
    1
    sage: a.f()
    compute
    1
    

Change History (4)

comment:1 in reply to: ↑ description Changed 3 years ago by jdemeyer

Replying to vdelecroix:

  1. The function is_extension_type from sage.structure.misc. Such object would be recognized as "extension type". It might be fine but should be documented.

And the function is_extension_type should be renamed to what it really tests. For example, has_writable_dict might be a better name.

comment:2 Changed 3 years ago by vdelecroix

  • Component changed from PLEASE CHANGE to performance
  • Description modified (diff)
  • Type changed from PLEASE CHANGE to defect

comment:3 Changed 3 years ago by vdelecroix

Right. I just created the ticket to weight pros and cons... and whether some of Sage features can be adapted.

comment:4 Changed 3 years ago by vdelecroix

And BTW, type has a non-zero dict offset

sage: from sage.structure.misc import is_extension_type
sage: is_extension_type(type)
False
Note: See TracTickets for help on using tickets.