Title: Generate LOAD_ATTR+CALL_FUNCTION instead of LOAD_METHOD+CALL_METHOD for imports
Type: Stage: patch review
Components: Interpreter Core Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: BTaskaya Nosy List: BTaskaya, Mark.Shannon, corona10
Priority: normal Keywords: patch

Created on 2021-06-04 16:33 by BTaskaya, last changed 2021-06-11 17:43 by BTaskaya.

Pull Requests
URL Status Linked Edit
PR 26677 open BTaskaya, 2021-06-11 17:43
Messages (3)
msg395099 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2021-06-04 16:33
import foo
def func():

The snippet above will generate the following code;

  2           0 LOAD_GLOBAL              0 (foo)
              2 LOAD_METHOD              1 (bar)
              4 CALL_METHOD              0
              6 RETURN_VALUE

Though this will make things harder for specializing the LOAD_ATTR for modules since now the handling of LOAD_METHOD for that case is necessary so for the imports that we can infer during the symbol analysis pass, we'll generate LOAD_ATTR+CALL_ATTR instead of LOAD_METHOD+CALL_METHOD and hopefully the generated code will get specialized via the PEP 659.

msg395103 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2021-06-04 16:40
@mark.shannon what do you think about doing this both for `import <module>` and `from <module> import <something>`. It will definitely simplify the implementation (no need to extra visitors or flags, just using the default DEF_IMPORT one and also might help you to specialize other cases too (e.g importing submodules, from concurrent import futures).
msg395516 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-06-10 08:37
Yes. Simpler is good.

I think it will also be better for performance:

In general, we don't know what X is in `from Y import X`. It could be a module or anything else.

However, if we are accessing an attribute it is quite likely to be a module or class.
For `X` defined by `from Y import X`, `X` is likely to be a module, class, function, or some sort of constant like a string, int or Enum.

If it is a string, int or function then it is rare to call a method on it, so we can ignore that case.
Calling methods on an Enum constant is probably not very common either (I'm guessing here)

For a module, `LOAD_ATTR; CALL_FUNCTION` is clearly better than `LOAD_METHOD; CALL_METHOD`.

For a class, specializing `LOAD_ATTR` is no more complex than `LOAD_METHOD`, probably simpler.
So, for a class `LOAD_ATTR; CALL_FUNCTION` is no worse than `LOAD_METHOD; CALL_METHOD`, and might be better.

Overall, it looks like `` when `X` is defiend by `from Y import X` is best as `LOAD_ATTR; CALL_FUNCTION` not `LOAD_METHOD; CALL_METHOD`.
Date User Action Args
2021-06-11 17:43:04BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request25263
2021-06-10 09:31:51corona10setnosy: + corona10
2021-06-10 08:37:15Mark.Shannonsetmessages: + msg395516
2021-06-04 16:40:59BTaskayasetmessages: + msg395103
2021-06-04 16:33:18BTaskayacreate