Class: CodeTools::Compiler::LRUCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/code/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.



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

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.



192
193
194
# File 'lib/rubinius/code/compiler/compiler.rb', line 192

def current
  @current
end

#missesObject (readonly)

Returns the value of attribute misses.



192
193
194
# File 'lib/rubinius/code/compiler/compiler.rb', line 192

def misses
  @misses
end

Instance Method Details

#clear!Object



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

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



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

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



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

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



237
238
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
# File 'lib/rubinius/code/compiler/compiler.rb', line 237

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