Skip to content

Specialize LOAD_METHOD #81

Closed
Closed
@Fidget-Spinner

Description

@Fidget-Spinner

Add specialization "families" for PEP 659 adaptive interpreter for LOAD_METHOD.

Background:

LOAD_METHOD is a compiler-level specialization of LOAD_ATTR. It's emitted with CALL_METHOD for calls of the form o.meth(), where o is not a top-level import. When this was added in 3.7, it brought 20% faster method calls by avoiding creating a bound method object.

Specialization

The specialization families for LOAD_METHOD are very similar to LOAD_ATTR (#52) and we can reuse many ideas and code. The speedup comes from avoiding a _PyObject_GetMethod which does two things:

  1. _PyType_Lookup (walk the MRO)
  2. Check if meth is in o.__dict__ to make sure it's not an attribute.

There are one or two specializations which I'm optimistic for:

  • LOAD_METHOD_WITH_HINT (see LOAD_ATTR_WITH_HINT) renamed to LOAD_METHOD_CACHED -- cache the descriptor object
  • Optional: LOAD_METHOD_WITH_HINT_NO_DICT -- specialized form of LOAD_METHOD_WITH_HINT for objects with no __dict__

These specializations make sense, but will likely bring little speedup in macrobenchmarks, so we need more profiling:

  • LOAD_METHOD_MODULE -- o is a module, almost same as specializing LOAD_ATTR_MODULE
  • LOAD_METHOD_CLASS -- o is a class and meth is a classmethod

Also, I don't expect much speedups for builtin methods (e.g {1,2,3}.keys()). Their _PyType_Lookup should be cheap due to the existing type method cache. Classes with a long MRO will probably benefit the most.

Workbranch: python/cpython#27722

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions