Scene object instantiation performance

Hi,
we were (actually, still kinda are) having problem with the performance of instantiating scene objects (think producing neutral units in a strategy game). The whole problem lies in Resources.FindObjectsOfTypeAll which is VERY VERY slow. I really think there should be more clever way of looking up things. More complex games will have problem with this.

We solved our problems for now by instantiating in batches rather than everything at once and changing the ID allocation part to the following "hack" (not yet tested!).
private const int STARTING_INDEX = 1;
private static int lastIndex = STARTING_INDEX;
private static bool traverseSearch = false;

private static PhotonViewID AllocateSceneViewID()
{
	for (int i = MAX_VIEW_IDS - lastIndex; i >= STARTING_INDEX; i--)
	{
		if (IsSceneViewIDFree(i))
		{
			lastIndex = i;
			
			if (traverseSearch) 
			{
				traverseSearch = false;
			}

			break;
		}

		if (lastIndex == -1 && !traverseSearch)
		{
			traverseSearch = true;
			i = MAX_VIEW_IDS - STARTING_INDEX;
		}
	}

	if (lastIndex == -1)
	{
		Debug.LogError("You ran out of scene view ID's!");
	}

	PhotonViewID viewID = new PhotonViewID(lastIndex, null);

	return viewID;
}

Generally, I think it would be useful if the algorithm for finding new IDs would be a bit more sophisticated as calling Resources.FindObjectsOfTypeAll can be VERY costly, especially with more complex games. I wouldn't personally mind if we could get rid of this call in general ;]. Any idea how to go around this?

Comments

  • Good point. Yes that's relatively weak. The FindObjectsOfTypeAll is done more than once and that can be avoided easily.
    I'll take a look when I can and am open for suggestions.

    Aside from that: Mass-instantiation is not going to be effective in any case. It takes up bandwidth for things that could be loaded as scene setup most likely.
  • Tobias wrote:
    Good point. Yes that's relatively weak. The FindObjectsOfTypeAll is done more than once and that can be avoided easily.
    I'll take a look when I can and am open for suggestions.

    Our instantiation performance is currently ok so it's not in our focus anymore. I didn't go through the whole PUN code but wouldn't it be better to save the instantiated PhotonView IDs in some simple structure (list, array...) and then check against that rather than lookup the IDs in resources? However as I said, I'm not 100% familiar with the inner workings of PUN yet. Going there :].
    Tobias wrote:
    Aside from that: Mass-instantiation is not going to be effective in any case. It takes up bandwidth for things that could be loaded as scene setup most likely.

    Yeah, we know, hence the batching.
  • Ok. I consider that solved :)
  • Ok, this fix breaks when we have scene objects in the scene from the start of the game (without Instantiation via code).

    In this case, batching is not enough to keep good performance, we also need to get rid of the Resource lookup.
  • Your fix or mine?
    I think I sent you a mail with an updated class.
Sign In or Register to comment.