Skip to main content
gamedev.net
🔒 Locked

Do You like optimization ?

Started by Woodchuck Dec 21, 2004 at 5:08 PM 12 replies 2.4k views
Original Post
Woodchuck
Woodchuck
Hello, so i'm not very happy because i've got a big problem. I am writing a pvs compiler and it happen a very strange thing. It is difficult for me to explain very well because my english is poor. So i will try. In the hurt of the program, inside many loop, and inside only 3 or 4 called functions, some instruction like function call cost an abnormal time of processing. The program is fast and become abnormaly very slow. It's all. This program use (only) something like 50mo of physics memory. What is strange is that a division for exemple become less expensive than a ++. Use a division don't affect the speed of the program, but an incrementing make it very slow. I really never seen that, the program speed is decrease of a factor 100 when i add just a little incrementing, but if i add an cross product, nothing to say the program is fast. It seems that my program is suffer of a stack or memory problem. Call a function (inline works well naturally), use a large class (i use only structures), use a table like that table (but pointer on it and pointer[0] works fine), all this thing cause an abnormal time to be compute. This is like there is a limit or i dont know what, and when i add an instruction, all become very slow. Ok, if somebody have seen that before and solve it, please say it to me. i use MSV.net 2003 in release configuration with maximize speed optimization. Thank you very much
Woodchuck
Woodchuck
I really, really think about a compiler bug... really odd

imagine : i've got a code and at one moment, if i write for exemple :

poly.nb_vertices ++;

the program become very slow. The nb_vertices is not initialized.
but if i write :

poly.nb_vertices = 0;
poly.nb_vertices ++;
poly.nb_vertices ++;
poly.nb_vertices ++;
poly.nb_vertices ++;
poly.nb_vertices ++;
poly.nb_vertices ++;
poly.nb_vertices ++;

the program is very fast ... really incredible !


i want to add that i'm not a beginner, its not a "dumb" bug, i'm sure.

i will try my code on an other compiler ....

if you have suggestions or i dont know, just a little idea, its welcome.

Thank you
Woodchuck
Woodchuck
Or maybe, maybe a memory overflow ! yes sometimes memory overflow can cause very strange bug , like that...

So sorry, this post was not very interresting but ... dont know , i was thinking that somebody might know something particular on .net 2003 and optimization or on stack..

Merry Chrismas !
Toonkides
Toonkides
I will try and test your code with vc++ 6.0
Toonkides
Toonkides
#include <windows.h>#include <cstdio>struct struc{	unsigned int vertices ;	unsigned int uselessVar ;} ;int main(){	unsigned int i ;	unsigned int curTime = timeGetTime() ;	unsigned int lastTime ;	struc thisVar ;	thisVar.vertices = 0 ;	for(i = 0; i < 1000000000; i++)		thisVar.vertices++ ;	lastTime = timeGetTime() ;	printf("%d milliseconds elapsed\n", lastTime - curTime) ;	return 0 ;}


Can you show me any code ?
This test was as useless as useless can be. With or without the initialization or thisVar.vertices the millseconds elapsed was always 2400, which leads me to conclude there is some sort of memory problem.

Perhaps you're using too much main memory and it's accessing your page file ?
Where do you allocate memory ?
Alura
Alura
Quote:

Can you show me any code ?
This test was as useless as useless can be. With or without the initialization or thisVar.vertices the millseconds elapsed was always 2400, which leads me to conclude there is some sort of memory problem.


Most likely, the compiler is just taking out this part:

Quote:

for(i = 0; i < 1000000000; i++)
thisVar.vertices++ ;


Since the results of that data ain't used for anything, the compiler more than likely just takes that for loop out altogether.

Wish I could help the OP but that sounds really weird.
Endurion
Endurion
I'd take my guess at The nb_vertices is not initialized.

