• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Stragen

Feedback - Benchmarking XML Reading

5 posts in this topic

Guys and Girls,

 

Looking for some feedback on a XML reader that i've thrown together, simply put, it reads the file in, verbose, and using vectors recreates the structure of the XML file into memory, which can then be searched...

 

I am using a COLLADA file as a benchmark, particularly the larger of the two files contained in the rar at this location: http://www.wazim.com/Downloads/AstroBoy_Walk.rar, for clarity purposes using astroBoy_walk_Max.DAE.

 

The file is 1.5mb decompressed, comprises of ~14000 lines of xml structured data.

 

Currently based on my basic benchmarking, i'm reading the file and creating the structure in, on average 9.5 seconds, i feel this is too slow especially if i plan to read in a number of these files... to be fair though this is a marked improvement over my first effort that took on average 45 seconds to read the same file.

int PopulateNode(std::string fC){
		//get line
		int lStart = 0, lEnd = 0, tagsOpen = 0;
		std::vector<XMLNODE*> nodeStack;
		nodeStack.push_back(this);
		while(((lStart = fC.find_first_not_of(" ", lStart)) != -1) && ((lEnd = fC.find("\n", lStart)) != -1)){
		//do{
			//lStart = fC.find_first_not_of(" ", lStart);
			//lEnd = fC.find("\n", lStart);
			std::string curLine;

			curLine = fC.substr(lStart,lEnd-lStart);
			
			if (curLine.at(0) != '<'){
				//no open tag found, but there is data on the line... approach as though elements of the node.
				nodeStack.back()->elements.push_back(XMLELEMENT("value",curLine.substr(lStart, lEnd - lStart- 1)));
			}else{
				switch (curLine.at(1)){
				case '/':
					{
						//close tag
						std::string closetag;
						nodeStack.pop_back();
						tagsOpen--;

						break;
					}
				case '?':
					{
						//question mark found
						std::string qnMark;

						break;
					}
				default:
					{
						//not closed/not question
						int bSBoundS = 0;
						int bSBoundE = curLine.find('>', bSBoundS);

						std::vector<std::string> tempStrVec = SubStrDelim(curLine.substr(bSBoundS+1, bSBoundE-bSBoundS-1),' ');

						std::string closeTag = std::string ("</").append(tempStrVec.at(0));
						std::string selfClose = "/>";

						if(!nodeStack.back()->nodeType.empty()){
							nodeStack.back()->childNodes.push_back(XMLNODE());
							nodeStack.push_back(&nodeStack.back()->childNodes.back());
						}

						nodeStack.back()->nodeType = tempStrVec.at(0);
						
						
						if (tempStrVec.size() > 1){
							for(int i=1; i<tempStrVec.size();++i){
								nodeStack.back()->elements.push_back(XMLELEMENT(tempStrVec.at(i)));
							}
						}


						int closeTagPos = curLine.find(closeTag);
						int selfCloseTag = curLine.find(selfClose);

						if(closeTagPos != -1){
							nodeStack.back()->elements.push_back(XMLELEMENT("value",curLine.substr(bSBoundE+1, closeTagPos-bSBoundE-1)));
						}

						if((closeTagPos == -1) && (selfCloseTag == -1)){
							//no close tag on line.
							nodeStack.back()->childNodes.push_back(XMLNODE());
							nodeStack.push_back(&nodeStack.back()->childNodes.back());
							tagsOpen++;
						}else{
							nodeStack.pop_back();
						}

						break;
					}
				}
			}
		
			lStart = lEnd +1;
		}//while(lStart < fC.size() && lEnd != -1);

		return 0;
	}

In this case the input is a Char* / std string from a full file read to memory.

 

I appreciate this might not be the most tidy, or error safe code, i'm working on getting it reading first then adding in some error trapping as i'm using the errors (if any are generated) to stop me in my tracks as i'm doing this rapid style.

 

To head off questions of "why not use a library out there?", i'm using this as a learning experience, and would like to learn ways of improving my own code over using someone elses...

 

So i'd like to ask for some C&C, pointers, or even just an idea on how long it takes others to read in this same file to see if my expectations are being unfair.

 

Thanks.

0

Share this post


Link to post
Share on other sites

I appreciate this might not be the most tidy, or error safe code, i'm working on getting it reading first then adding in some error trapping as i'm using the errors (if any are generated) to stop me in my tracks as i'm doing this rapid style.

You are approaching this backwards.

First make your implementation correct, and come up with test cases to ensure correctness. Then optimize, using the test cases to ensure that you don't compromise correctness.
0

Share this post


Link to post
Share on other sites

You are doing a lot of temporary memory allocation and copying during your parsing. This will rack up a lot in terms of degraded performance.

 

Strings, in particular, are expensive because they require a memory allocation and a copy anytime you use substr(), append() or when you push one into a vector. At one point you create a temporary vector of strings (tempStrVec) and transfer them into an 'elements' vector using push_back (which will allocate space and copy all the chars for each string).

 
Secondary to that, seeking through strings multiple times at every iteration using find() methods is going to ramp up the time complexity. Find is implemented as a loop, it has to search through character-by-character until it finds (or not) the characters you are looking for. So you have loops within loops, repeatedly re-scanning through the text.
 
For speedy file parsing you generally want to load the entire file into a character buffer in one go, then zip through that, only allocating and copying out what you intend to keep at the end, producing little to no temporary structures. You also either want to take it character-by-character (without looking ahead using searching), or do an initial pass over it to split it all up into tokens and then examine token-by-token. Char-by-char is usually faster but token-by-token can sometimes be easier to implement.
0

Share this post


Link to post
Share on other sites
@dmatter thanks for the feedback, i've taken it away and considered it over the last couple of months and have revisited my approach.

The use of tokens, over substr, appeared to have the most impact to the code that i had. Char-by-char was more time consuming to write and I found it more problematic for the generation of the strings etc, whereas using tokens allowed for reasonably quick translation from char* to string, char* to float etc.

Before changing tact completely (now using a version of TinyXML, i know i didnt want to use libraries but i think i learnt what i needed) i had the reader down from 9.5 seconds, to around 2, noting that this was pulling the entire file and generating a structure to use it, rather than polling up to the points that i actually need and discounted the rest.

The TinyXML implementation now pulls the file apart and gets what i need to survive in about 0.2 seconds depending on the machine.

Thanks for the advice.
0

Share this post


Link to post
Share on other sites
Writing a XML parser as a learning experience makes sense only if you want to become a XML *expert*, which I presume isn't your purpose. Your code has nothing to do with XML parsing: it tries to parse a specific file with extremely specific assumptions (e.g. tags at line start), with the effect of being dangerous to you (what if the file changes?) and counterproductive towards learning actual XML. So you should really, really use a library to load the COLLADA files you need in a robust way; if you want to do something related and useful, and acquire some modest but valid knowledge of XML, I suggest writing XML files instead (saved game configurations, tool output, etc.).
1

Share this post


Link to post
Share on other sites

Reading between the lines here a bit: Once you progress past the academic goals of writing your own parsers and 3d model loading you might like to look into using something like AssImp as a robust way to load in your mesh and animation resources.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0