From ce5e7350abd8e03fac533336f102998ea8f4ec72 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 12 Apr 2016 19:44:36 +0300 Subject: [PATCH] rewritten data.getImages(..) removing a .compact() related bottleneck... Signed-off-by: Alex A. Naanou --- ui (gen4)/data.js | 134 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 1 deletion(-) diff --git a/ui (gen4)/data.js b/ui (gen4)/data.js index 616eb915..efa7fe79 100755 --- a/ui (gen4)/data.js +++ b/ui (gen4)/data.js @@ -849,7 +849,8 @@ var DataPrototype = { // one of the contained images (first?) // XXX for some reason negative target number (ribbon number) // breaks this... - getImages: function(target, count, mode){ + /* + _getImages: function(target, count, mode){ target = (target == null && count == null) ? 'loaded' : target mode = mode == null ? 'around' : mode var list @@ -898,6 +899,7 @@ var DataPrototype = { // get the ribbon gids... if(list == null){ list = this.ribbons[this.getRibbon(target)] + // XXX compacting here is bad, we'll be handling the whole ribbon... list = list != null ? list.compact() : [] } @@ -914,15 +916,24 @@ var DataPrototype = { var from = i - Math.floor(count) var to = i + Math.ceil(count) + var pre = Math.floor(count) + var post = Math.ceil(count) + } else if(mode == 'before'){ // NOTE: we are shifting by 1 to include the target... var from = (i - count) + 1 var to = i + 1 + var pre = count - 1 + var post = 0 + } else if(mode == 'after'){ var from = i var to = i + count + var pre = 0 + var post = count - 1 + } else { // XXX bad mode.... return null @@ -932,6 +943,127 @@ var DataPrototype = { from = Math.max(0, from) return list.slice(from, to) + + + var res = [target] + + // pre... + for(var n = i-1; n >= 0 && pre > 0; n--){ + if(n in list){ + res.push(list[n]) + pre-- + } + } + + res.reverse() + // post... + for(var n = i+1; n < list.length && post > 0; n--){ + if(n in list){ + res.push(list[n]) + post-- + } + } + return res + }, + */ + // NOTE: this is a partial rewrite avoiding .compact() as much as + // possible and restricting it to as small a subset as possible + getImages: function(target, count, mode){ + target = (target == null && count == null) ? 'loaded' : target + mode = mode == null ? 'around' : mode + var list + + // normalize target and build the source list... + + // 'current' ribbon... + target = target == 'current' ? this.current : target + + // get all gids... + if(target == 'all'){ + list = this.order + target = null + + // get loaded only gids... + } else if(target == 'loaded'){ + var res = [] + var ribbons = this.ribbons + for(var k in ribbons){ + this.makeSparseImages(ribbons[k], res) + } + list = res.compact() + target = null + + // filter out the unloaded gids from given list... + } else if(target != null && target.constructor === Array){ + var loaded = count == 'current' ? this.getImages('current') + : count in this.ribbons ? this.ribbons[count].compact() + : typeof(count) == typeof(123) ? + this.ribbons[this.getRibbon(count)].compact() + : this.getImages('loaded') + count = null + + list = target.filter(function(e){ + return loaded.indexOf(e) >= 0 + }) + + target = null + + // target is ribbon gid... + } else if(target in this.ribbons){ + list = this.ribbons[target] + } + + // NOTE: list can be null if we got an image gid or ribbon order... + // get the ribbon gids... + list = list || this.ribbons[this.getRibbon(target)] || [] + + if(count == null){ + return list.compact() + } + + target = this.getImage(target) + var i = list.indexOf(target) + + // prepare to slice the list... + if(mode == 'around'){ + count = count/2 + var pre = Math.floor(count) + var post = Math.ceil(count) - 1 + + } else if(mode == 'before'){ + var pre = count - 1 + var post = 0 + + } else if(mode == 'after'){ + var pre = 0 + var post = count - 1 + + } else { + // XXX bad mode.... + return null + } + + var res = [target] + + // pre... + for(var n = i-1; n >= 0 && pre > 0; n--){ + if(n in list){ + res.push(list[n]) + pre-- + } + } + + res.reverse() + + // post... + for(var n = i+1; n < list.length && post > 0; n++){ + if(n in list){ + res.push(list[n]) + post-- + } + } + + return res }, // Get ribbon...