Tencent / VasSonic

VasSonic is a lightweight and high-performance Hybrid framework developed by tencent VAS team, which is intended to speed up the first screen of websites working on Android and iOS platform.
Other
11.8k stars 1.61k forks source link

Optimize speed of spliting template and data from html data #261

Closed zhongwuzw closed 5 years ago

zhongwuzw commented 5 years ago

We use - replaceOccurrencesOfString:withString:options:range: to replace tag. But it's not efficient and much time cost. It needs to scan whole file to replace.

I run a performance test to compare the new(my PR) and old(framework currently implementation), split the framework demo main.html 1000 times, results are below:

Used main.html:

image

Used main.html but has 10 data tag:

image

Used main.html but has 20 data tag:

image

Used main.html but has 20 data tag, and double file size:

image

Conclusion: Along with increasing of data tag or file size, the old method's cost is increasing much higher. For the last test, new split method is at least 6ms faster than old way.

tencent-adm commented 5 years ago

CLA assistant check
All committers have signed the CLA.

sonic-ios-dev commented 5 years ago

为你的钻研精神点赞,Sonic的目的在于优化单次页面加载速度,首先强调一点这里这个文本替换方法并不会加快或者拖慢页面加载速度,拆分数据目的是在二次加载的时候用来刷新局部数据的,并且拆分模版和动态数据是在子线程进行的异步操作,不会影响主线程,可以肯定的是搜索替换文本方法改为明确range替换是一个更好的方法,我会在之后验证代码执行的结果来确定是否合入PR。

zhongwuzw commented 5 years ago

@sonic-ios-dev 谢谢,是的,不会对主线程有大的影响,优化只是减少了split时的CPU占用时间,即从整体上看SonicSessionQueueThread将会消耗更少的CPU时间片。

zhongwuzw commented 5 years ago

@vasdeveloper 咦?这个是最后验证有什么问题么?