[FreedomAI] Week 5 - Pooling Technology

This optimization method is borrowed from other fields. One of the things I first understood is the thread pool and the memory pool.

In fact, both thread pools and memory pools are very common in game engines. First, let's introduce their application scenarios, because we often encounter such a problem in game engines, that is, the application and release of memory (threads). Very frequently, although the number or size of this thread or memory application is not large, whether it is the application and release of memory or the application and release of threads, it will cause the operating system to remain between user mode and kernel mode. will switch, which is a time-consuming operation.

So the predecessors thought of a way to exchange space for time, that is, you can pre-apply for a large block of memory and a large number of threads in advance, and then use a management system to store them without waking up, so when the user applies for memory or When threading, it is not an application to the operating system, but an application to our own management system. Often this is a time to move the pointer - although this method actually uses up a lot of standby space.

When we were doing AI, we also faced this problem. There are some AIs that died and spawned very frequently, such as the self-destruction soldiers summoned by the Boss, but the number of these things may still be large. However, Instantiate and Destroy are two very time-consuming operations, so we also use the AI ​​pool technology.

public class AIPoorComponent:UComponent
	{
		public GameObject mAI_Template;
		public AIEntity [] mAIPoor = new AIEntity [3000];
		public int mMaxCount = 3000;
		private int mTempCount = 0;


		public override void Init ()
		{
			for (int i = 0; i < mMaxCount; i++)
			{
				mAIPoor [i] .mAI = mAI_Template;
				mAIPoor [i].Init ();
				mAIPoor [i].GetComponent<BaseAIComponent> ().mPoorID = i;
			}
		}

		public override void Release ()
		{
			for (int i = 0; i < mMaxCount; i++)
			{
				mAIPoor [i].Release ();
			}
		}



		public void DestroyEntity(int id)
		{
			mUEntity.mWorld.deleteEntity (mAIPoor[id]);
			mAIPoor [id].GetComponent<BaseAIComponent> ().mAIRT.SetActive (false);
			AIEntity temp = mAIPoor[id];
			mAIPoor[id] = mAIPoor[mTempCount-1];
			mAIPoor [mTempCount - 1] = temp;
			mAIPoor [id].GetComponent<BaseAIComponent> ().mPoorID = id;
			mAIPoor [mTempCount - 1].GetComponent<BaseAIComponent> ().mPoorID = mTempCount - 1;
			mTempCount--;
		}



		public AIEntity InstantiateEntity()
		{
			mAIPoor [mTempCount] .mAllBitBunch.SetCount ((int) mUEntity.mWorld.mComponentCount);
			mUEntity.mWorld.registerEntity (mAIPoor[mTempCount]);
			mAIPoor [mTempCount].GetComponent<BaseAIComponent> ().mAIRT.SetActive (true);
			mTempCount ++;
			return mAIPoor [mTempCount - 1];
		}

	}

First of all, this manager will have a template, which is the Prefab of these AIs, and then at the beginning of the game, we generate a lot of these things, and then do not activate them.

Then we can see that Instantiate and Destroy are here, just move the array pointer and activate the GameObject operation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325743612&siteId=291194637