Skip to content

Don't enable GC for classes that don't add new fields #67914

Closed

Description

@eltoder
mannequin
BPO 23726
Nosy @pitrou, @benjaminp, @njsmith, @eltoder, @serhiy-storchaka
Files
  • class_gc.diff
  • class_gc2.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2015-04-13.18:10:31.603>
    created_at = <Date 2015-03-20.20:25:57.755>
    labels = ['interpreter-core', 'performance']
    title = "Don't enable GC for classes that don't add new fields"
    updated_at = <Date 2015-04-13.19:30:52.528>
    user = 'https://github.com/eltoder'

    bugs.python.org fields:

    activity = <Date 2015-04-13.19:30:52.528>
    actor = 'eltoder'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-04-13.18:10:31.603>
    closer = 'pitrou'
    components = ['Interpreter Core']
    creation = <Date 2015-03-20.20:25:57.755>
    creator = 'eltoder'
    dependencies = []
    files = ['38610', '38624']
    hgrepos = []
    issue_num = 23726
    keywords = ['patch']
    message_count = 11.0
    messages = ['238718', '238721', '238727', '238730', '238732', '238733', '240690', '240691', '240699', '240710', '240711']
    nosy_count = 6.0
    nosy_names = ['pitrou', 'benjamin.peterson', 'njs', 'python-dev', 'eltoder', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue23726'
    versions = ['Python 3.5']

    Activity

    1. eltoder commented on Mar 20, 2015

      eltodermannequin
      MannequinAuthor

      As far as I can tell, if a new class does not add any new fields, and its base class doesn't use GC, there's no reason to enable GC for the new class.
      This is useful for creating lightweight wrappers around classes implemented in C.

    2. added
      performance
      on Mar 20, 2015
    3. added
      interpreter-core
      on Mar 20, 2015
    4. serhiy-storchaka commented on Mar 20, 2015

      @serhiy-storchaka
      Member
      >>> class UserIntSlots(int):
      ...     __slots__ = ()
      ...     def __repr__(s): return '5'
      ... 
      >>> (4).__class__ = UserIntSlots
      >>> 2+2
      5
      >>> type(2+2)
      <class '__main__.UserIntSlots'>

      It looks weird.

    5. eltoder commented on Mar 20, 2015

      eltodermannequin
      MannequinAuthor

      Agreed, but this is not new. This works without my change:

      >>> class Tuple(tuple):
      ...   __slots__ = ()
      ...   def __repr__(self): return 'Imma tuple!'                                                    
      ... 
      >>> ().__class__ = Tuple
      >>> ()
      Imma tuple!
    6. pitrou commented on Mar 20, 2015

      @pitrou
      Member

      I wasn't aware of that :-o

    7. serhiy-storchaka commented on Mar 20, 2015

      @serhiy-storchaka
      Member

      May be we should forbid assigning the __class__ attribute of basic builtin
      types (especially internable, such as int, str, bytes, tuple, bool, NoneType).

    8. eltoder commented on Mar 20, 2015

      eltodermannequin
      MannequinAuthor

      Actually, this is rather new -- new in 3.5. The check was relaxed in bpo-22986: https://hg.python.org/cpython/rev/c0d25de5919e
      Previously only heap types allowed re-assigning __class__.

    9. python-dev commented on Apr 13, 2015

      python-devmannequin
      Mannequin

      New changeset a60b7945ef87 by Antoine Pitrou in branch 'default':
      Issue bpo-23726: Don't enable GC for user subclasses of non-GC types that don't add any new fields.
      https://hg.python.org/cpython/rev/a60b7945ef87

    10. pitrou commented on Apr 13, 2015

      @pitrou
      Member

      I've pushed the patch, thank you!

    11. eltoder commented on Apr 13, 2015

      eltodermannequin
      MannequinAuthor

      Thank you!

      Benjamin, Nathaniel, any opinion if we should restrict reassigning __class__ for types like tuple, int and str, where some/many instances are cached?

    12. njsmith commented on Apr 13, 2015

      @njsmith
      Contributor

      Yes, it probably would be a good idea to disallow assigning __class__ for immutable types.

    13. eltoder commented on Apr 13, 2015

      eltodermannequin
      MannequinAuthor

      Agreed. There's a small problem with that, as far as I know. Nothing on type declares that it is immutable.

    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-coreperformance

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions