My favorites | Sign in
Project Home Downloads Wiki Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 119: optimize returned tuples
2 people starred this issue and may be notified of changes. Back to list
Status:  Accepted
Owner:  ----


Sign in to add a comment
 
Reported by fahh...@gmail.com, Nov 28, 2010
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.
Nov 28, 2010
Project Member #1 mark.duf...@gmail.com
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]
Status: Accepted
Nov 28, 2010
Project Member #2 mark.duf...@gmail.com
(No comment was entered for this change.)
Labels: -Type-Defect Type-Enhancement
Jul 16, 2011
Project Member #3 mark.duf...@gmail.com
(No comment was entered for this change.)
Summary: optimize returned tuples
Sign in to add a comment

Powered by Google Project Hosting