cython icon indicating copy to clipboard operation
cython copied to clipboard

[ENH] Missed optimization in keyword argument unpacking

Open da-woods opened this issue 2 years ago • 0 comments

def call(f, name1):
    return f(**{name1: 1})

This generates the C code

__pyx_t_1 = __Pyx_PyDict_NewPresized(1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)
  __Pyx_GOTREF(__pyx_t_1);
  if (unlikely(PyDict_Contains(__pyx_t_1, __pyx_v_name1))) {
    __Pyx_RaiseDoubleKeywordsError("function", __pyx_v_name1); __PYX_ERR(0, 2, __pyx_L1_error)
  } else {
    if (PyDict_SetItem(__pyx_t_1, __pyx_v_name1, __pyx_int_1) < 0) __PYX_ERR(0, 2, __pyx_L1_error)
  }
  __pyx_t_2 = __Pyx_PyObject_Call(__pyx_v_f, __pyx_empty_tuple, __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 2, __pyx_L1_error)

In this case PyDict_Contains is unnecessary because it's the first argument and is being added to an empty dict (it would be necessary for subsequent arguments of course)

Obviously this is a tiny and pretty rare case.

da-woods avatar Jul 17 '22 10:07 da-woods