using System; using ProtoCSStruct; namespace Sog { /// /// cache一下结构体消息,每次在栈上new的话有个问题,c#new的时候会memset整个结构体,效率极低 /// 这么用要注意的情况是线程不安全且同时多个使用的时候有数量限制 /// /// public class CSStructPool : Singleton> where T : struct, IStructMessage { class WrapperObj where S : struct, IStructMessage { S _object; public ref S GetObj() { return ref _object; } } int _index; const int MaxPoolCount = 2; WrapperObj[] _objects = new WrapperObj[MaxPoolCount]; public CSStructPool() { for (int i = 0; i < _objects.Length; i++) { _objects[i] = new WrapperObj(); } } public ref T GetObjRef() { ref T obj = ref _objects[_index].GetObj(); obj.Clear(); _index++; if(_index >= MaxPoolCount) { _index = 0; } return ref obj; } } }