lru_redux 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78eae8fc324be527cb810fa0346fc009f4bee816
4
- data.tar.gz: dcc9a4af9681636d338ebdeca2e2eef99152dd06
3
+ metadata.gz: af4d80362e99b12a12ab32c6a8274f216195d666
4
+ data.tar.gz: 1d68495be201eadc3c851b4efdfb6efb7128e710
5
5
  SHA512:
6
- metadata.gz: 8c55ef7737644cdc0bdc25abbb3d4756eeef996e179ec31871f22925abd370c43e6070b513cf6bbc82afb31782430534111a32f2fab31a576d0892b8fef87241
7
- data.tar.gz: f11d972cd374a60163cf0430d6727755bb652a490fec1a0b36de96045faf35c5b7fab45ef8598f3a3223253141f851135e86cb23c741a6eee64bff854172f1b4
6
+ metadata.gz: a836ef1195b172d1d3bf9f4091e8fbfd5802546ef45504f75d0db2842d1943a83df9df83b0138eb199deea1e52449bb7ef2613a8bfb386b5e81c8d42df7a7d5a
7
+ data.tar.gz: 25cce1c929ad8eda1975d3af356b9705f114042f85d14a5b4aa7a128be89f12dc3cfe6b8a93837a692a9f7932efd6ff753592e216972760c1e20b61e4736b557
data/README.md CHANGED
@@ -45,19 +45,19 @@ see: benchmark directory
45
45
  ```
46
46
  sam@ubuntu:~/Source/lru_redux/bench$ ruby ./bench.rb
47
47
  Rehearsal ---------------------------------------------------------
48
- thread safe lru 27.940000 0.020000 27.960000 ( 28.000938)
49
- lru gem 2.300000 0.000000 2.300000 ( 2.305732)
50
- lru_cache gem 1.960000 0.010000 1.970000 ( 1.975683)
51
- lru_redux gem 1.710000 0.000000 1.710000 ( 1.704134)
52
- lru_redux thread safe 2.830000 0.000000 2.830000 ( 2.837608)
53
- ----------------------------------------------- total: 36.770000sec
48
+ thread safe lru 27.640000 0.370000 28.010000 ( 28.049021)
49
+ lru gem 2.460000 0.000000 2.460000 ( 2.454292)
50
+ lru_cache gem 2.170000 0.000000 2.170000 ( 2.174306)
51
+ lru_redux gem 1.530000 0.020000 1.550000 ( 1.552481)
52
+ lru_redux thread safe 2.610000 0.070000 2.680000 ( 2.684895)
53
+ ----------------------------------------------- total: 36.870000sec
54
54
 
55
55
  user system total real
56
- thread safe lru 27.740000 0.000000 27.740000 ( 27.756163)
57
- lru gem 2.250000 0.000000 2.250000 ( 2.252772)
58
- lru_cache gem 1.960000 0.000000 1.960000 ( 1.963679)
59
- lru_redux gem 1.710000 0.000000 1.710000 ( 1.712147)
60
- lru_redux thread safe 2.750000 0.000000 2.750000 ( 2.752526)
56
+ thread safe lru 28.170000 0.280000 28.450000 ( 28.465008)
57
+ lru gem 2.330000 0.000000 2.330000 ( 2.328316)
58
+ lru_cache gem 2.140000 0.000000 2.140000 ( 2.142749)
59
+ lru_redux gem 1.640000 0.000000 1.640000 ( 1.643732)
60
+ lru_redux thread safe 2.590000 0.000000 2.590000 ( 2.600422)
61
61
 
62
62
  ```
63
63
 
@@ -72,6 +72,11 @@ lru_redux thread safe 2.750000 0.000000 2.750000 ( 2.752526)
72
72
 
73
73
  ## Changlog
74
74
 
75
- ###verson 0.0.2 - 23-April-2013
75
+ ###version 0.0.2 - 23-April-2013
76
76
 
77
77
  - Added .clear method
78
+
79
+ ###version 0.0.3 - 23-April-2013
80
+
81
+ - Added .max_size= so you can resize caches on demand
82
+ - Added implemented using arrays for extra perf
@@ -2,6 +2,8 @@ class LruRedux::Cache
2
2
 
3
3
  # for high efficiency nodes in double linked list are stored in arrays
4
4
  # [prev,key,val,next]
5
+ #
6
+ # This makes this much more annoying to code, but gives us a 5-10% edge
5
7
 
6
8
  def initialize(max_size)
7
9
  @max_size = max_size
@@ -10,11 +12,17 @@ class LruRedux::Cache
10
12
  @tail = nil
11
13
  end
12
14
 
15
+ def max_size=(size)
16
+ @max_size = size
17
+ while pop_tail
18
+ end
19
+ end
20
+
13
21
  def [](key)
14
22
  node = @data[key]
15
23
  if node
16
24
  move_to_head(node)
17
- node.value
25
+ node[2]
18
26
  end
19
27
  end
20
28
 
@@ -22,7 +30,7 @@ class LruRedux::Cache
22
30
  node = @data[key]
23
31
  if node
24
32
  move_to_head(node)
