473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Method calls and stack consumption

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
Apr 15 '07 #1
3 1191
Martin Manns wrote:
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}
a = a.a
while True:
a = a()

That's how you can do it if your real program is similar enough to the
example...

Peter

Apr 15 '07 #2
On Sun, 15 Apr 2007 07:27:25 +0200
Peter Otten <__*******@web.dewrote:
Martin Manns wrote:
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.)
a = a.a
while True:
a = a()

That's how you can do it if your real program is similar enough to the
example...
Thanks for pointing out the oversimplified nature of the original
example.I hope that the following one clarifies the problem.

(I do not know, what has to be stored on the stack, but it should not be
that much, because all recursion calls originate from inside the return
statement.)
from random import randint,seed
import sys
seed()
sys.setrecursionlimit(1000000)

class Node(object):
def __init__(self):
self.state = abs(randint(1,1000))
def GetDepState(self):
return self.state + max(s.GetDepState() for s in S[self])

class ConditionalBreakNode(Node):
def GetDepState(self):
if randint(1,5) 1:
return Node.GetDepState(self)
else:
return self.state

S = {}
nodes = [Node()]

def AppendNode(curr_node, depth=1):
global nodes
r = randint(1,30)
if r >= depth:
for i in xrange(randint(1,3)):
newnode = Node()
nodes += [newnode]
try: S[curr_node] += [newnode]
except: S[curr_node] = [newnode]
AppendNode(newnode, depth+1)
else:
newnode = ConditionalBreakNode()
nodes += [newnode]
try: S[curr_node] += [newnode]
except: S[curr_node] = [newnode]
S[newnode] = [nodes[0]]

AppendNode(nodes[0])
print len(nodes)
print nodes[0].GetDepState()

Apr 15 '07 #3
Martin Manns wrote:
Thanks for pointing out the oversimplified nature of the original
example.I hope that the following one clarifies the problem.

(I do not know, what has to be stored on the stack, but it should not be
that much, because all recursion calls originate from inside the return
statement.)
class Node(object):
****def*__init__(self):
********self.state*=*abs(randint(1,1000))
****def*GetDepState(self):
********return*self.state*+*max(s.GetDepState()*fo r*s*in*S[self])

class ConditionalBreakNode(Node):
****def*GetDepState(self):
********if*randint(1,5)*>*1:
************return*Node.GetDepState(self)
********else:
************return*self.state
Have you investigated the libraries available for that kind of problem? (No,
I don't know them) Maybe one of them provides an implementation that does
not depend on the stack.

On the other hand, if the above is the "natural" expression of your problem,
why not give stackless a try?

Peter
Apr 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: juchytil | last post by:
Here is a method that calls a static GetInfo() method on the MyObject class. public void MethodName(string param1, int param2) { MyObject.GetInfo(); } Is there any way for the GetInfo()...
10
by: Michael | last post by:
Guys, I'm interested in how the compiler implements function calls, can anyone correct my understanding/point me towards some good articles. When a function is called, is the stack pointer...
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
7
by: Brad Quinn | last post by:
Is there a way to get the values of the paramaters to a method programatically? I know that I can use reflection to find out the parameter names and types, etc., but I want to know the values...
0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
6
by: John Salerno | last post by:
I have a question about how one of these methods is called. The call theNote.Write(); uses the Write() method in the Document class, and not the Write() from the Note class. Why is this?...
3
by: Nirbho | last post by:
Hi, I'm newish to C# .Net. I'm doing some error handling in my ASP.net pages. When handling the error in the catch block, I want to pass through the namespace of where the error occured.....
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.