Skip to content

Seemingly unnecessary complexification of foo(**kw) #86199

Open

Description

@masklinn
mannequin
BPO 42033
Nosy @masklinn, @markshannon, @serhiy-storchaka, @MojoVampire, @brandtbucher

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/markshannon'
closed_at = None
created_at = <Date 2020-10-14.10:48:31.902>
labels = ['3.9']
title = 'Seemingly unnecessary complexification of foo(**kw)'
updated_at = <Date 2020-12-16.14:34:05.732>
user = 'https://github.com/masklinn'

bugs.python.org fields:

activity = <Date 2020-12-16.14:34:05.732>
actor = 'josh.r'
assignee = 'Mark.Shannon'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2020-10-14.10:48:31.902>
creator = 'xmorel'
dependencies = []
files = []
hgrepos = []
issue_num = 42033
keywords = []
message_count = 5.0
messages = ['378613', '378667', '378674', '378686', '383172']
nosy_count = 5.0
nosy_names = ['xmorel', 'Mark.Shannon', 'serhiy.storchaka', 'josh.r', 'brandtbucher']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue42033'
versions = ['Python 3.9']

Linked PRs

Activity

  1. masklinn commented on Oct 14, 2020

    masklinnmannequin
    MannequinAuthor

    Following bpo-39320 the highly specialised bytecode for vararg calls were replaced by simpler ones, but there seems to be at least one area where the generated bytecode regressed for possibly no reason?

    In Python 3.8, foo(**var) compiles to:

    0 LOAD_GLOBAL 0 (foo)
    2 BUILD_TUPLE 0
    4 LOAD_FAST 2 (var)
    6 CALL_FUNCTION_EX 1

    In Python 3.9, it compiles to:

    0 LOAD_GLOBAL 0 (foo)
    2 BUILD_TUPLE 0
    4 BUILD_MAP 0
    6 LOAD_FAST 2 (var)
    8 DICT_MERGE 1
    0 CALL_FUNCTION_EX 1

    The PR 18141 does not seem to change the implementation of CALL_FUNCTION_EX so I would expect that if it was fine with taking the var arbitrary mapping before it stil is now, and the extra two opcodes (and creation of a dict) is unnecessary?

  2. markshannon commented on Oct 15, 2020

    @markshannon
    Member

    Have you observed any slowdown or incorrect behaviour?

    The 3.8 bytecode looks incorrect to me.
    The C-API documentation doesn't prohibit callables from mutating the dictionary they receive.
    Unless a copy is made, then a callee could mutate var.

    https://docs.python.org/3/c-api/call.html

  3. masklinn commented on Oct 15, 2020

    masklinnmannequin
    MannequinAuthor

    I have not noticed anything, I was just looking at the bytecode changes and stumbled upon this oddity. Though I would expect a small slowdown as every fn(**kw) would now incur an extra dict copy, unless there’s something in call_function_ex which copies the input dict iff its ref count is not one?

    For whatever that’s worth, the 3.8 bytecode has been there since call_function_ex was added in 3.6 and before that call_function_kw looks identical (load_global foo, load_local var, call_function_kw)

  4. serhiy-storchaka commented on Oct 15, 2020

    @serhiy-storchaka
    Member
    $ python3.8 -m timeit -s "a = {'a': 1}" "dict(**a)"
    2000000 loops, best of 5: 113 nsec per loop
    $ python3.9 -m timeit -s "a = {'a': 1}" "dict(**a)"
    2000000 loops, best of 5: 181 nsec per loop
  5. MojoVampire commented on Dec 16, 2020

    MojoVampiremannequin
    Mannequin

    Even if making a copy is necessary when the underlying function receives the dict "raw", preemptively performing the copy (before knowing if the function being called is a Vectorcall function) means that when it's a Vectorcall function (e.g. all user-defined functions, right?), instead of just copying from the original dict to the unpacked stack for vectorcall, it makes an intermediate copy, then copies from that copy to the unpacked stack later on; the copy is otherwise completely unused.

    The extra bytecode isn't even defending against "dict-like" kwargs, because CALL_FUNCTION_EX itself already copies to a true dict for anything that's not an exact dict (that defense shouldn't even be there if the bytecode compiler is already guaranteeing a true dict).

    Seems like, if preventing the caller's dict from being passed directly to the underlying function is necessary and intended, it should be done in PyObject_Call (which can avoid the copy entirely when call a Vectorcall function and when the reference count on the dict is 1), not at the bytecode interpreter layer. As is, PyObject_Call is already violating the documented behavior by *not* matching the behavior of callable(*args, **kwargs) (see bpo-42629), so moving it to PyObject_Call would fix that problem and improve performance passing a single kwargs.

  6. transferred this issue fromon Apr 10, 2022
  7. MojoVampire commented on Apr 30, 2022

    @MojoVampire
    Contributor

    I've investigated fixing this. It's pretty easy to special-case the compiler to not emit the BUILD_MAP 0/DICT_MERGE when there's only one source of keyword args and it's being ** unpacked.

    The downside to this is that, per #86795, PyObject_Call does nothing to prevent a dict that is both passed as **kwargs and received as a keywords dict (via non-vectorcall APIs) from being modified by the callee. User-defined functions (at the Python level) are all vectorcall now (right?), so that's fine, but without the BUILD_MAP 0/DICT_MERGE, any METH_VARARGS | METH_KEYWORDS C level function will called with an alias of the original dict, and any modifications made will affect the caller's dict.

    I see four options for fixing this:

    1. Disable BUILD_MAP 0+DICT_MERGE and leave everything else unchanged:
      Pro: Most performant, never makes unnecessary copies
      Can: Running callable(**kwargs) where callable isn't vectorcall will allow callable to change the caller's kwargs (do we make explicit guarantees here?)

    2. Disable BUILD_MAP 0+DICT_MERGE and modify CALL_FUNCTION_EX to apply the copying technique for inexact dicts for dicts with refcnt > 1:
      Pro: Running callable(**kwargs) will never modify caller's kwargs
      Con: A wasted copy when callable turns out to be vectorcall (equivalent to the wasted copy from BUILD_MAP 0+DICT_MERGE, but removing those two opcodes, which would probably make it a bit faster)

    3. Disable BUILD_MAP 0+DICT_MERGE and modify PyObject_Call to apply copy any incoming dicts when following the non-vectorcall path
      Pros: Running callable(**kwargs) will never modify caller's kwargs. Even C API users can rely on this guaranteed copy. No copies made when following vectorcall path
      Con: C API users who want to pass kwargs through (with the intent of either handing off ownership or expecting the callee to mutate it for beneficial reasons) can't do so; the decision to copy is made for them.

    4. Leave the BUILD_MAP 0+DICT_MERGE in place, and remove the special-case for inexact dicts in CALL_FUNCTION_EX (I suspect we could also drop the special-case for inexact tuples too, for the same reason, but I haven't checked thoroughly)
      Pro: Shrink the eval loop; the inexact dict case can never be hit with the guaranteed BUILD_MAP involved, so we can dump half of the code in CALL_FUNCTION_EX (if it's just the dict case we can eliminate) or almost all of it (if we can eliminate the tuple special casing too).
      Con: All user passed **kwargs will be a little bit slower in the common case where they'd ultimately reach a vectorcall function

    Performance-wise, options #2 and #4 are probably the worst (since they preserve an unnecessary copy in the common case where the callable is vectorcall). #1 is the best performance, but might constitute a behavior regression and violate expectations (guarantees?). #3 slows things down in a rare case (C API callers calling non-vectorcall functions), but arguably makes things more consistent (making the documentation claim that PyObject_Call(callable, args, kwds) is equivalent to callable(*args, **kwargs) fully correct).

    Any thoughts?

  8. MojoVampire commented on Apr 30, 2022

    @MojoVampire
    Contributor

    I'm considering submitting a PR for option #3 during the upcoming sprints, so please let me know ASAP if there is a problem with it.

  9. added 2 commits that reference this issue on May 2, 2022
    52b6ec4
    b205d6b
  10. MojoVampire commented on May 2, 2022

    @MojoVampire
    Contributor

    I've submitted a PR for this and #86795 (since it's not possible to fix this issue without fixing #86795 too). A tiny patch to the compiler makes it not perform the BUILD_MAP+DICT_MERGE when the only source of keyword arguments is a single **kwargs. Tests added to ensure no future regression causes the dict to ever reach a callee without being copied.

    Performance-wise, using Serhiy's case, we save about 30% on overhead:

    $ ./python_unmodified_20220429 -m timeit -s "a = {'a': 1}" "dict(**a)"
    2000000 loops, best of 5: 120 nsec per loop
    $ ./python3.dictoptimize -m timeit -s "a = {'a': 1}" "dict(**a)"
    5000000 loops, best of 5: 88.7 nsec per loop
    

    Same behavior seen when a = {} (30% faster). When any other keyword arguments are passed (meaning the new dict has to be built regardless), no savings are observed (on repeated runs, passing b=1 either before or after **a, the times are all within 3%, and there's no pattern to which one wins):

    $ ./python_unmodified_20220429 -m timeit -s "a = {'a': 1}" "dict(b=1, **a)"
    2000000 loops, best of 5: 167 nsec per loop
    $ ./python3.dictoptimize -m timeit -s "a = {'a': 1}" "dict(b=1, **a)"
    2000000 loops, best of 5: 160 nsec per loop
    $ ./python_unmodified_20220429 -m timeit -s "a = {'a': 1}" "dict(**a, b=1)"
    2000000 loops, best of 5: 191 nsec per loop
    $ ./python3.dictoptimize -m timeit -s "a = {'a': 1}" "dict(**a, b=1)"
    2000000 loops, best of 5: 198 nsec per loop
    

    The winner swaps from run to run, so I'm guessing it's unrelated to the change (which changed nothing in the code path followed by this use case).

    This does involve a small regression in the time required to call non-vectorcall functions from the C layer (required to make PyObject_Call adhere to the documented equivalence to callable(*args, **kwargs), which never allows the original kwargs to reach the callee). It's hard to test without my _testcapi addition though. Rather than continue the microbenchmarks, I'm going to try running the pyperformance benchmarks to check behavior on more varied code.

  11. MojoVampire commented on May 2, 2022

    @MojoVampire
    Contributor

    pyperformance numbers. orig was built from the base checkout from April 30th that the PR is based on, dictoptimize is with my changes. Both builds were made with ./configure --enable-optimizations --with-lto, compiled using gcc 10.3.1, run in an Alpine Linux 3.15 container running under WSLv2 on Windows:

    
    +-------------------------+---------+-----------------------+
    | Benchmark               | orig    | dictoptimize          |
    +=========================+=========+=======================+
    | pidigits                | 318 ms  | 279 ms: 1.14x faster  |
    +-------------------------+---------+-----------------------+
    | pathlib                 | 18.8 ms | 16.6 ms: 1.13x faster |
    +-------------------------+---------+-----------------------+
    | html5lib                | 61.8 ms | 56.2 ms: 1.10x faster |
    +-------------------------+---------+-----------------------+
    | django_template         | 33.5 ms | 30.7 ms: 1.09x faster |
    +-------------------------+---------+-----------------------+
    | 2to3                    | 259 ms  | 238 ms: 1.09x faster  |
    +-------------------------+---------+-----------------------+
    | json_dumps              | 12.5 ms | 11.6 ms: 1.08x faster |
    +-------------------------+---------+-----------------------+
    | pickle_pure_python      | 313 us  | 292 us: 1.07x faster  |
    +-------------------------+---------+-----------------------+
    | pickle                  | 12.1 us | 11.4 us: 1.06x faster |
    +-------------------------+---------+-----------------------+
    | dulwich_log             | 106 ms  | 99.7 ms: 1.06x faster |
    +-------------------------+---------+-----------------------+
    | genshi_xml              | 56.8 ms | 53.8 ms: 1.06x faster |
    +-------------------------+---------+-----------------------+
    | nbody                   | 79.6 ms | 75.5 ms: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | logging_format          | 6.78 us | 6.43 us: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | hexiom                  | 5.63 ms | 5.36 ms: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | meteor_contest          | 97.2 ms | 92.6 ms: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | fannkuch                | 346 ms  | 330 ms: 1.05x faster  |
    +-------------------------+---------+-----------------------+
    | float                   | 68.1 ms | 64.9 ms: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | genshi_text             | 21.2 ms | 20.3 ms: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | logging_simple          | 6.04 us | 5.77 us: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | deltablue               | 3.52 ms | 3.37 ms: 1.05x faster |
    +-------------------------+---------+-----------------------+
    | go                      | 128 ms  | 123 ms: 1.05x faster  |
    +-------------------------+---------+-----------------------+
    | scimark_sparse_mat_mult | 3.69 ms | 3.54 ms: 1.04x faster |
    +-------------------------+---------+-----------------------+
    | mako                    | 10.2 ms | 9.80 ms: 1.04x faster |
    +-------------------------+---------+-----------------------+
    | unpack_sequence         | 39.9 ns | 38.4 ns: 1.04x faster |
    +-------------------------+---------+-----------------------+
    | pickle_dict             | 26.0 us | 25.1 us: 1.04x faster |
    +-------------------------+---------+-----------------------+
    | json_loads              | 23.1 us | 22.5 us: 1.03x faster |
    +-------------------------+---------+-----------------------+
    | sympy_expand            | 427 ms  | 415 ms: 1.03x faster  |
    +-------------------------+---------+-----------------------+
    | pyflate                 | 396 ms  | 385 ms: 1.03x faster  |
    +-------------------------+---------+-----------------------+
    | python_startup_no_site  | 6.18 ms | 6.02 ms: 1.03x faster |
    +-------------------------+---------+-----------------------+
    | chameleon               | 7.04 ms | 6.86 ms: 1.03x faster |
    +-------------------------+---------+-----------------------+
    | chaos                   | 62.5 ms | 60.9 ms: 1.02x faster |
    +-------------------------+---------+-----------------------+
    | tornado_http            | 199 ms  | 194 ms: 1.02x faster  |
    +-------------------------+---------+-----------------------+
    | nqueens                 | 76.5 ms | 74.7 ms: 1.02x faster |
    +-------------------------+---------+-----------------------+
    | pickle_list             | 3.47 us | 3.41 us: 1.02x faster |
    +-------------------------+---------+-----------------------+
    | scimark_lu              | 92.7 ms | 91.0 ms: 1.02x faster |
    +-------------------------+---------+-----------------------+
    | raytrace                | 267 ms  | 262 ms: 1.02x faster  |
    +-------------------------+---------+-----------------------+
    | sympy_str               | 265 ms  | 261 ms: 1.02x faster  |
    +-------------------------+---------+-----------------------+
    | python_startup          | 8.73 ms | 8.58 ms: 1.02x faster |
    +-------------------------+---------+-----------------------+
    | scimark_fft             | 276 ms  | 271 ms: 1.02x faster  |
    +-------------------------+---------+-----------------------+
    | scimark_sor             | 102 ms  | 101 ms: 1.01x faster  |
    +-------------------------+---------+-----------------------+
    | xml_etree_process       | 57.2 ms | 56.5 ms: 1.01x faster |
    +-------------------------+---------+-----------------------+
    | regex_v8                | 20.0 ms | 19.7 ms: 1.01x faster |
    +-------------------------+---------+-----------------------+
    | sympy_sum               | 158 ms  | 156 ms: 1.01x faster  |
    +-------------------------+---------+-----------------------+
    | sympy_integrate         | 18.8 ms | 18.6 ms: 1.01x faster |
    +-------------------------+---------+-----------------------+
    | unpickle                | 11.6 us | 12.2 us: 1.05x slower |
    +-------------------------+---------+-----------------------+
    | Geometric mean          | (ref)   | 1.03x faster          |
    +-------------------------+---------+-----------------------+
    
    Benchmark hidden because not significant (15): xml_etree_iterparse, logging_silent, regex_compile, sqlite_synth, crypto_pyaes, telco, regex_effbot, xml_etree_parse, spectral_norm, regex_dna, unpickle_list, scimark_monte_carlo, unpickle_pure_python, richards, xml_etree_generate
    
  12. MojoVampire commented on May 2, 2022

    @MojoVampire
    Contributor

    So it looks like a grand total of one benchmark got slower (unpickle), 15 were unchanged, and 43 got at least 1% faster, overall gain of 3% across all the benchmarks.

  13. 3 remaining items

  14. added
    3.12only security fixes
    and removed
    3.11only security fixes
    on Sep 11, 2022
  15. added 8 commits that reference this issue on Aug 24, 2026
  16. added 2 commits that reference this issue on Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions