hi , i would like to hear from you , what is your tips and solutions to combating iteration against frame drop or performance decrease.

for example , i am busy with a voxel based project , in c# on another engine ,however i am sure the principles i am going to describe ,also manifests and translates in to gamestudio .

i have a function that iterates over about +-3000 vertices ,to trace shadows ,in to my scene on top of chunks .

given that each chunk is updated and prepared for rendering 1 per frame ,which means that chunk updates are spread across multiple frames to keep frame rates up and performance and update time to an acceptable degree , i thought i would apply the same concept to the tracing function .

each chunk fire's off a coroutine ,which iterates through about 2000 vertices on avarage , at first i thought i would do one yield per iteration and see how
fast the iteration runs until completion , and see how much impact is made on frame rate , the code which iterates being to this effect :

Code:
int i=0;
void foo()
{
	foreach (item in vertices)
	{
		//...ray cast and vertex coloring
		i++;
		wait(1);//yield
	}
	update_vertex_coloring;
}




this gave me 1 yield per iteration, so that amounts to alot of frames before it becomes visible on screen , frame rate is not a problem but completion time is.

so next i decided to see how would frame rate be impacted if i ran the iteration to completion before yielding :

Code:
int i=0;
void foo()
{
	foreach (item in vertices)
	{
		//...ray cast and vertex coloring
		i++;
	}
	wait(1);
	update_vertex_coloring;
}



this means now , each chunk that fires off this function , iterates through i-amount of vertices to completion before yielding , which gives the result of this being visible faster , the time it takes before the result is visible on screen is less but the frame rate suffers , seeing that for example maby 8 chunks across 8 frames fire's off , each , this function , the amount of iteration per frame increases such that said 8 chunks takes alot more time to become visible, so nothing is really gained here.

next i decided that my solution would then be,to split total iteration ,in to percentages , and then yield after every given percentage has completed ,like this:

Code:
int i=0;
void foo()
{
	foreach (item in vertices)
	{
		//...ray cast and vertex coloring
		i++;
		if(i==vertex_count * 0.25f)
		{
			wait(1);
		}
		if(i==vertex_count * 0.50f)
		{
			wait(1);
		}
		if(i==vertex_count * 0.75f)
		{
			wait(1);
		}
		if(i==vertex_count * 0.90f)
		{
			wait(1);
		}
		if(i==vertex_count * 1.0f)
		{
			wait(1);
		}
}
	update_vertex_coloring;
}



this gave me what i was looking for , i get an impact in frame rate/performance which is acceptable , and i get a total iteration completion speed which is acceptable too .

i then found that the following gives me the best solution , because here i can complete up to 90% of the iteration split up in to percentage and yield a few times, then switch over to 1 iterationper yield, which means if i had about 8 chunks firing off this function , that at first i would favour completion time above a slight frame drop up to 90% completion then each of these 8 functions running asyncrounesly(??) would then favour frame rate above completion time :

Code:
int i=0;
void foo()
{
	foreach (item in vertices)
	{
		//...ray cast and vertex coloring
		i++;
		if(i==vertex_count * 0.25f)
		{
			wait(1);
		}
		if(i==vertex_count * 0.50f)
		{
			wait(1);
		}
		if(i==vertex_count * 0.75f)
		{
			wait(1);
		}
		if(i>vertex_count * 0.90f)
		{
			wait(1);
		}
}
	update_vertex_coloring;
}



i know there are better methods like space partitioning and reducing vertices , but its iteration itself that sticks out to me,seeing that splitting up across multiple frames is so important for performance , in games , and that iteration is such a killer , i would like to hear the teqniques and solutions you favour to combat frame rate/performance loses ,for iteration,and balancing them together.

thanks.

Last edited by Wjbender; 01/01/16 21:33. Reason: corrections made

Compulsive compiler