白鹭 有人好奇为什么hashcode 增长的那么快么?都创建了些什么?

带着这样的疑问, 我做了个小统计

简单粗暴, 直接在HashObject 中插入:

[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
HashObject.count = {};
function HashObject() {
    this.$hashCode = egret.$hashCount++;
    if(this.__proto__){
        var name = this.__proto__.__class__;
        if(name){
            HashObject.count[name] = (HashObject.count[name] || 0) + 1;
        }
    }
}



输出统计:
var t1 = []; var count = egret.HashObject.count; for(let k in count){ t1.push({class:k, count:count[k]});}; t1.sort((a, b)=>{return a.count - b.count;})
t1.forEach(v=>{console.log(v.class, v.count)})

运行90秒左右, 总 hashCode 为 81705.
排行前十的类:
VM7412:1 ui.ColorLabel 584
VM7412:1 eui.Group 598
VM7412:1 eui.BasicLayout 837
VM7412:1 egret.Texture 1110
VM7412:1 eui.Label 1149
VM7412:1 eui.State 1338
VM7412:1 eui.Image 2614
VM7412:1 egret.Rectangle 12692
VM7412:1 egret.Matrix 23851
VM7412:1 egret.Point 34043

感觉其中大部分是 用于渲染的对象。

然后 发现源码中, 这三个类都是有做对象池的。 为什么创建数量还是这么多呢?

希望大家能发表一些关于对象池的使用心得。 

对于大的对象, 逻辑比较复杂, 使用对象池, 回收的时候太麻烦怎么办?
对于小的对象, 用完就释放了, 不使用对象池会造成什么样的不良后果?

 

原文地址:https://bbs.egret.com/thread-29226-1-1.html

posted @ 2019-08-03 15:40  dbc_pp  阅读(332)  评论(0编辑  收藏  举报