unity里c# gc优化 -字符串
1使用unsafe,直接修改字符串
public static class UnsafeString
{
public static unsafe void Copy(this string str, LuaString luastr)
{
if (luastr.ptr == IntPtr.Zero)
Clear( str );
fixed (char* pstr = str)
{
var dest = (char*)luastr.ptr.ToPointer();
var len = luastr.len;
for (int i = ; i < len; i++)
*(pstr + i) = *(dest + i);
*(pstr + len) = '\0';
int* strlen = (int*)pstr - ;
*strlen = len;
}
} public static unsafe void Clear(this string str)
{
fixed (char* ptr = str)
{
int* iptr = (int*)ptr - ;
*iptr = ;
}
}
}
修改text部分
if (txt.text.Length * >= _str.len)//c#是ucs2编码,一个字符是2个字节
{
txt.text.Copy(_str);
txt.cachedTextGenerator.Invalidate();
txt.SetVerticesDirty();
}
else
txt.text = _str.ToString();
但是有个坑,lua是utf8编码,而c#里是ucs2b编码,所以需要做个转换,明天补上
需要2个方法,一个是获取utf8编码个数
public unsafe int ulen()
{
if (len <= ) return ;
if (utf8len > ) return utf8len;
var str = (byte*)(strdata().ToPointer());
int n = ;
for (int i = ; i < len; i++)
{
var c = str[i];
//short c = (short) s;
i += c >= 0xfc ? : (c >= 0xf8 ? : (c >= 0xf0 ? : (c >= 0xe0 ? : (c >= 0xc0 ? : ))));
n++;
}
utf8len = n;
return utf8len;
}
一个是从utf8转到unicode(utf16)
public static unsafe void CopyAnsi(this string str, LuaString luastr)
{
if (luastr.ptr == IntPtr.Zero)
Clear(str);
fixed (char* pstr = str)
{
var dest = (byte*)luastr.strdata().ToPointer();
var len = luastr.len;
var pstrn = ;
int oldlen = *((int*)pstr - );
for (int i = ; i < len; i++)
{
var c = *(dest + i);
if (c >= 0xfc)
{ }
else if (c >= 0xf8)
{ }
else if (c >= 0xf0)
{ }
else if (c >= 0xe0)
{
var c1 = * (dest + i + );
var c2 = * (dest + i + );
*(pstr + pstrn) = (char)(((c & 0x1f) << ) + ((c1 & 0x3f) << )+ (c2 & 0x3f));
i += ;
}
else if (c >= 0xc0)
{
var c1 = *(dest + i + );
*(pstr + pstrn) = (char)(((c & 0x3c) << ) + (c1 & 0x3f));
i++;
}
else
{
* (pstr + pstrn) = (char)c;
}
pstrn++;
}
//*(pstr + pstrn) = (byte)0;
int* strlen = (int*)pstr - ;
*strlen = pstrn;
}
}
修改text部分
set {
if(!(c is Text) && !(c is InputField)) Log.Assert((c is Text), "the component name {0} is not a Text or InputField", c.name);
if (c is Text)
{
if (_str.ptr == value.str.ptr) return;
unrefString(_str);
_str = value.str;
var txt = (c as Text);
if (txt.text.Length >= _str.ulen())
{
//txt.text.Copy(_str);
txt.text.CopyAnsi(_str);
txt.cachedTextGenerator.Invalidate();
txt.SetVerticesDirty();
}
else
txt.text = _str.ToString();
refString(_str);
}
if (c is InputField) (c as InputField).text = (string)value;
}
unity里c# gc优化 -字符串的更多相关文章
- Unity中的GC以及优化
[简介] 常见的 Unity GC 知识点总结出来的思维导图 Unity 官方文档,正巧在博客园发现了已经有位大神(zblade)把原文翻译出来了,而且质量很高~,译文地址 在这里.下面我就可耻地把译 ...
- 浅谈Unity中的GC以及优化
介绍: 在游戏运行的时候,数据主要存储在内存中,当游戏的数据不在需要的时候,存储当前数据的内存就可以被回收再次使用.内存垃圾是指当前废弃数据所占用的内存,垃圾回收(GC)是指将废弃的内存重新回收再次使 ...
- Unity下XLua方案的各值类型GC优化深度剖析
转自:http://gad.qq.com/article/detail/25645 前言 Unity下的C#GC Alloc(下面简称gc)是个大问题,而嵌入一个动态类型的Lua后,它们之间的交互很容 ...
- 关于Unity中的UGUI优化,你可能遇到这些问题
https://blog.uwa4d.com/archives/QA_UGUI-1.html 关于Unity中的UGUI优化,你可能遇到这些问题 作者:admin / 时间:2016年11月08日 / ...
- Unity基础-脚本的优化
脚本的优化 object pool 避免频繁的内存分配和gc噩梦(字符串相加?) 是否有必要都写在update里?分帧? 需要的只取一次 使用editor内赋值,而不是find 复杂的物理 复杂的数学 ...
- Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)
在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...
- Unity3D游戏GC优化总结---protobuf-net无GC版本优化实践
protobuf-net优化效果图 protobuf-net是Unity3D游戏开发中被广泛使用的Google Protocol Buffer库的c#版本,之所以c#版本被广泛使用,是因为c++版本的 ...
- 如何降低90%Java垃圾回收时间?以阿里HBase的GC优化实践为例
过去的一年里,我们准备在Ali-HBase上突破这个被普遍认知的痛点,为此进行了深度分析及全面创新的工作,获得了一些比较好的效果.以蚂蚁风控场景为例,HBase的线上young GC时间从120ms减 ...
- JVM:从实际案例聊聊Java应用的GC优化
原文转载自美团从实际案例聊聊Java应用的GC优化,感谢原作者的贡献 当Java程序性能达不到既定目标,且其他优化手段都已经穷尽时,通常需要调整垃圾回收器来进一步提高性能,称为GC优化.但GC算法复杂 ...
随机推荐
- 了解机器学习框架CoreML
代码地址如下:http://www.demodashi.com/demo/11972.html CoreML是iOS 11新推出的机器学习框架,是人工智能的核心内容,他可以在训练好的机器学习模型应用到 ...
- javascript - Underscore 与 函数式编程
<Javascript函数式编程 PDF> # csdn下载地址http://download.csdn.net/detail/tssxm/9713727 Underscore # git ...
- FPGA组成、工作原理和开发流程
FPGA组成.工作原理和开发流程 原创 2012年01月07日 09:11:52 9402 0 4 ********************************LoongEmbedded***** ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- iOS使用AVCaptureSession自定义相机
关于iOS调用摄像机来获取照片,通常我们都会调用UIImagePickerController来调用系统提供的相机来拍照,这个控件非常好 用.但是有时UIImagePickerController控件 ...
- Math函数的"四舍五入",Floor,Ceiling,Round的一些注意事项!
1.Math.Round:四舍六入五取偶 引用内容 Math.Round(0.0) //0Math.Round(0.1) //0Math.Round(0.2) //0Math.Round(0.3) / ...
- 40-语言入门-40-C小加之随机数
题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=255 15 20 32 40 67 89 300 400 代码: #includ ...
- IOS设计模式浅析之外观模式(Facade)
引言 在项目开发中,有时候会遇到这样的一种情景:已有系统的各个子系统之间,随着业务需求的发展,有了比较紧凑的耦合关系.现在需要利用这些子系统的功能,为移动端提供业务处理.我们该怎么应对这样的业务需求呢 ...
- eclipse通过maven远程发布应用到Tomcat
好久没有写博客了,今天为大家分享一下如何在eclipse通过maven远程发布应用到Tomcat. 一般情况下,我们发布应用到服务器需要现将应用导出成war包,然后连接服务器部署更新,这样是很耗时的, ...
- iframe定位获取
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...