美文网首页
字符串优化

字符串优化

作者: APP4x | 来源:发表于2020-06-21 23:26 被阅读0次

参考文章:
Unity 游戏的 string interning 优化
C#的字符串优化-String.Intern、IsInterned

主要目的是减少字符串的GC。

1.优化字符串数量
字符串是不可变的,每次修改字符串都会生成一个新的字符串
即使创建了两个相同的字符串,也会生成2个对象

问题是啥:
1.string创建与销毁的时机
2.是否存在重复的字符串
3.存在没有意义的字符串对象
4.capacity远大于length
5.string泄露

那对于不可变的相同对象,完全是可以复用的
(通过string.Intern)

intern是啥?

举个例子:

 string hello = "hello";
string helloWorld = "hello world";
string helloWorld2 = hello + " world";

Debug.Log(helloWorld == helloWorld2);//true
Debug.Log(object.ReferenceEquals(helloWorld, helloWorld2));//false

helloWorld2 = "hello world";

Debug.Log(helloWorld == helloWorld2);//true
Debug.Log(object.ReferenceEquals(helloWorld, helloWorld2));//true

下面的helloWorld和helloWorld2引用一致,why?

因为c#已经处理了,建立了内部池,池中每一个不同的字符串存在唯一一个个体在池中

但是因为c#不知道啥玩意应该放进去,也不是所有的字符串都要放进去,所以c#提供了String.InternString.Isinterned接口,交给程序自己维护

Intern 如图所示:

原理:如果已经在池中就返回引用,如果不在就添加进去并返回引用

eg:

string a =new string(new char[] {'a', 'b', 'c'});
object o = String.Copy(a);

Debug.Log(object.ReferenceEquals(o, a));//false

string.Intern(o.ToString());
Debug.Log(object.ReferenceEquals(o, string.Intern(a)));//true

object o2 = String.Copy(a);
String.Intern(o2.ToString());//此处虽然已经有cache了,但是o2还指向其自身
Debug.Log(object.ReferenceEquals(o2, string.Intern(a)));//false

//如果是常量字符串,会自动加入内部池

string a ="abc";
object o = String.Copy(a);

string.Intern(o.ToString());
Debug.Log(object.ReferenceEquals(o, string.Intern(a)));//同理o还指向自身,此处为 false

Isinterned 如图所示:

原理:判断是否在池内,如果在就返回引用;不在就返回null

string s = new string(new char[] { 'x', 'y', 'z' });
Debug.Log(string.IsInterned(s) ?? "not interned");//not interned

string s2 = "xyz2";
Debug.Log(string.IsInterned(s2) ?? "not interned");//xyz

坑:
1.手动在运行时拼出来的字符串不会自动复用已有对象(拼接、解析、转换等方式)
2.妹有提供清楚所有Intern字符串的功能


咋优化?
1.自己写个池
2.类似Animator.GetHash接口,代替字符串;第一次得到这个字符后立即调用Resources.PathToHash计算Hash值并存储
(使用ulong降低Hash冲突,构建时候也要判断是否冲突

相关文章

网友评论

      本文标题:字符串优化

      本文链接:https://www.haomeiwen.com/subject/rgiaxktx.html