Unity中的string gc优化

在项目中如果有大量的字符串拼接,比如每秒执行的倒计时,协议中的日志输出,每次拼接会产生大量的gc,尤其是在ILRuntime下执行 gc alloc的次数会更加频繁。

zstring

有两个字符串处理的库都叫zstring,其中小写的zstring是一款国人开源的zstring,而大写的ZString是日本的CySharp公司的

国人的zstring:https://github.com/871041532/zstring

ZString:https://github.com/Cysharp/ZString

经测试zstring在update中是0gc,而ZString还有gc,短字符串处理,会比原生string慢一些,我的测试环境:Unity2019.3.4f1

gc分配值:string >ZString >zstring

执行耗时:zstring >ZString >string

易用性:string>zstring>ZString

测试代码

void Update()
{
	Profiler.BeginSample("zstring=============concat");
	ZString.Concat("abc",1);
	Profiler.EndSample();
	
	Profiler.BeginSample("string=============concat");
	string.Concat("abc",1);
	Profiler.EndSample();
	
	Profiler.BeginSample("cn-zstring=============concat");
	using (zstring.Block())
	{
		zstring.Concat("abc",1);
	}
	Profiler.EndSample();
	
	Profiler.BeginSample("zstring=============format");
	ZString.Format("hello,{0}",1111);
	Profiler.EndSample();
	
	Profiler.BeginSample("string=============format");
	string.Format("hello,1111);
	Profiler.EndSample();
	
	Profiler.BeginSample("cn-zstring=============format");
	using (zstring.Block())
	{
		zstring.Format("hello,1111);
	}
	Profiler.EndSample();
}

ZString和string gc对比

Method Allocated(B) Mean(ns) Mean(ns)
StringPlus 224 126.66 126.66
ZStringConcat 56 96.95 96.95
StringFormat 128 158.21 158.21
ZStringFormat 56 185.36 185.36
StringBuilder 296 144.2 144.2
ZStringBuilder 56 131.68 131.68

结论

个人而言更倾向于使用国人开发的zstring,它只有一个代码文件,放Plugins目录下就可以使用,相对而言上手更容易

Unity的建议

关于内存管理可看这篇: https://docs.unity3d.com/cn/current/Manual/UnderstandingAutomaticMemoryManagement.html

其中有条建议,对于需要频繁拼接的字符串,先判断字符串是否有变化,无变化则不需要拼接

对于字符串的拼接,使用stringbuilder代替string,同时我也建议使用zstring代替string的+=

对于参数的传递,可以传入引用类型,就不需要每次都构建一个新的对象

相关文章

这篇文章主要介绍了Unity游戏开发中外观模式是什么意思,具有...
这篇文章主要介绍Unity中地面检测方案的示例分析,文中介绍的...
这篇文章主要介绍了Unity游戏开发中如何消除不想要的黄色警告...
这篇文章主要介绍了Unity中有多少种渲染队列,具有一定借鉴价...
这篇文章主要介绍Unity中如何实现Texture,文中介绍的非常详...
小编给大家分享一下Unity中DOTS要实现的特点有哪些,相信大部...