关于UIText使用ttf字体创建时,效率低下的问题反馈!望修复

关于UIText使用ttf字体创建时,效率低下的问题反馈!望修复

主要是在Text::init中发现了两个问题:

  • setString函数在setFontName和setFontSize之后调用 : 这导致一串文字会被创建两次
  • setFontName 和 setFontSize 里面都调用了一遍setTTFConfig,这个操作也比较废时间

这两个问题导致创建速度慢了7~10倍左右(引擎项目组也核实下吧)

解决方案

把setString方法移到setFontName和setFontSize之后调用
把setFontName和SetFontSize合并
(同理:在读取cocostudio的ui文件中,也有这个顺序的问题!!!)

代码如下
bool Text::init(const std::string &textContent, const std::string &fontName, int fontSize)
{
  bool ret = true;
  do
  {
    if (!Widget::init())
    {
      ret = false;
      break;
    }
    this->setFontNameAndSize(fontName, fontSize);
 this->setString(textContent);
 } while (0);
  return ret;
}

这样改了之后创建速度会提升7~10倍左右吧,以下是测试数据

测试代码

this.schedule(function(){
  var start_time = Date.now()
  for(var i = 0 ; i < 1000; i++) {
    var text = ccui.Text.create("KlayGE的字体系统设计目标是一个快速、易于实现、支持字体的高", "xxxxx.ttf", 11);
  }
  HHLog("label time: " + (Date.now() - start_time))
}.bind(this), 5)

以下数据是在debug模式下跑的

修改过的版本

IOS版本

JS: Tag :0 label time: 1431
JS: Tag :0 label time: 1278
JS: Tag :0 label time: 1189
JS: Tag :0 label time: 1183
JS: Tag :0 label time: 1181
JS: Tag :0 label time: 1188
JS: Tag :0 label time: 1180
JS: Tag :0 label time: 1180
JS: Tag :0 label time: 1177

mac版本

JS: Tag :0 label time: 139
JS: Tag :0 label time: 128
JS: Tag :0 label time: 157
JS: Tag :0 label time: 133

未修改过的版本

IOS版本

cocos2d: JS: Tag :0 label time: 10858
cocos2d: JS: Tag :0 label time: 10619
cocos2d: JS: Tag :0 label time: 10676
cocos2d: JS: Tag :0 label time: 11550

mac版本

cocos2d: JS: Tag :0 label time: 2710
cocos2d: JS: Tag :0 label time: 2014
cocos2d: JS: Tag :0 label time: 2192
cocos2d: JS: Tag :0 label time: 3132
cocos2d: JS: Tag :0 label time: 3411
cocos2d: JS: Tag :0 label time: 3639
cocos2d: JS: Tag :0 label time: 2468


以下数据是在release模式下跑的

修改过的版本

IOS版本

JS: Tag :0 label time: 918
JS: Tag :0 label time: 789
JS: Tag :0 label time: 687
JS: Tag :0 label time: 678
JS: Tag :0 label time: 675
JS: Tag :0 label time: 674
JS: Tag :0 label time: 670
JS: Tag :0 label time: 676

未修改过的版本

IOS版本

cocos2d: JS: Tag :0 label time: 8672
cocos2d: JS: Tag :0 label time: 8457
cocos2d: JS: Tag :0 label time: 8434
cocos2d: JS: Tag :0 label time: 8429

楼主是说浏览器上的问题,还是native上的?

native …

哦。好的。感谢反馈,:877:

对于这个问题,更好的做法应该是修改Label的实现,不管修改ttf字体的字体名称还是字体大小,都不要立即生效,而只是标识dirty,最后在渲染之前,统一修改这些属性。

我们会在后续的版本修复这个问题,感谢反馈。:2: