|
| 1 | +// Copyright (c) 2015 Hans Alexander Gugel <alexander.gugel@gmail.com> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +// This file contains a modified version of package arc from |
| 22 | +// https://github.com/alexanderGugel/arc |
| 23 | +// |
| 24 | +// It implements the ARC (Adaptive Replacement Cache) algorithm as detailed in |
| 25 | +// https://www.usenix.org/legacy/event/fast03/tech/full_papers/megiddo/megiddo.pdf |
| 26 | + |
| 27 | +package trie |
| 28 | + |
| 29 | +import ( |
| 30 | + "container/list" |
| 31 | + "sync" |
| 32 | +) |
| 33 | + |
| 34 | +type arc struct { |
| 35 | + p int |
| 36 | + c int |
| 37 | + t1 *list.List |
| 38 | + b1 *list.List |
| 39 | + t2 *list.List |
| 40 | + b2 *list.List |
| 41 | + cache map[string]*entry |
| 42 | + mutex sync.Mutex |
| 43 | +} |
| 44 | + |
| 45 | +type entry struct { |
| 46 | + key hashNode |
| 47 | + value node |
| 48 | + ll *list.List |
| 49 | + el *list.Element |
| 50 | +} |
| 51 | + |
| 52 | +// newARC returns a new Adaptive Replacement Cache with the |
| 53 | +// given capacity. |
| 54 | +func newARC(c int) *arc { |
| 55 | + return &arc{ |
| 56 | + c: c, |
| 57 | + t1: list.New(), |
| 58 | + b1: list.New(), |
| 59 | + t2: list.New(), |
| 60 | + b2: list.New(), |
| 61 | + cache: make(map[string]*entry, c), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Put inserts a new key-value pair into the cache. |
| 66 | +// This optimizes future access to this entry (side effect). |
| 67 | +func (a *arc) Put(key hashNode, value node) bool { |
| 68 | + a.mutex.Lock() |
| 69 | + defer a.mutex.Unlock() |
| 70 | + ent, ok := a.cache[string(key)] |
| 71 | + if ok != true { |
| 72 | + ent = &entry{key: key, value: value} |
| 73 | + a.req(ent) |
| 74 | + a.cache[string(key)] = ent |
| 75 | + } else { |
| 76 | + ent.value = value |
| 77 | + a.req(ent) |
| 78 | + } |
| 79 | + return ok |
| 80 | +} |
| 81 | + |
| 82 | +// Get retrieves a previously via Set inserted entry. |
| 83 | +// This optimizes future access to this entry (side effect). |
| 84 | +func (a *arc) Get(key hashNode) (value node, ok bool) { |
| 85 | + a.mutex.Lock() |
| 86 | + defer a.mutex.Unlock() |
| 87 | + ent, ok := a.cache[string(key)] |
| 88 | + if ok { |
| 89 | + a.req(ent) |
| 90 | + return ent.value, ent.value != nil |
| 91 | + } |
| 92 | + return nil, false |
| 93 | +} |
| 94 | + |
| 95 | +func (a *arc) req(ent *entry) { |
| 96 | + if ent.ll == a.t1 || ent.ll == a.t2 { |
| 97 | + // Case I |
| 98 | + ent.setMRU(a.t2) |
| 99 | + } else if ent.ll == a.b1 { |
| 100 | + // Case II |
| 101 | + // Cache Miss in t1 and t2 |
| 102 | + |
| 103 | + // Adaptation |
| 104 | + var d int |
| 105 | + if a.b1.Len() >= a.b2.Len() { |
| 106 | + d = 1 |
| 107 | + } else { |
| 108 | + d = a.b2.Len() / a.b1.Len() |
| 109 | + } |
| 110 | + a.p = a.p + d |
| 111 | + if a.p > a.c { |
| 112 | + a.p = a.c |
| 113 | + } |
| 114 | + |
| 115 | + a.replace(ent) |
| 116 | + ent.setMRU(a.t2) |
| 117 | + } else if ent.ll == a.b2 { |
| 118 | + // Case III |
| 119 | + // Cache Miss in t1 and t2 |
| 120 | + |
| 121 | + // Adaptation |
| 122 | + var d int |
| 123 | + if a.b2.Len() >= a.b1.Len() { |
| 124 | + d = 1 |
| 125 | + } else { |
| 126 | + d = a.b1.Len() / a.b2.Len() |
| 127 | + } |
| 128 | + a.p = a.p - d |
| 129 | + if a.p < 0 { |
| 130 | + a.p = 0 |
| 131 | + } |
| 132 | + |
| 133 | + a.replace(ent) |
| 134 | + ent.setMRU(a.t2) |
| 135 | + } else if ent.ll == nil { |
| 136 | + // Case IV |
| 137 | + |
| 138 | + if a.t1.Len()+a.b1.Len() == a.c { |
| 139 | + // Case A |
| 140 | + if a.t1.Len() < a.c { |
| 141 | + a.delLRU(a.b1) |
| 142 | + a.replace(ent) |
| 143 | + } else { |
| 144 | + a.delLRU(a.t1) |
| 145 | + } |
| 146 | + } else if a.t1.Len()+a.b1.Len() < a.c { |
| 147 | + // Case B |
| 148 | + if a.t1.Len()+a.t2.Len()+a.b1.Len()+a.b2.Len() >= a.c { |
| 149 | + if a.t1.Len()+a.t2.Len()+a.b1.Len()+a.b2.Len() == 2*a.c { |
| 150 | + a.delLRU(a.b2) |
| 151 | + } |
| 152 | + a.replace(ent) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + ent.setMRU(a.t1) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func (a *arc) delLRU(list *list.List) { |
| 161 | + lru := list.Back() |
| 162 | + list.Remove(lru) |
| 163 | + delete(a.cache, string(lru.Value.(*entry).key)) |
| 164 | +} |
| 165 | + |
| 166 | +func (a *arc) replace(ent *entry) { |
| 167 | + if a.t1.Len() > 0 && ((a.t1.Len() > a.p) || (ent.ll == a.b2 && a.t1.Len() == a.p)) { |
| 168 | + lru := a.t1.Back().Value.(*entry) |
| 169 | + lru.value = nil |
| 170 | + lru.setMRU(a.b1) |
| 171 | + } else { |
| 172 | + lru := a.t2.Back().Value.(*entry) |
| 173 | + lru.value = nil |
| 174 | + lru.setMRU(a.b2) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func (e *entry) setLRU(list *list.List) { |
| 179 | + e.detach() |
| 180 | + e.ll = list |
| 181 | + e.el = e.ll.PushBack(e) |
| 182 | +} |
| 183 | + |
| 184 | +func (e *entry) setMRU(list *list.List) { |
| 185 | + e.detach() |
| 186 | + e.ll = list |
| 187 | + e.el = e.ll.PushFront(e) |
| 188 | +} |
| 189 | + |
| 190 | +func (e *entry) detach() { |
| 191 | + if e.ll != nil { |
| 192 | + e.ll.Remove(e.el) |
| 193 | + } |
| 194 | +} |
0 commit comments