Skip to content

Specialisation fail LOAD_ATTR_INSTANCE_VALUE specialised when no instance variable #158934

Description

@johng

Bug report

def test_load_attr_class_default_not_set_on_instance(self):
    class C:
        timeout = 30

        def set_timeout(self, value):
            self.timeout = value  # puts "timeout" in C's shared keys

    c = C()  # never sets timeout, so c.timeout is the class default

    @reset_code
    def get_timeout(n):
        for _ in range(n):
            value = c.timeout
        return value

    self.assertEqual(get_timeout(_testinternalcapi.SPECIALIZATION_THRESHOLD), 30)
    # Must load the class attribute, not specialize as an instance
    # attribute load that misses on every execution.
    self.assert_specialized(get_timeout, "LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES")
    self.assert_no_opcode(get_timeout, "LOAD_ATTR_INSTANCE_VALUE")

    # Setting the attribute on the instance must be seen.
    c.set_timeout(5)
    self.assertEqual(get_timeout(1), 5)

This is due to instance_has_key returning True even if setting an instance field is not actually set. Json dumps shows this issue with many instance level attributes not set by default relying on class level defaults

if separators is not None:

For json.dumps this can add about a 5% tax due to missed optimisations although it depends on the payload size. threading.Condition is another example from the stdlib.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

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-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions