【Unity C#优化】业务逻辑代码方面的优化

代码方面的优化,主要是以降低内存的开辟和清理,提高CPU缓存命中率,利用多线程,为核心所进行的。

1.List与Dictionary的优化

List和Dictionary核心都是数组,Insert插入和Remove删除都要对数组进行复制和拷贝移动。并且每次扩容时,也会创建新的内存空间。

因此在创建时,最好规定一个预期容量,不要交给程序自己扩容。并且少用Insert插入接口。

2.巧用struct

struct是值类型,内存分配在栈上,栈是内存连续,回收快速简单,不会产生内存碎片,也不需要内存垃圾回收,CPU读取数据对连续内存很友好和高效。

struct数组的内存空间和值类型是连续的,调用时CPU的缓存命中率高。(CPU在读取数据时,会将数据缓存下来,下次先从缓存中找数据,如果命中,则不需要在内存中读取数据)

而class是引用类型,class数组中的每个对象都是堆上的独立内存空间,而数组的引用相当于C++的指针地址,数组中只有引用空间内存是连续的。缓存机制的命中率要照struct数组大大降低。

但如果struct太大,超过了缓存机制上线,则缓存机制不在起作用。可以使用值类型的数组来提高缓存命中率。

将A类的数组

Class A
{
	public int a;
	public float b;
	public bool c;
}

改为B类中的int数组、float数组、bool数组的形式

Class B
{
	public int[] a;
	public float[] b;
	public bool[] c;
}

A类数据是内存分散的,并且每个A的实例也是内存分散,只有arrayA的引用是连续的。

而B类中每个数组都是内存连续的(值类型的数组一定是内存连续),这样能更好地利用缓存,提高CPU读取数据时缓存的命中率。

3.对象池

使用对象池来减少对象的创建和回收。

对于普通对象也可以使用对象池,特别是使用List和Dictionary时,添加和回收都通过对象池进行操作。

可以大大降低内存的分配和释放所带来的消耗。

而且可以开局对对象池进行初始化,降低临时开辟内存所带来的开销。

4.string字符串相关

(1)缓存项目中的常用字符串

通过ID或Dictionary字典将项目中字符串缓存下来。下次调用时就不需要开辟新内存了。

(2)通过指针更改缓存字符串中的值

使用Dictionary字典缓存,字符串长度作为Key,字符串作为value。
使用字符串前,调用方法,如有相同长度的字符串,则通过指针将字符串中的值更改为期望值。

Dictionary<int, string> cacheStr;

Public unsafe string Concat(string strA, string strB)
{
	int a_length = strA.Length;	
	int b_length = strB.Length;	
	int sum_length = a_length + b_length;
	string strResult = null;
	
	if(!cacheStr.TryGetValue(sum_length, out strResult))
	{
		strResult = strA + strB;
		cacheStr.Add(sum_length, strResult);
		return strResult;
	}
	
	fixed(char* strA_ptr = strA)
	{
		fixed(char* strB_ptr = strB)
		{
			fixed(char* strResult_ptr =strResult)
			{
				memcopy((byte*)strResult_ptr, (byte*)strA_ptr, a_length * sizeof(char));
				
				memcopy((byte*)strResult_ptr + a_length, (byte*)strB_ptr, b_length * sizeof(char));
			}
		}
	}
	return strResult;
}

public unsafe void memcopy(byte* dest, byte* src, int len)
{
	while((--len) >= 0)
	{
		dest[len] = src[len];
	}
}

相关推荐

  1. 【Unity C#优化业务逻辑代码方面优化

    2024-07-19 02:36:02       22 阅读
  2. 代码优化方法记录

    2024-07-19 02:36:02       22 阅读
  3. 30个业务场景SQL优化

    2024-07-19 02:36:02       32 阅读
  4. vue优化方法

    2024-07-19 02:36:02       56 阅读
  5. 优化SQL方法

    2024-07-19 02:36:02       29 阅读
  6. 优化SQL方法

    2024-07-19 02:36:02       37 阅读
  7. 一次业务批量数据任务处理优化

    2024-07-19 02:36:02       18 阅读
  8. 不停止业务情况下优化 Elasticsearch Reindex

    2024-07-19 02:36:02       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 02:36:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 02:36:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 02:36:02       58 阅读
  4. Python语言-面向对象

    2024-07-19 02:36:02       69 阅读

热门阅读

  1. 【Linux】微基准测试

    2024-07-19 02:36:02       21 阅读
  2. AI发展下的伦理挑战,应当如何应对?

    2024-07-19 02:36:02       19 阅读
  3. 软件测试有光明的未来

    2024-07-19 02:36:02       20 阅读
  4. 1、PostgreSQL安装

    2024-07-19 02:36:02       20 阅读
  5. 数组reduce的众多用法

    2024-07-19 02:36:02       21 阅读
  6. 泛型+反射

    2024-07-19 02:36:02       18 阅读
  7. 牛客周赛51

    2024-07-19 02:36:02       22 阅读
  8. 开始构建我们自己的大语言模型:数据处理部分

    2024-07-19 02:36:02       21 阅读
  9. Vue.js(第一天)

    2024-07-19 02:36:02       21 阅读
  10. ThreadPoolExecutor拒绝策略

    2024-07-19 02:36:02       23 阅读
  11. Redis 散列

    2024-07-19 02:36:02       17 阅读