7

Python 3.11 introduces new two parameters to the dis.dis function, show_caches and adaptive.

>>> import dis
>>>
>>> help(dis.dis)
Help on function dis in module dis:

dis(x=None, *, file=None, depth=None, show_caches=False, adaptive=False)
    Disassemble classes, methods, functions, and other compiled objects.

    With no argument, disassemble the last traceback.

    Compiled objects currently include generator objects, async generator
    objects, and coroutine objects, all of which store their code object
    in a special attribute.

What does this parameters means in python 3.11?. I did check the result by setting it to True but the result remains same as like setting it to False.

>>> dis.dis("a = 1", show_caches=True, adaptive=True)
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1)
              4 STORE_NAME               0 (a)
              6 LOAD_CONST               1 (None)
              8 RETURN_VALUE
>>>
>>>
>>> dis.dis("a = 1")
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1)
              4 STORE_NAME               0 (a)
              6 LOAD_CONST               1 (None)
              8 RETURN_VALUE
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
  • The `show_caches` parameter is explained somewhat higher up in the [docs](https://docs.python.org/3.11/library/dis.html): _Changed in version 3.11: Some instructions are accompanied by one or more inline cache entries, which take the form of CACHE instructions. These instructions are hidden by default, but can be shown by passing `show_caches=True` to any dis utility._ – Nelewout Oct 25 '22 at 07:52
  • 1
    @kaya3 It is in the docs, just not where one might expect them. See my answer. – DeepSpace Oct 25 '22 at 07:54

1 Answers1

4

The show_caches argument is described at the very top of dis documentation since it is possible to pass it to many of the module's functions:

Changed in version 3.11: Some instructions are accompanied by one or more inline cache entries, which take the form of CACHE instructions. These instructions are hidden by default, but can be shown by passing show_caches=True to any dis utility.

adaptive is described under CACHE documentation:

Rather than being an actual instruction, this opcode is used to mark extra space for the interpreter to cache useful data directly in the bytecode itself. It is automatically hidden by all dis utilities, but can be viewed with show_caches=True.

Logically, this space is part of the preceding instruction. Many opcodes expect to be followed by an exact number of caches, and will instruct the interpreter to skip over them at runtime.

Populated caches can look like arbitrary instructions, so great care should be taken when reading or modifying raw, adaptive bytecode containing quickened data.

New in version 3.11.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 2
    It would be helpful to have an example of an instruction accompanied by a CACHE instruction, to demonstrate this; it's not clear if `dis` would always show a `CACHE` instruction, or if it might show the contents of such a cache. The second part does contain the word "adaptive", but not in reference to the argument of that name, so it's not clear what that argument is supposed to do. – kaya3 Oct 25 '22 at 07:57
  • 1
    @DeepSpace Thank you for your answer. But I really appreciate if could include an example to demonstrate the usage of those two parameters. – Abdul Niyas P M Oct 26 '22 at 07:03