Question about Python and Memory Usage...

The Capo

godless Heathen
Joined
Jan 29, 2001
Messages
9,302
Location
Washington, DC
I am in the process of creating a mod, and I noticed after adding a new python wonder (the Flavian Amphitheatre) I am starting to experience sporadic MAFs. Which is upsetting because, up until this point, I had played and finished many long games without a MAF.

I was just wondering if there was a way to tell how much memory is wasted on a particular function. I am asking this as a general question, but here is a specific example of the wonder I am talking about. Here is the code for it:

Code:
	def onUnitBuilt(self, argsList):
		'Unit Completed'
		city = argsList[0]
		unit = argsList[1]
		player = PyPlayer(city.getOwner())

## Topkapi Palace Start ##

		pCity = argsList[0]
		pUnit = argsList[1]
		pPlayer = gc.getPlayer(pUnit.getOwner())
		iUnitType = pUnit.getUnitType()

		b_BUILDING_TOPKAPI = gc.getInfoTypeForString("BUILDING_FLAVIAN")
		obsoleteTech = gc.getBuildingInfo(b_BUILDING_TOPKAPI).getObsoleteTech()
		if ( gc.getTeam(pPlayer.getTeam()).isHasTech(obsoleteTech) == false or obsoleteTech == -1 ):        
			if ( pPlayer.getBuildingClassCount(gc.getInfoTypeForString("BUILDINGCLASS_FLAVIAN"))>0 ):            
				iTeam = pPlayer.getTeam()
				pTeam = gc.getTeam(iTeam)
				l_vassalUB = []
				for iPlayer in range(gc.getMAX_PLAYERS()):
					ppPlayer = gc.getPlayer(iPlayer)
					if ( (ppPlayer.isAlive()==true) and (ppPlayer.isBarbarian()==false) ):                    
						if ( (gc.getTeam(ppPlayer.getTeam()).isHasMet(iTeam)) and (ppPlayer.isAlive()==true) and (ppPlayer.isBarbarian()==false) ):                        
							civ_type = gc.getPlayer(iPlayer).getCivilizationType()
							for iUnit in range(gc.getNumUnitClassInfos()):
								iUniqueUnit = gc.getCivilizationInfo(civ_type).getCivilizationUnits(iUnit);
								iDefaultUnit = gc.getUnitClassInfo(iUnit).getDefaultUnitIndex();
								if (iDefaultUnit > -1 and iUniqueUnit > -1 and iDefaultUnit != iUniqueUnit):                                
									if ( iUnitType == iDefaultUnit ):                                    
										l_vassalUB.append(iUniqueUnit)
				if ( len(l_vassalUB) >= 1 ):                
					self.iVassalUUChance = self.getRandomNumber( 4 )
					if self.iVassalUUChance == 0:
						chance = CyGame().getSorenRandNum(len(l_vassalUB), "Random for UB")
						iX = pUnit.getX()
						iY = pUnit.getY()
						pNewUnit = pPlayer.initUnit( l_vassalUB[chance], iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.NO_DIRECTION )
						pNewUnit.convert(pUnit)


## Topkapi Palace End ##

If anyone could tell me how memory-demanding this code is, that would be helpful. I am deciding if this wonder is worth having. Basically what it does is give you a chance to build a rival's UU rather than the unit in replaces. So if you were the Maya and were playing a game against the Byzantines, when you build a Knight there is an X% chance it will be a Cataphract.

The wonder is cool, but not game-breaking or that necessary, so I was just wondering if I should do away with it.
 
Short answer:
Probably not, it's far more likely that if the Flavian Ampitheatre is causing MAFs it's because of some graphics issue involved with it.

To test it I'd just take a save where you are having the MAF problems, and comment out that python code, and see if you still have the problem. If the problem persists you know it's not caused by the python code.


Long answer would take a while going into how memory works on a computer. This gets somewhat complex in terms of describing how an array of objects can be much larger then an integer variable, and all that, and plus while I know the basics, I'm not that knowledgeable about the subject to go into it in detail.
 
Normal python mods in civ use hardly any memory. It is possible to write bad python which uses a lot. For example, if the __init__ function declares a lot of variables with {} or [] syntax, those represent dictionaries and lists which are defined for the entire mod. If these are big, they could use memory. But most people who succeed at using large data structures like this are already programmers, and they realize that storing this type of data inside the sdk instead of python is much better.

So python mods generally use a few KB of data, not even MB, and not something that would cause a MAF on its own.
 
I agree with both answers. I briefly scanned the above code anyway and didn't see anything that would allocate anything significant, and it all will be released when the function exits. I suspect the graphics as well.
 
Back
Top Bottom