Skip to content

Improve generated code for some cases #55

@gvanrossum

Description

@gvanrossum

I just noticed that the generated code for this fragment could be better.

def f():
  x, y = 101, 102

The disassembly is:

  2           0 LOAD_CONST               1 ((101, 102))
              2 UNPACK_SEQUENCE          2
              4 STORE_FAST               0 (x)
              6 STORE_FAST               1 (y)

The UNPACK_SEQUENCE opcode is pretty complex, the code would probably run faster if we generated it like this:

LOAD_CONST 1 (101)
STORE_FAST 0 (x)
LOAD_CONST 2 (102)
STORE_FAST 2 (y)

Another example:

def f(a, b):
  x, y = a, b

Disassembled:

  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                1 (b)
              4 ROT_TWO
              6 STORE_FAST               2 (x)
              8 STORE_FAST               3 (y)

Why not

LOAD_FAST 0 (a)
STORE_FAST 2(x)
LOAD_FAST 1 (b)
STORE_FAST 3 (y)

?

That saves an opcode and a stack level. You'd have to reason about aliases, but for fast locals that's pretty simple, and threads can't interfere.

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