25
- node.value = val
33
+ node[2] = val
26
34
  else
27
35
  pop_tail
28
36
  @data[key] = add_to_head(key,val)
@@ -33,8 +41,8 @@ class LruRedux::Cache
33
41
  def each
34
42
  if n = @head
35
43
  while n
36
- yield [n.key, n.value]
37
- n = n.prev
44
+ yield [n[1], n[2]]
45
+ n = n[0]
38
46
  end
39
47
  end
40
48
  end
@@ -54,11 +62,11 @@ class LruRedux::Cache
54
62
  node = @data[k]
55
63
  if node
56
64
  @data.delete(k)
57
- prev = node.prev
58
- nex = node.next
65
+ prev = node[0]
66
+ nex = node[3]
59
67
 
60
- prev.next = nex if prev
61
- nex.prev = prev if nex
68
+ prev[3] = nex if prev
69
+ nex[0] = prev if nex
62
70
  end
63
71
  end
64
72
 
@@ -74,60 +82,53 @@ class LruRedux::Cache
74
82
  # for cache validation only, ensures all is sound
75
83
  def valid?
76
84
  expected = {}
85
+
86
+ count = 0
77
87
  self.each_unsafe do |k,v|
78
- expected[k] = v
88
+ return false if @data[k][2] != v
89
+ count += 1
79
90
  end
80
- expected == @data
91
+ count == @data.count
81
92
  end
82
93
 
83
94
  protected
84
95
 
85
96
  def add_to_head(key,val)
86
97
  if @head.nil?
87
- @tail = @head = Node.new(key,val,nil,nil)
98
+ @tail = @head = [nil,key,val,nil]
88
99
  else
89
- node = Node.new(key,val,@head,nil)
90
- @head = @head.next = node
100
+ node = [@head,key,val,nil]
101
+ @head = @head[3] = node
91
102
  end
92
103
  end
93
104
 
94
105
  def move_to_head(node)
95
- return unless @head && node != @head
106
+ return unless @head && node[1] != @head[1]
96
107
 
97
- prev = node.prev
98
- nex = node.next
108
+ prev = node[0]
109
+ nex = node[3]
99
110
 
100
111
  if prev
101
- prev.next = nex
112
+ prev[3] = nex
102
113
  else
103
114
  @tail = nex
104
115
  end
105
116
 
106
117
  if nex
107
- nex.prev = prev
118
+ nex[0] = prev
108
119
  end
109
120
 
110
- @head.next = node
111
- node.prev = @head
121
+ @head[3] = node
122
+ node[0] = @head
112
123
  @head = node
113
124
  end
114
125
 
115
126
  def pop_tail
116
- if @data.length == @max_size
117
- @data.delete(@tail.key)
118
- @tail = @tail.next
119
- @tail.prev = nil
127
+ if @data.length >= @max_size
128
+ @data.delete(@tail[1])
129
+ @tail = @tail[3]
130
+ @tail[0] = nil
120
131
  end
121
132
  end
122
133
 
123
-
124
- class Node
125
- attr_accessor :key, :value, :next, :prev
126
- def initialize(key,value,prev,nex)
127
- self.key = key
128
- self.value = value
129
- self.prev = prev
130
- self.next = nex
131
- end
132
- end
133
134
  end
@@ -15,6 +15,6 @@ class LruRedux::ThreadSafeCache < LruRedux::Cache
15
15
  end
16
16
  end
17
17
 
18
- synchronize :[], :[]=, :each, :to_a, :delete, :count, :valid?
18
+ synchronize :[], :[]=, :each, :to_a, :delete, :count, :valid?, :max_size
19
19
 
20
20
  end
@@ -1,3 +1,3 @@
1
1
  module LruRedux
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -7,6 +7,10 @@ class CacheTest < MiniTest::Unit::TestCase
7
7
  @c = LruRedux::Cache.new(3)
8
8
  end
9
9
 
10
+ def teardown
11
+ assert_equal true, @c.valid?
12
+ end
13
+
10
14
  def test_drops_old
11
15
  @c[:a] = 1
12
16
  @c[:b] = 2
@@ -56,4 +60,21 @@ class CacheTest < MiniTest::Unit::TestCase
56
60
  @c.clear
57
61
  assert_equal [], @c.to_a
58
62
  end
63
+
64
+ def test_grow
65
+ @c[:a] = 1
66
+ @c[:b] = 2
67
+ @c[:c] = 3
68
+ @c.max_size = 4
69
+ @c[:d] = 4
70
+ assert_equal [[:d,4],[:c,3],[:b,2],[:a,1]], @c.to_a
71
+ end
72
+
73
+ def test_shrink
74
+ @c[:a] = 1
75
+ @c[:b] = 2
76
+ @c[:c] = 3
77
+ @c.max_size = 2
78
+ assert_equal [[:c,3],[:b,2]], @c.to_a
79
+ end
59
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lru_redux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  requirements: []
137
137
  rubyforge_project:
138
- rubygems_version: 2.0.0.rc.2
138
+ rubygems_version: 2.0.3
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: An efficient implementation of an lru cache