客户端配置文件优化策略

news/2024/7/10 3:27:45 标签: 配置文件, 优化, 手机开发, 性能, uwa

原文链接:https://blog.uwa4d.com/archives/2045.html

这是侑虎科技第303篇原创文章,感谢作者故国之晚秋供稿,欢迎转发分享,未经作者授权请勿转载。当然,如果您有任何独到的见解或者发现也欢迎联系我们,一起探讨。(QQ群:465082844)

作者GitHub:https://github.com/billwillman/NsConfigLib


前要

一般游戏策划给的配置都是用Excel,最后给程序是CSV、或者是转换成Json和二进制格式。本次测试中比对了市面上大部分Json库的性能比较,包括:LitJson、System.Net.Json、FastJson、Newtonsoft.Json以及二进制格式。

其中,Json库中在全部同步读取解析最优秀的是Newtonsoft.json,GC以及速度最快。最差的是System.Net.Json,GC竟然高达100多MB,下文将介绍详细的测试过程。


一、Json库性能比较

测试方式:IO全部读取完成,测试平台为同一台PC机器,只测试其解析耗时以及GC量。

1、LitJson
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F1.png" alt="请输入图片描述" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC量:15.7 MB
读取速度:494.61 ms

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F2.png" width="300" height="180" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

2、FastJson
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F3.png" width="800" height="30" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC量:25.4 MB
耗时:199.72 ms

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F4.png" width="300" height="180" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

3、System.Net.Json
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F5.png" width="800" height="30" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC量:151.2 MB
耗时:1657.7 ms

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F6.png" width="300" height="180" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

4、NewtonJson
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F7.png" width="800" height="30" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC量: 7.7 MB
耗时:266.84 ms

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F8.png" width="300" height="180" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />


二、二进制读取测试

一般配置文件都分KEY和VALUE两部分,我们也将二进制数据也分为KEY和VALUE两部分保存,文件结构:

文件头(包括信息以及有效性标识)
—————————————————————————
数据VALUE部分
—————————————————————————
数据KEY部分
—————————————————————————

测试分为以下四种:只读取二进制KEY部分,二进制全部读取,二进制采用协程分步骤全部读取(测试中,默认每次读取500条数据,读完KEY,再读取VALUE),以及协程只读取KEY部分。

1、只读取二进制KEY部分
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F9.png" width="800" height="30" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC量:1.6 MB
耗时:10.06 ms

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F10.png" width="300" height="180" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

2、二进制全部读取
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F11.png" width="800" height="30" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC量:6.5 MB
耗时:119.41 ms

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F12.png" width="300" height="180" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

3、协程每次500条,全部读取
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F13.png" width="800" height="30" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC最高峰值:462.5 KB
耗时最高峰值:17.06 ms
回调全部读取完成消耗时间:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F14.png" width="500" height="60" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F15.png" width="300" height="60" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

4、协程只读取KEY

uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F16.png" width="800" height="30" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F17.png" width="800" height="30" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

GC最高峰值:0.7 MB
耗时最高峰值:13.97 ms
回调全部读取完成消耗时间:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F18.png" width="500" height="60" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />

性能走势图:
uwa-ducument-img.oss-cn-beijing.aliyuncs.com/Blog%2FUSparkle_%20Configuration%2F19.png" width="300" height="180" align="center" alt="" style="margin:0px; padding:0px; outline:0px; border:0px; vertical-align:baseline; height:auto; max-width:100%; background:transparent" />


三、Json库测试结论

1.NewtonJson库的GC量以及耗时最低,最差是System.Net.Json。
2.其中GC量大的原因是:System.Text.Encoding.UTF8.GetString函数,以及Json库内部字符串处理,本身IO读取产生的GC量相对比较小。


四、二进制优化策略

1.常用小配置文件可以采用一次性将KEY和VALUE全部读取;

2.数据列不是很多但数据量中等的配置,可以采用只读取KEY,用到取VALUE再读取策略;

3.数据列很多并且数据量很大的配置,可以采用全部异步,预加载的方式优化

4.采用预加载的方式,可以将配置文件IO部分和解析部分执行分开,先IO异步预加载完,再执行解析,尽量将内存峰值降低,防止因为配置文件导致堆内存过高。


五、示例工程

地址:https://github.com/billwillman/NsConfigLib

1.考虑到很多项目都是Json格式,示例工程中,我们采用尽量兼容Json的结构方式,封装了一个ConfigDictionary类,用于针对模拟一部分Dictionary的操作,隔离掉解析操作。

2.支持三种Json定义方式的转换,Dictionary<KEY, Vo>、Dictionary<KEY, List> 、Dictionary<KEY1, Dictionary<KEY2, Vo>>, 其中Vo为数据。

