Class: SC::Helpers::PackedOptimizer

Inherits:
Object
  • Object
show all
Defined in:
lib/sproutcore/helpers/packed_optimizer.rb

Overview

Given a set of targets, this can determine the optimial mix of loading packed targets vs individual targets to yield the smaller number of assets. To use this optimizer, just call the optimize() method.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePackedOptimizer

Returns a new instance of PackedOptimizer.



45
46
47
48
49
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 45

def initialize
  @seen = []
  @packed = []
  @unpacked = []
end

Instance Attribute Details

#packedObject (readonly)

Returns the value of attribute packed.



42
43
44
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 42

def packed
  @packed
end

#unpackedObject (readonly)

Returns the value of attribute unpacked.



43
44
45
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 43

def unpacked
  @unpacked
end

Class Method Details

.optimize(targets) ⇒ Object

Returns two arrays: the first array are targets that should be loaded packed. The array of targets that should be loaded, but not packed. And optional third array is also returned that includes targets which were passed in but are no longer needed because they are included in a packed target.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 20

def self.optimize(targets)
  packed = []
  unpacked = targets
  cnt = packed.size + unpacked.size
  
  # for each target, try to use the packed version and see how many
  # total items we come back with.  If the total number of targets is 
  # less than the current best option, use that instead.
  targets.each do |target|
    cur_packed, cur_unpacked = self.new.process(target, targets)
    cur_cnt = cur_packed.size + cur_unpacked.size
    if cur_cnt < cnt
      packed   = cur_packed
      unpacked = cur_unpacked
      cnt      = cur_cnt
    end
  end
  
  # return best!
  return [packed, unpacked]
end

Instance Method Details

#process(packed_target, targets) ⇒ Object

Sorts the passed array of targets into packed and unpacked, starting with the passed target as the packed target.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 53

def process(packed_target, targets)
  # manually add in packed target...
  @seen   << packed_target
  @packed << packed_target
  packed_target.expand_required_targets.each { |t| @seen << t }
  
  # then handle the rest of the targets
  targets.each { |t| process_target(t) }
  
  return [packed, unpacked]
end

#process_target(target) ⇒ Object

Sorts a single target into the correct bucket based on whether it is seen or not. If you also pass true to the second param, the required targets will also be sorted.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 68

def process_target(target)
  # if target was seen already, nothing to do
  return if @seen.include?(target)

  # add to seen
  @seen << target
  
  # have we already seen any of the required targets?
  req_targets = target.expand_required_targets
  if req_targets.size > 0
    already_seen = req_targets.find do |t|
      @seen.include?(t)
    end
  else
    already_seen = true
  end
  
  
  # if we have seen one, then we can't use the packed version so put it
  # in the unpacked set.
  if already_seen
    req_targets.each { |t| process_target(t) }
    @unpacked << target  # add last to keep order intact
    
  # if we have not seen any required, then we can mark this as packed.
  # yeah!  mark the required targets as seen also
  else
    req_targets.each { |t| @seen << t }
    @packed << target
  end
end