Commit a30fd24f authored by Stefan Behnel's avatar Stefan Behnel

avoid generating loop code if there is only one kw-only argument to fetch

parent 8112697d
...@@ -3905,20 +3905,25 @@ class DefNodeWrapper(FuncDefNode): ...@@ -3905,20 +3905,25 @@ class DefNodeWrapper(FuncDefNode):
first_optional_arg = max_positional_args + i first_optional_arg = max_positional_args + i
optional_args.append(arg.name) optional_args.append(arg.name)
if optional_args: if optional_args:
# if we receive more than the named kwargs, we either have **kwargs if len(optional_args) > 1:
# (in which case we must iterate anyway) or it's an error (which we # if we receive more than the named kwargs, we either have **kwargs
# also handle during iteration) => skip this part if there are more # (in which case we must iterate anyway) or it's an error (which we
code.putln('if (kw_args > 0 && %s(kw_args <= %d)) {' % ( # also handle during iteration) => skip this part if there are more
not self.starstar_arg and 'likely' or '', code.putln('if (kw_args > 0 && %s(kw_args <= %d)) {' % (
len(optional_args))) not self.starstar_arg and 'likely' or '',
code.putln('Py_ssize_t index;') len(optional_args)))
# not unrolling the loop here reduces the C code overhead code.putln('Py_ssize_t index;')
code.putln('for (index = %d; index < %d && kw_args > 0; index++) {' % ( # not unrolling the loop here reduces the C code overhead
first_optional_arg, first_optional_arg + len(optional_args))) code.putln('for (index = %d; index < %d && kw_args > 0; index++) {' % (
first_optional_arg, first_optional_arg + len(optional_args)))
else:
code.putln('if (kw_args == 1) {')
code.putln('const Py_ssize_t index = %d;' % first_optional_arg)
code.putln('PyObject* value = PyDict_GetItem(%s, *%s[index]);' % ( code.putln('PyObject* value = PyDict_GetItem(%s, *%s[index]);' % (
Naming.kwds_cname, Naming.pykwdlist_cname)) Naming.kwds_cname, Naming.pykwdlist_cname))
code.putln('if (value) { values[index] = value; kw_args--; }') code.putln('if (value) { values[index] = value; kw_args--; }')
code.putln('}') if len(optional_args) > 1:
code.putln('}')
code.putln('}') code.putln('}')
code.putln('if (unlikely(kw_args > 0)) {') code.putln('if (unlikely(kw_args > 0)) {')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment