[心得] 如何减少 GTA Online 70% 加载时间?(转)

楼主: alihue (wanda wanda)   2021-03-27 23:29:27
原始文章: https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times-by-70/
如何减少 GTA Online 70% 加载时间?
作者觉得 GTA Online 加载时间机八久,因此想要一探究竟
首先作者做 benchmark 发现
Story mode load time: ~1m 10s
Online mode load time: ~6m flat
然后看看 cpu usage 同时在这段时间燃烧
第一步,作者先做 profiling 可以得知哪一些 function 占了最多 CPU 资源
第二步,用 disassembler 去看程式码
问题1. It’s… strlen?!
好像会 parse 一个高达 10MB 的 json 档案,其中包含 63k items
这些资料是游戏中的线上商城的
问题不是 10MB,而是读取资料的方式
https://i.imgur.com/ZHLlEDJ.png
读法是用 sscanf 一次读一个 char,读完马上呼叫 strlen,
再继续读下一个 char....
然后当要读的档案有 10mb 这么大就有很重的 overhead
更新: 这段是错的 请见另一位版友回文
问题2. Let’s use a Hash- … Array?
有一段 code 是读取资料,判断是否存在 array 中 (linear scan),然后储存
然后如果资料有 ~63k,则会有 1984531500 次判断,然后大部分没用
为什么不用 hash map!!!!
POC
作者写了一个 .dll 然后 inject 到 GTA
问题1.解法;
- hook strlen
- wait for a long string
-“cache” the start and length of it
- if it’s called again within the string’s range, return cached value
问题2.解法;
移除重复的判断,然后直接塞值 (因为已经知道值一定唯一)
结果: 效能增进 70%
最后 GTA 开发商采用了这个,并给出 10k 的奖金
作者: ms0529876 (大洋游侠)   2021-03-27 23:44:00
作者: APTON (玮玮)   2021-03-28 06:36:00
作者: ab4daa (nooooooooooooooooooo)   2021-03-28 11:27:00
威 没有source 还能加速
作者: hduek153 (专业打酱油)   2021-03-28 12:35:00
菜鸡表示strlen看不太懂 有人能解释一下吗
作者: tomap41017 (绝梦)   2021-03-28 12:40:00
干谁写的烂扣
作者: fr75 (阿巴 )   2021-03-28 12:53:00
卖了几亿的公司只发了一万镁赏金 帮QQ
作者: peterbrucele (DM5)   2021-03-28 19:32:00
看到linear scanf就头疼...更正 scan
作者: jasonwung (路人JJ)   2021-03-29 00:07:00
厉害推
作者: viper9709 (阿达)   2021-03-29 01:21:00
推卖了几亿只发一万赏金XD
作者: Ofianse (大便人)   2021-03-29 02:01:00
然后拿着这头衔看看能不能多赚几百万镁
作者: Sindragosa (蔡刺刺)   2021-03-29 10:13:00
作者: zrna0515 (神定o枪枪)   2021-03-29 10:58:00
作者: fbid (禁止)   2021-03-29 18:12:00
所以每次strlen出来都是1?
作者: shooter555 (shooter)   2021-03-30 17:02:00
应该是加快strlen吧 先做好string cache 然后判断strring是哪一份cache里的就直接回整份cache的长度 就不用跑原本strlen一个字一个字判断结尾的动作话说用strstr会不会快一点前面看错了 他只是靠static cache前一次的point
楼主: alihue (wanda wanda)   2021-03-30 17:38:00
我对于 strlen 那段理解是错的,详情请看另一篇版友回文
作者: ce173310 (Alan...)   2021-04-08 21:38:00
既然确定是parse json,就可以省略检查有没有重复的步骤

Links booklink

Contact Us: admin [ a t ] ucptt.com