mnba / shedskin

Automatically exported from code.google.com/p/shedskin
0 stars 0 forks source link

optimize returned tuples #119

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Lots of code in Python, due to the inability to pass references to primitives, 
returns tuples instead of simply modifying the value like so:

def top_two(lst):
    return lst[0],lst[1]

where in C++ that translates roughly to (without all the template stuff):

function top_two(pylist * lst) {
    return new pytuple(lst->__getfast__(0),lst->__getfast__(1));
}

But it would be much faster to not create the tuple and not need GC to take 
care of it later like so:

function top_two(pylist * lst, int * ret_one, int * ret_two) {
    *one = lst->__getfast__(0);
    *two = lst->__getfast__(1);
}

This can be recognized in functions where the return type is a fixed-size 
tuple, and can be enabled via command line option since it technically changes 
the behavior.

Original issue reported on code.google.com by fahh...@gmail.com on 28 Nov 2010 at 7:51

GoogleCodeExporter commented 9 years ago
yep, lots of small tuples are definitely a problem for shedskin. it also fits 
with the escape analysis issue, in that we really want to be smarter about 
avoiding heap allocations. copy-by-value may be a good option for short tuples, 
since they're immutable, so there are no side-effects.

I sometimes solve this particular problem in Python by using globals or a 
special class:

class Retval: pass
RETVAL = Retval()

def top_two(lst, retval):
    RETVAL.one, RETVAL.two = lst[0], lst[1]

Original comment by mark.duf...@gmail.com on 28 Nov 2010 at 8:11

GoogleCodeExporter commented 9 years ago

Original comment by mark.duf...@gmail.com on 28 Nov 2010 at 8:11

GoogleCodeExporter commented 9 years ago

Original comment by mark.duf...@gmail.com on 16 Jul 2011 at 9:52