Class: Rubinius::ToolSet.current::TS::Compiler::LRUCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/compiler/compiler.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total) ⇒ LRUCache

Returns a new instance of LRUCache.



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rubinius/compiler/compiler.rb', line 181

def initialize(total)
  @cache = {}
  @total = total
  @current = 0

  @head = Entry.new(nil, nil)
  @tail = Entry.new(nil, nil)

  @tail.insert_after(@head)

  @misses = 0
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



194
195
196
# File 'lib/rubinius/compiler/compiler.rb', line 194

def current
  @current
end

#missesObject (readonly)

Returns the value of attribute misses.



194
195
196
# File 'lib/rubinius/compiler/compiler.rb', line 194

def misses
  @misses
end

Instance Method Details

#clear!Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rubinius/compiler/compiler.rb', line 196

def clear!
  Rubinius.synchronize(self) do
    @cache = {}
    @current = 0

    @head = Entry.new(nil, nil, -1)
    @tail = Entry.new(nil, nil, -2)

    @tail.insert_after(@head)
  end
end

#explainObject



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rubinius/compiler/compiler.rb', line 208

def explain
  entry = @head.next_entry
  while entry != @tail
    str, layout = entry.key
    puts "hits: #{entry.hits}"
    puts "layout: #{layout.inspect}"
    puts "<STRING>"
    puts str
    puts "</STRING>"

    entry = entry.next_entry
  end
end

#retrieve(key) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rubinius/compiler/compiler.rb', line 222

def retrieve(key)
  Rubinius.synchronize(self) do
    if entry = @cache[key]
      entry.inc!

      entry.detach!
      entry.insert_before @tail

      return entry.value
    end

    @misses += 1

    nil
  end
end

#set(key, value) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rubinius/compiler/compiler.rb', line 239

def set(key, value)
  Rubinius.synchronize(self) do
    if entry = @cache[key]
      entry.value = value

      entry.detach!
      entry.insert_before @tail

      return value
    end

    if @current == @total
      entry = @head.next_entry

      entry.detach!

      @cache.delete entry.key
    else
      @current += 1
    end

    entry = Entry.new(key, value)

    entry.insert_before @tail

    @cache[key] = entry
  end
end