Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改进cacheExpr的缓存命中率的一点想法 #508

Closed
liujian16 opened this issue Sep 29, 2014 · 3 comments
Closed

改进cacheExpr的缓存命中率的一点想法 #508

liujian16 opened this issue Sep 29, 2014 · 3 comments

Comments

@liujian16
Copy link

parseExpr中使用了一个cacheExpr作为生成的代码的缓存。但是这个缓存的命中率很低,对于后面附的例子,命中率是0。其原因在于cacheExpr把所有的VM的id拼起来形成一个exprId作为key, 而对于ms-repeat每次生成的eachProxy的id都是不一样的。这样虽然每次循环的代码都是一样的,可是因为exprId不一样缓存无法命中。

我的改进思路如下,为每一次repeat展开所生成的eachProxy赋予一个“批次号”,用这个批次号参与exprId的构造,这样就能保证缓存的命中。

我的试验性修改方法如下:

  1. 修改bindingExecutor.repeatcase "add":分支, 为eachProxy增加$repeatbatchId属性
                       var batchId = '$repeatbatchId$' + Math.random() //新增代码
                        for (var i = 0, n = arr.length; i < n; i++) {
                            var ii = i + pos
                            var proxy = getEachProxy(ii, arr[i], data, last)
                            proxy['$repeatbatchId'] = batchId                   //新增代码
                            proxies.splice(ii, 0, proxy)
                            lastFn = shimController(data, transation, spans, proxy)
                        }
  1. 修改parseExpr,调整exprId的构造逻辑
 var exprId = scopes.map(function(el) {
            return  el.$repeatbatchId || el.$id.replace(rproxy, "$1") //如果存在$repeatbatchId则使用之
        }) + code + dataType + filters

对于下面的测试用例,avalon.scan的耗时能减少约30%. 我对avalon整体的代码还是不熟悉,这里的修改不一定能适用所有的场景,仅供参考吧。

测试用例

<!DOCTYPE HTML>
<html>
    <head>
        <title>ms-repeat</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
        <script src="avalon.js" ></script>

    </head>
    <body >
      <div ms-controller="test">
        <div ms-repeat-array="arrays">
            <img ms-repeat="array" ms-src="{{el}}" style="width:150px ; height:150px" ms-click="imageClicked(el)" ms-visible="visible"/>
        </div>
      </div>
      <script>
            var vmodel = avalon.define("test", function(vm) {
               vm.arrays=[];
               vm.imageClicked = function(src){
                    console.log(src + 'clicked!')
               }
               vm.visible = true
            })

            for(var i=0 ; i<20 ; i++){
                var arr = [];
                for(var j=0 ;j <30; j++){
                    arr.push("12.jpg");
                }
                vmodel.arrays.push(arr);
            }

            setTimeout(function(){
                // vmodel.arrays = []
            } ,20000)

            var start = new Date().getTime();
            avalon.scan();
            var end = new Date().getTime();
            console.log("avalon.scan cost " + (end - start) + "ms")
        </script>
    </body>
</html>
@RubyLouvre
Copy link
Owner

@RubyLouvre
Copy link
Owner

缓存的命中率与缓存运行的时间长短及客户机访问相同数量从缓存读取的次数等因素有关,所以当缓存命中率低

时可以从以下两方面进行排查:

1.缓存运行的时间,时间越长命中率会越高

2.缓存设置的大小,缓存设置过小也会导致命中率低

(如服务器8G内存 服务器系统正常运行保留2G 网络游戏:4096M 单机游戏:1024M 启动盘:256-1024M)

@RubyLouvre
Copy link
Owner

已经修复 f889dc4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants