[Python-ideas] Ordering keyword dicts

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Jun 10 09:09:10 CEST 2013


On Mon, Jun 10, 2013 at 2:33 AM, Eric Snow <ericsnowcurrently at gmail.com>
wrote:
>
>
> On Jun 9, 2013 11:15 PM, "Nick Coghlan" <ncoghlan at gmail.com> wrote:
>
> > It may be that the cost of the flag check is swamped by the cost of
> > actually creating the keyword dictionary, in which case the runtime
> > check would be a preferable design choice, since function *invocation*
> > wouldn't need to change, only function declarations.
>
> I'm actually doing some of this checking incidentally as part of testing
my > OrderedDict implementation.  I'm using a decorator to set the flag.
 Don't > have anything conclusive to say yet.

Let's not duplicate the effort.  I started experimenting with a patch on
top of issue16991 which is essentially this:


--- a/Python/ceval.c Mon Jun 10 02:24:00 2013 -0400
+++ b/Python/ceval.c Mon Jun 10 02:58:38 2013 -0400
@@ -3378,7 +3378,7 @@

     /* Parse arguments. */
     if (co->co_flags & CO_VARKEYWORDS) {
-        kwdict = PyDict_New();
+        kwdict = PyODict_New();
         if (kwdict == NULL)
             goto fail;
         i = total_args;
@@ -3441,7 +3441,7 @@
                          keyword);
             goto fail;
         }
-        PyDict_SetItem(kwdict, keyword, value);
+        PyODict_SetItem(kwdict, keyword, value);
         continue;
       kw_found:
         if (GETLOCAL(j) != NULL) {

This was enough to get

>>> def f(**kw): return kw
...
>>> f(a=2, b=3)
OrderedDict([('a', 2), ('b', 3)])

In this particular code path, checking for another flag should not cost
anything in performance: even if compiler will not optimize it completely
the flags will be in a register and at most the other check will cost a few
CPU cycles and with no memory access.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130610/e8876750/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: odict-kw.diff
Type: application/octet-stream
Size: 976 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130610/e8876750/attachment.obj>


More information about the Python-ideas mailing list