473,797 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.setrecursio nlimit(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 1204
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:
************pri nt*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.setrecursio nlimit(1000000)

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

class ConditionalBrea kNode(Node):
def GetDepState(sel f):
if randint(1,5) 1:
return Node.GetDepStat e(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(newn ode, depth+1)
else:
newnode = ConditionalBrea kNode()
nodes += [newnode]
try: S[curr_node] += [newnode]
except: S[curr_node] = [newnode]
S[newnode] = [nodes[0]]

AppendNode(node s[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.st ate*=*abs(randi nt(1,1000))
****def*GetDepS tate(self):
********return* self.state*+*ma x(s.GetDepState ()*for*s*in*S[self])

class ConditionalBrea kNode(Node):
****def*GetDepS tate(self):
********if*rand int(1,5)*>*1:
************ret urn*Node.GetDep State(self)
********else:
************ret urn*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
1792
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() method to find out the name and parameters of the method it was called from?
10
4295
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 incremented by the size of the variables declared in the called function, and then the function will know where these are in memory relative to the current stack pointer position. ALso how are variables returned, is that by a set position in the...
5
3647
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 wishes to avoid unnecessary garbage collection, but that is exactly what's going to happen if a lot of arrays, matrixes etc are allocated on the heap.
7
3684
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 of those parameters too. I'm trying to write a generic error handler. I wan't to be able to log the values that caused the error, without the user of the class having to specify the values 'cause
0
3678
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 code that does the call looks roughly like: try { // do reflective call response = method.Invoke(obj, params); }
24
2632
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 you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
6
1379
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? theNote is a Note type, isn't it? So shouldn't it call the method from the derived class? Or does it have something to do with the fact that the Write() method in Note uses the new keyword?
3
1198
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.. something like "application.page.method" - I could hardcode this for every ASP.net page, but I'm sure there must be something in .Net that gives me this for free. Does anyone know of something I could use?
4
2374
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 which I mean here. Just for a moment, let's just take one definition for one of the
0
9536
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.