You should initialize it. If nb_vertices is used for a loop and it's not initialized it may be some random (high) value, which can introduce heavy slowdowns (imagine running a loop 3452816845 times instead 100).
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Woodchuck
Woodchuck
ok here is the code :

void M_DCP_DetPVS(s_dcp_tree &dcp_tree, s_tab_strip &strips, s_tab_poly3D &zone_polys, s_dcp_node &node_d, unsigned int start_strip_node_s, unsigned int nb_strips_node_s){	int f,g;	int i,j,k,l;	plan *nodes_planes;	unsigned int nb_nodes_planes;	s_strip *tab_strips;	unsigned int nb_strips;		s_poly3D ret_poly1,ret_poly2;	D3DXVECTOR3 bounding_box_node[8];	plan P;	float D;	unsigned int nb_vertex,nb_portails;	D3DXVECTOR3 w,triangle[3];	int cpt;	nodes_planes = new plan[M_DCP_MAX_NODES_PLANE];	tab_strips = new s_strip[M_DCP_MAX_STRIPS];		portails.nb_polys = 0;	ret_poly1.vertices = new s_poly3D_vertex[M_POLY3D_MAX_VERTICES];	ret_poly2.vertices = new s_poly3D_vertex[M_POLY3D_MAX_VERTICES];	M_DCP_GetBoundingBox(node_d,bounding_box_node);	nb_vertex = 0;	for(f = 0 ; f < zone_polys.nb_polys ; f++)	{		nb_vertex += zone_polys.polys[f].nb_vertices;	}	M_DCP_Get2NodesPlanes(zone_polys,node_d,nodes_planes,nb_nodes_planes);	M_DCP_Get2NodesStrips(tab_strips,nb_strips,nodes_planes,nb_nodes_planes,dcp_tree,strips,node_d,start_strip_node_s,nb_strips_node_s);	delete [] nodes_planes;	for(f = 0 ; f < nb_strips ; f++)	{		if(portails.polys[f].vertices == NULL)		{			portails.polys[f].vertices = new s_poly3D_vertex[M_POLY3D_MAX_VERTICES];		}		M_POLY3D_DetBigPoly3D(tab_strips[f].plane,strips.vertex_buffer[tab_strips[f].start_vertex].coord_pos,portails.polys[f]);		portails.nb_polys ++;	}	for(f = 0 ; f < nb_strips ; f++)	{		for(i = tab_strips[f].start_vertex ; i < tab_strips[f].start_vertex + tab_strips[f].nb_vertices - 2 ; i++)		{			triangle[0] = strips.vertex_buffer[i+0].coord_pos;			triangle[1] = strips.vertex_buffer[i+1].coord_pos;			triangle[2] = strips.vertex_buffer[i+2].coord_pos;			for(l = 0 ; l < 3 ; l++)			{				for(j = 0 ; j < 8 ; j++)				{					D3DXVec3Cross(&P.normal,&(bounding_box_node[j]-triangle[l]),&(bounding_box_node[j]-triangle[(l+1)%3]));					D = sqrt(P.normal.x*P.normal.x + P.normal.y*P.normal.y + P.normal.z*P.normal.z);					P.normal.x /= D;					P.normal.y /= D;					P.normal.z /= D;					P.D = -D3DXVec3Dot(&bounding_box_node[j],&P.normal);					cpt = 0;					for(k = 0 ; k < 8 ; k++)					{						if(k != j)						{							D = D3DXVec3Dot(&P.normal,&bounding_box_node[k]) + P.D;							if(D >= 0.f)							{								cpt++;							}						}					}					for(k = 0 ; k < 3 ; k++)					{						if(k != l && k != (l+1)%3)						{							D = D3DXVec3Dot(&P.normal,▵[k]) + P.D;							if(D >= 0.f)							{								cpt++;							}						}					}					if(cpt == 0 || cpt == 8+3-3)					{						nb_portails = portails.nb_polys;												for(g = 0 ; g < nb_portails ; g++)						{							if(portails.polys[g].nb_vertices >= 3)							{								//ret_poly1.nb_vertices = 0;								ret_poly1.nb_vertices ++;								//ret_poly2.nb_vertices = 0;								//M_POLY3D_ClipPoly3D2(P,portails.polys[g],ret_poly1,ret_poly2);																/*if(ret_poly1.nb_vertices >= 3)								{									portails.polys[g] = ret_poly1;								}								if(l != 2)								{									if(ret_poly2.nb_vertices >= 3)									{										if(portails.polys[portails.nb_polys].vertices == NULL)										{											portails.polys[portails.nb_polys].vertices = new s_poly3D_vertex[M_POLY3D_MAX_VERTICES];										}										portails.polys[portails.nb_polys] = ret_poly2;										portails.nb_polys ++;									}								}*/							}						}					}				}			}		}	}	delete [] tab_strips;	delete [] ret_poly1.vertices;	delete [] ret_poly2.vertices;}


you can see that ret_poly1 are not use anywhere and yet the ret_poly1.nb_vertices++ instruction slow down the program. But really, i'm sure, when the ret_poly1.vertices = 0 instruction is added, the program is normaly "fast".
Endurion, i think you are right, i think that ... no no in fact , the ret_poly1.nb_vertices is just an exemple, i have many "ridiculous" examples like this : the M_POLY3D_ClipPoly3D2(...) is an empty function ! and all the parameter are references, and this function slow down the program ... a function call slow down the program but a cross product do not (i do the test too).

memories are almost entirely dynamic. All table are allocated with new. Maybe i use to much main memory (near 128 mo in fact) ... what is page file ?

i really don't understand what's happen.

Thanks for help.
xsirxx
xsirxx
I have the same problem right now. I fixed this in VC++ by switching to Release mode and turning on optimizations. I jumped from 63fps to 600fps. This was while I was using Vectors, or hell anything from the std.

My problem is Ive upgraded to .NET and my problem is back and I cant figure how to optimize the compiler. Even though I have set all optimizations on. Im almost positive its your compiler not optimizing. Try that.

Brad


EDIT: you have this...

struct struc
{
unsigned int vertices ;
unsigned int uselessVar ;
} ;



And your incrementing an unsigned int? Should vertices be a pointer? Im not sure from the code what your incrementing....memory?
--X
Woodchuck
Woodchuck
Thx brad. I ve do the same, i turn on all the optimizations, i tried all the possibilities yesterday :\ whithout success.

ok , you seems to say that VC6 can do better, i will try now ...
Woodchuck
Woodchuck
yes i ve got :

struct s_poly3D{   s_vertex *vertices;   unsigned int nb_vertices;   an operator=  but i eliminate it and that do the same};
Woodchuck
Woodchuck
I try with VC6 and ... all cases are slow. then i deduced that maybe .net 2003 do optimization , some big optimization i dont control. my code here is useless then maybe .net2003 remove instruction that do nothin in out ...
all of my test seems to indicate that there is not 2 cases in VC6, but one that give a slow program.
pfff what a story, i understand nothin ....

very nice chrismas decoration hehe :)

thx all.
xsirxx
xsirxx
Ok, I was making sure that you were incrementing memory locations, because in your first post it seemed you were incrementing the int as in i = i + 1;. So no luck with VC6.0? and no luck with .NET? In your release build you went through all the different settings with no luck? I use Glow Code to profile, im still in my 30 days, but they let ya use it for that long as a trial. I would recomend using that, unless you have a better profiler. And the slow down definitly appears here and other areas that use the same style? Try doing memcpys to see if it eleviants the problem?

EDIT: also another person had a good idea on initializing, if you place it full of dummy info and make sure you dont go over the bounds, do you get the same result? This will atleast allocate enough for it... or what if you dont create with "new" maybe there is a difference from the stack and heap?
--X
Woodchuck
Woodchuck
Thanks for reply, i will try with Glow Code.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.