recastnavigation / recastnavigation

Industry-standard navigation-mesh toolset for games

Home Page:http://recastnav.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Small optimizations for CalculateDistanceField() in RecastRegion.cpp

minhkhoi0975 opened this issue · comments

In this part of the function:

for (int y = 0; y < h; ++y)
{
	for (int x = 0; x < w; ++x)
	{
		const rcCompactCell& c = chf.cells[x+y*w];
		for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
		{
			const rcCompactSpan& s = chf.spans[i];
			const unsigned char area = chf.areas[i];
			
			int nc = 0;
			for (int dir = 0; dir < 4; ++dir)
			{
				if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
				{
					const int ax = x + rcGetDirOffsetX(dir);
					const int ay = y + rcGetDirOffsetY(dir);
					const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
					if (area == chf.areas[ai])
						nc++;
				}
			}
			if (nc != 4)
				src[i] = 0;
		}
	}
}

I think that nc should be a char instead of an int since its value is between 0 and 3. I also think that if one side of the span doesn't have a neighbor, the for loop should break immediately instead of checking the remaining directions.

Thanks!

I think we could certainly early-out of the loop if one direction is not connected. As for the char data type, it likely doesn't matter and will be padded to at least 32 bits anyway, so I'd want to profile that before making that change.

If you're interested in submitting a PR for early-outing the loop, it'd be appreciated!