Method Call Top 3

Key internal steps contributing to Python method call overhead include namespace lookup, fetching the function object, and the descriptor protocol invocation for binding.

The top 3

  1. Dynamic Attribute Lookup: The MRO Traversal Cost: For every method call, Python's interpreter performs a dynamic attribute lookup, traversing the Method Resolution Order (MRO) of the class hierarchy, which is a significant overhead not present in statically compiled languages. This `PyType_Lookup` operation is a primary factor in method resolution slowness.
  2. Descriptor Protocol's Binding Overhead: When a method is accessed via an instance, Python's descriptor protocol invokes the method's `__get__` method. This dynamically creates a *bound method object* (e.g., `types.MethodType`), which encapsulates the original function and the instance (`self`), adding a distinct overhead compared to direct function pointers.
  3. Function Call Frame & Argument Setup Tax: Beyond method resolution, each Python function call incurs overhead for creating a new execution frame and managing argument passing, including 'boxing' and 'unboxing' values. This setup cost is substantial, with an empty Python function call taking approximately 150ns.

Sources

Open the full topic