3.使用方式很简单,Json解析方式,以LITJson为例

LitJson.JsonMapper.ToObject<Dictionary<string, List<VO>>>(str);

ConfigDictioanry示例

Dictionary<KEY, Vo>对应ConfigVoMap<KEY, Vo>
Dictionary<KEY, List<Vo>对应ConfigVoListMap<KEY, Vo>
Dictionary<KEY1, Dictionary<KEY2, Vo>对应ConfigVoMapMap<KEY1, KEY2, Vo>

ConfigVoListMap<KEY, Vo> map = new ConfigVoListMap<KEY, Vo>();

// 读取全部:
map .LoadFromTextAsset
map.LoadFromBytes

// 预加载方式
map.Preload(m_FileName, m_Mono, OnEnd, OnProcess);
  1. 测试场景:
    Untitled和Test1场景均是测试场景分别测试List结构和Dictionary结构。

文末,再次感谢故国之晚秋的分享,如果您有任何独到的见解或者发现也欢迎联系我们,一起探讨。(QQ群:465082844)。

也欢迎大家来积极参与U Sparkle开发者计划,简称"US",代表你和我,代表UWA和开发者在一起!



http://www.niftyadmin.cn/n/1031958.html

相关文章

python 0xa_运行时错误:根据API版本0xa编译的模块,但此版本的numpy为0x9

我正在学习关于新编码器(this one)的API教程&#xff0c;在尝试运行程序时出现以下错误&#xff1a;RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9Traceback (most recent call last):File "api.py", line 7, in import …

UWA 优化日杭州站:节后第一波充电计划约起来!

原文链接&#xff1a;https://blog.uwa4d.com/archives/uwaday_1028.html 【UWA优化日】是UWA不定期举办的线下技术交流活动&#xff0c;目前已在北、上、广多次成功举办。这次&#xff0c;我们来到了杭州&#xff0c;你也恰好在这里吗&#xff1f; 近一年来&#xff0c;中国移…

python中stacked_第15.35节 PyQt编程实战:结合QDial实现的QStackedWidget堆叠窗口程序例子...

一、案例说明本案例是老猿在学习QStackedWidget中的一个测试案例&#xff0c;该案例使用QStackedWidget展示一个文件目录下的图片文件&#xff0c;可以有多种实现方式&#xff0c;在本案例中一个图片文件使用QStackedWidget的一个页面窗口展现&#xff0c;有多少图片文件就有多…

我所理解的委托和匿名函数

原文链接&#xff1a;https://blog.uwa4d.com/archives/2072.html 这是侑虎科技第310篇原创文章&#xff0c;感谢作者卢建供稿。欢迎转发分享&#xff0c;未经作者授权请勿转载。作者QQ&#xff1a;345005607。如果您有任何独到的见解或者发现也欢迎联系我们&#xff0c;一起探…

为什么他们都用UWA GOT?

原文链接&#xff1a;https://blog.uwa4d.com/archives/2082.html 手游开发和优化的时间一直是寸土寸金的&#xff0c;随着中国移动游戏的精品化趋势日趋明显&#xff0c;整体表现都在迅速向端游品质进行靠拢。随之而来的&#xff0c;则是各种性能压力的急剧上升&#xff0c;优…

wps怎么把边框加粗_WPS文字表格怎么把表格外框线加粗?

当我们要对word中插入的表格边框进行加粗的话&#xff0c;该如何操作呢&#xff1f;这就是本章的主要内容&#xff0c;脚本之家教大家如何去给word表格编辑进行加粗的方法。一、对word表格整个表格进行边框加粗&#xff1a;选中表格&#xff0c;右击-“边框和底纹”&#xff0c…

微信开网页mysql_长见识了,原来微信浏览器内可以直接启动外部浏览器

微信浏览器内&#xff0c;不需任何操作&#xff0c;直接启动外部浏览器是怎么实现的&#xff1f;看完这篇文章你就明白了。**做微信营销活动或者APK下载推广时候&#xff0c;域名被经常被封&#xff0c;做到微信中正常使用呢&#xff1f;这就要借助一些工具来实现有效的操作。*…

关于Unity渲染优化,你可能遇到这些问题

原文链接&#xff1a;https://blog.uwa4d.com/archives/QA_Rendering.html 关键字 Draw Call 半透明物体渲染 多层纹理渲染 Graphics.PresentAndSync VBO 相机后处理特效 Draw Call相关 Q1&#xff1a;移动游戏场景中&#xff0c;相同的怪物&#xff0c;Draw Call会动态合并吗&…