Load some tables files only when needed

Wrote new code? Fixed a bug? Want to discuss technical stuff? Feel free to post it here.

Moderator: Moderators

iMikeLance
Moderators
Moderators
Posts: 208
Joined: 01 Feb 2010, 17:37
Noob?: No
Location: Brazil - MG

Load some tables files only when needed

#1 Post by iMikeLance »

We can load some txt files like quests, skills and items descriptions only when we need them. I've been using this code for some time, and it should be rewritten as this is just a suggestion. Using this I'm saving about 3mb of RAM.

Code: Select all

Index: AI/CoreLogic.pm
===================================================================
--- AI/CoreLogic.pm	(revision 8125)
+++ AI/CoreLogic.pm	(working copy)
@@ -999,6 +999,13 @@
 	####### AUTO MAKE ARROW #######
 	if ((AI::isIdle || AI::is(qw/route move autoBuy storageAuto follow sitAuto items_take items_gather/))
 	 && timeOut($AI::Timeouts::autoArrow, 0.2) && $config{autoMakeArrows} && defined binFind(\@skillsID, 'AC_MAKINGARROW') ) {
+	 
+	#$L load config file on demand
+	if (!%arrowcraft_items) {
+		Settings::loadByHandle(Settings::addControlFile('arrowcraft.txt',loader => [\&parseDataFile_lc, \%arrowcraft_items]));
+		message TF("Loading %s...\n", 'arrowcraft.txt');
+	}	
+	
 		my $max = @arrowCraftID;
 		my $nMake = 0;
 		for (my $i = 0; $i < $max; $i++) {
Index: Commands.pm
===================================================================
--- Commands.pm	(revision 8125)
+++ Commands.pm	(working copy)
@@ -2079,6 +2079,11 @@
 				error TF("Error in function '%s skills desc' (Skill Description)\n" .
 					"Skill %s does not exist.\n", $cmd, $args[2]);
 			} else {
+				#$L load table file on demand
+				if (!%skillsDesc_lut) {
+					message TF("Loading %s...\n", 'skillsdescriptions.txt');
+					Settings::loadByHandle(Settings::addTableFile('skillsdescriptions.txt',loader => [\&parseRODescLUT, \%skillsDesc_lut], mustExist => 1));
+				}
 				my $description = $skillsDesc_lut{$skill->getHandle()} || T("Error: No description available.\n");
 				message TF("===============Skill Description===============\n" .
 					"Skill: %s\n\n", $skill->getName()), "info";
@@ -2591,6 +2596,12 @@
 	my @commands_req = split(/ +/, $args);
 	my @unknown;
 	my @found;
+	
+	#$L load table file on demand
+	if (!%descriptions) {
+		message TF("Loading %s...\n", 'commanddescriptions.txt');
+		Settings::loadByHandle(Settings::addTableFile('commanddescriptions.txt', loader => [\&parseCommandsDescription, \%descriptions], mustExist => 0));
+	}
 
 	my @commands = (@commands_req)? @commands_req : (sort keys %descriptions);
 
@@ -4039,6 +4050,12 @@
 			error TF("Error in function 'skills desc' (Skill Description)\n" .
 				"Skill %s does not exist.\n", $arg2);
 		} else {
+			#$L load table file on demand
+			if (!%skillsDesc_lut) {
+				message TF("Loading %s...\n", 'skillsdescriptions.txt');
+				Settings::loadByHandle(Settings::addTableFile('skillsdescriptions.txt',loader => [\&parseRODescLUT, \%skillsDesc_lut], mustExist => 1));
+			}
+			
 			my $description = $skillsDesc_lut{$skill->getHandle()} || T("Error: No description available.\n");
 			message TF("===============Skill Description===============\n" .
 				"Skill: %s\n\n", $skill->getName()), "info";
@@ -5481,6 +5498,11 @@
 	} elsif ($args[0] eq 'info') {
 		if ($args[1] =~ /^\d+/) {
 			# note: we need the questID here now, might be better if we could make it so you only have to insert some questIndex
+			#$L load table file on demand
+			if (!%quests_lut) {
+				message TF("Loading %s...\n", 'quests.txt');
+				Settings::loadByHandle(Settings::addTableFile('quests.txt', loader => [\&parseROQuestsLUT, \%quests_lut], mustExist => 1));
+			}
 			if ($quests_lut{$args[1]}) {
 				my $msg = center (' ' . ($quests_lut{$args[1]}{title} || T('Quest Info')) . ' ', 79, '-') . "\n";
 				$msg .= TF("%s\n", $quests_lut{$args[1]}{summary}) if $quests_lut{$args[1]}{summary};
Index: functions.pl
===================================================================
--- functions.pl	(revision 8125)
+++ functions.pl	(working copy)
@@ -146,10 +146,11 @@
 		loader => [\&parseItemsControl, \%items_control],
 		internalName => 'items_control.txt',
 		autoSearch => 0);
-	Settings::addControlFile(Settings::getShopFilename(),
-		loader => [\&parseShopControl, \%shop],
-		internalName => 'shop.txt',
-		autoSearch => 0);
+	#$L removed as we'll load config files on demand. check Misc::makeShop
+		# Settings::addControlFile(Settings::getShopFilename(),
+			# loader => [\&parseShopControl, \%shop],
+			# internalName => 'shop.txt',
+			# autoSearch => 0);
 	Settings::addControlFile('overallAuth.txt',
 		loader => [\&parseDataFile, \%overallAuth]);
 	Settings::addControlFile('pickupitems.txt',
@@ -168,8 +169,9 @@
 		loader => [\&parseSectionedFile, \%consoleColors]);
 	Settings::addControlFile('routeweights.txt',
 		loader => [\&parseDataFile, \%routeWeights]);
-	Settings::addControlFile('arrowcraft.txt',
-		loader => [\&parseDataFile_lc, \%arrowcraft_items]);
+	#$L removed as we'll load config files on demand. check CoreLogic::processAutoMakeArrow
+		# Settings::addControlFile('arrowcraft.txt',
+			# loader => [\&parseDataFile_lc, \%arrowcraft_items]);
 
 	# Loading of Table files
 	# Load Servers.txt first
@@ -199,8 +201,9 @@
 	# Load all other tables
 	Settings::addTableFile('cities.txt',
 		loader => [\&parseROLUT, \%cities_lut]);
-	Settings::addTableFile('commanddescriptions.txt',
-		loader => [\&parseCommandsDescription, \%descriptions], mustExist => 0);
+	#$L removed as we'll load table files on demand. check Commands::cmdHelp 
+		# Settings::addTableFile('commanddescriptions.txt',
+			# loader => [\&parseCommandsDescription, \%descriptions], mustExist => 0);
 	Settings::addTableFile('directions.txt',
 		loader => [\&parseDataFile2, \%directions_lut]);
 	Settings::addTableFile('elements.txt',
@@ -215,8 +218,9 @@
 		loader => [\&parseArrayFile, \@headgears_lut]);
 	Settings::addTableFile('items.txt',
 		loader => [\&parseROLUT, \%items_lut]);
-	Settings::addTableFile('itemsdescriptions.txt',
-		loader => [\&parseRODescLUT, \%itemsDesc_lut], mustExist => 0);
+	#$L removed as we'll load table files on demand. check Misc::printItemDesc
+		# Settings::addTableFile('itemsdescriptions.txt',
+			# loader => [\&parseRODescLUT, \%itemsDesc_lut], mustExist => 0);
 	Settings::addTableFile('itemslots.txt',
 		loader => [\&parseROSlotsLUT, \%itemSlots_lut]);
 	Settings::addTableFile('itemslotcounttable.txt',
@@ -245,8 +249,9 @@
 		loader => \&Skill::StaticInfo::parseSkillsDatabase_handle2name, mustExist => 0);
 	Settings::addTableFile('spells.txt',
 		loader => [\&parseDataFile2, \%spells_lut]);
-	Settings::addTableFile('skillsdescriptions.txt',
-		loader => [\&parseRODescLUT, \%skillsDesc_lut], mustExist => 0);
+	#$L removed as we'll load table files on demand. check Commands::cmdSkills
+		# Settings::addTableFile('skillsdescriptions.txt',
+			# loader => [\&parseRODescLUT, \%skillsDesc_lut], mustExist => 0);
 	Settings::addTableFile('skillssp.txt',
 		loader => \&Skill::StaticInfo::parseSPDatabase);
 	Settings::addTableFile('STATUS_id_handle.txt', loader => [\&parseDataFile2, \%statusHandle]);
@@ -259,7 +264,8 @@
 	Settings::addTableFile('statusnametable.txt', loader => [\&parseDataFile2, \%statusName], mustExist => 0);
 	Settings::addTableFile('skillsarea.txt', loader => [\&parseDataFile2, \%skillsArea]);
 	Settings::addTableFile('skillsencore.txt', loader => [\&parseList, \%skillsEncore]);
-	Settings::addTableFile('quests.txt', loader => [\&parseROQuestsLUT, \%quests_lut], mustExist => 0);
+	#$L removed as we'll load table files on demand. check Commands::cmdQuest
+		#Settings::addTableFile('quests.txt', loader => [\&parseROQuestsLUT, \%quests_lut], mustExist => 0);
 	Settings::addTableFile('effects.txt', loader => [\&parseDataFile2, \%effectName], mustExist => 0);
 
 	use encoding 'utf8';
Index: Misc.pm
===================================================================
--- Misc.pm	(revision 8125)
+++ Misc.pm	(working copy)
@@ -2107,6 +2103,11 @@
 #
 # Print the description for $itemID.
 sub printItemDesc {
+	#$L load table file on demand
+	if (!%itemsDesc_lut) {
+		message TF("Loading %s...\n", 'itemsdescriptions.txt');
+		Settings::loadByHandle(Settings::addTableFile('itemsdescriptions.txt', loader => [\&FileParsers::parseRODescLUT, \%itemsDesc_lut]));
+	}
 	my $itemID = shift;
 	my $itemName = itemNameSimple($itemID);
 	my $description = $itemsDesc_lut{$itemID} || T("Error: No description available.\n");
@@ -4180,6 +4181,17 @@
 		error T("You need this with a cart in order to create a shop!\n");
 		return;
 	}
+	
+	#$L load config file on demand
+	if (!%shop) {
+		Settings::loadByHandle(
+			Settings::addControlFile(
+				Settings::getShopFilename(),  loader => [\&parseShopControl, \%shop], 
+					internalName => 'shop.txt', autoSearch => 0)
+				);
+		
+		message TF("Loading %s...\n", Settings::getShopFilename());
+	}
 
 	if (!$shop{title_line}) {
 		error T("Your shop does not have a title.\n");
EternalHarvest
Developers
Developers
Posts: 1798
Joined: 05 Dec 2008, 05:42
Noob?: Yes

Re: Load some tables files only when needed

#2 Post by EternalHarvest »

Yea, for real implementation of this an option in Settings::addTableFile would be nice.

There also is alternative way of tying %skillsDesc_lut etc to some database, for example, sqlite. It probably would work faster.
User avatar
kLabMouse
Administrator
Administrator
Posts: 1301
Joined: 24 Apr 2008, 12:02

Re: Load some tables files only when needed

#3 Post by kLabMouse »

EternalHarvest wrote:Yea, for real implementation of this an option in Settings::addTableFile would be nice.

There also is alternative way of tying %skillsDesc_lut etc to some database, for example, sqlite. It probably would work faster.
Hmm. there is also one more method.
to "tie" thus hash tables. so if they are not loaded, they will get auto loaded.
EternalHarvest
Developers
Developers
Posts: 1798
Joined: 05 Dec 2008, 05:42
Noob?: Yes

Re: Load some tables files only when needed

#4 Post by EternalHarvest »

That's how it may be implemented, I've left out details.