Archived

This topic is now archived and is closed to further replies.

bzroom

File loading, slowing wayyyy down.

Recommended Posts

bzroom    647
For some reason loading this same file 8 times is a big problem. I am loading it 8 times as I only have one test model and 8 places where i need it. I decided it have it print out how long it took just to see why my game never showed up. Heres the output: model loaded: data//test.mat in 0.344s. model loaded: data//test.mat in 17.953s. model loaded: data//test.mat in 28.203s. Then i get tired of waiting... After a few runs of this my computer is noticibly slower, maybe a super huge memory leak? While the files are being loaded the processor is usualy around 40% but the hdd light is solidly lit. I''m stumped. Here is my model loading code:
bool cModel::Load(char *filename)
{
	long start =GetTickCount();
	ifstream myFile (filename, ios::in | ios::binary);

	if (myFile.fail())
		return false;

	long i, j;
	long temp = 0;

	myFile.read((char *)&vertcount, sizeof(long));
	myFile.read((char *)&tricount, sizeof(long));
	myFile.read((char *)&bonecount, sizeof(long));

	verts = new sVertex[vertcount];
	indices = new unsigned short[tricount*3];
	bones = new sBone[bonecount];

	//vertices

	for( i = 0; i < vertcount; i ++)
	{
		myFile.read((char *)verts[i].xyz, sizeof(float)*5+1);  //include bone and uv

		//myFile.read((char *)&verts[i].bone, 1);

		//myFile.read((char *)verts[i].uv, sizeof(float)*2);

	}

	//indices

	myFile.read((char *)indices, sizeof(unsigned short)*tricount*3);

	//bones

	for (i = 0; i < bonecount; i ++)
	{
		myFile.read((char *)bones[i].name, 32+sizeof(int)*2+sizeof(float)*6);
		//myFile.read((char *)bones[i].pos, sizeof(float)*3);

		//myFile.read((char *)bones[i].rot, sizeof(float)*3);

		//myFile.read((char *)&bones[i].numpframes, sizeof(int));

		//myFile.read((char *)&bones[i].numrframes, sizeof(int));


		bones[i].pframes = new sFrame[bones[i].numpframes];
		bones[i].rframes = new sFrame[bones[i].numrframes];

		for (j = 0; j < bones[i].numpframes; j++)
		{
			myFile.read((char *)&bones[i].pframes[j].time, sizeof(float)*4);
			//myFile.read((char *)bones[i].pframes[j].vals, sizeof(float)*3);

		}
		for (j = 0; j < bones[i].numrframes; j++)
		{
			myFile.read((char *)&bones[i].rframes[j].time, sizeof(float)*4);
			//myFile.read((char *)bones[i].rframes[j].vals, sizeof(float)*3);

		}
	}


	cout<<"model loaded: "<<filename<<" in "<<(GetTickCount()-start)/1000.0f<<"s."<<endl;
	myFile.close();

	return true;
}

;

Share this post


Link to post
Share on other sites
bzroom    647
I should have mentioned that this is version 2, version 1 worked perfectly. Vertices consisted only of 3 floats. No bones, just vertices and indices. I thought maybe that increase of read calls was the problem but look at the first load time. It almost instant. Why would it slow down? It just dosnt makes sense.

Share this post


Link to post
Share on other sites
Oluseyi    2112
quote:
Original post by honayboyz
For some reason loading this same file 8 times is a big problem. I am loading it 8 times as I only have one test model and 8 places where i need it.
Load it into a buffer and simply copy that buffer that is already in memory, or use a resource management technique!

In any case, your cModel::Load method is leaking memory. You never delete [] verts, indices and bones.

Share this post


Link to post
Share on other sites
ggs    166
Also reading only a few bytes from a file at a time is slow. Buffer it, and it goes much much faster, or use memory mapped files and let windows do the buffering.

Share this post


Link to post
Share on other sites
bzroom    647
I delete them when the program closes.

cModel::~cModel()
{
delete[] verts;
delete[] indices;
delete[] bones;

vertcount = 0;
tricount = 0;
}

and the bones struct has a deconstructor which delets the frames.

I dont want to just copy the model to the other 8 because eventualy it will be 8 differnt models.

edit: yea its slow, but has anyone looked at the loading times? this function takes les then half a second the first time.

[edited by - honayboyz on January 25, 2004 10:23:24 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Most likely its not in the load code. From looking it over, there isn''t anything which would indicate an error. If it performs memory clean up, as you''ve said. Get some moniters out and see what is happening behind the scenes. If your working on Win2K or above, check the task manager, and moniter the memory usage, read/writes, and paging behavior of the application.

You can also go into further depth, using the performance moniter which is found in admin tools in win2k not sure where its on other systems.

Also make sure to check any error with uninitialzied variables.

Good Luck!

-ddn

Share this post


Link to post
Share on other sites
bzroom    647
Whoops, forgot to load one more long before all the data. Since all the data would be shifted by a long, i bet it was trying to alocated like bones.postion.x number of frames or something. Its all fixed now thanks.

Share this post


Link to post
Share on other sites
bastardos    100

Loading a file more than once is teh evul. Don''t do it.

At least it gets in the face of this asm freak who''ll sell his kidney to shave a clock off the initialization routine. >_< (I know I''m sick.)

.bas

[sPiKie] mmorpg isnt hard in vb

Share this post


Link to post
Share on other sites
quote:
Original post by honayboyz
myFile.read((char *)verts[i].xyz, sizeof(float)*5+1); //include bone and uv
//myFile.read((char *)&verts[i].bone, 1);
//myFile.read((char *)verts[i].uv, sizeof(float)*2);

Is that an error? I'm guessing bone and uv follow xyz so you simply read the structure in one go? If xyz is an array then that code should work, but it's really bad code. Better is this:

myFile.read( (char *)&verts[i], sizeof sVertex );

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

[edited by - CGameProgrammer on January 26, 2004 7:23:06 AM]

Share this post


Link to post
Share on other sites