Method calls and stack consumption

Martin Manns mmanns at gmx.net
Sat Apr 14 21:54:41 EDT 2007


Hi,

Calling methods of other object instances seems quite expensive on the
stack (see example below). Is there a better way of traversing through
methods of instances that are connected in a cyclic graph? (The real
program's graph contains multiple successors in lists.)

class A(object):
    def __init__(self):
        self.i = 0
    def a(self):
        if self.i % 1000 == 0:
            print self.i
        self.i += 1                     
        return S[self].a()

a = A()
b = A()
S = {a:b, b:a}

import sys
sys.setrecursionlimit(1000000)
print a.a()


0
0
1000
1000
[...]
69000
69000
Segmentation fault
~ $ ulimit -s
65536
~ $

Thus, each call seems to be about 480 bytes on the stack.

Is there a good way to reduce stack consumption per call?
Could and should I resort to stackless?
If yes, how can the graph structure be realized?
Would I need a tasklet for each instance?

I am currently hesitant to depend on something from svn.
How stable and robust is stackless?

Martin



More information about the Python-list mailing list