Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(608)

Side by Side Diff: ios/chrome/browser/snapshots/lru_cache.mm

Issue 1410973008: Added an experiment for an LRU snapshot cache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/snapshots/lru_cache.h"
6
7 #include "base/containers/hash_tables.h"
8 #include "base/containers/mru_cache.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h"
11
12 namespace {
13
14 class MRUCacheNSObjectDelegate {
15 public:
16 MRUCacheNSObjectDelegate(id<LRUCacheDelegate> delegate)
17 : delegate_(delegate) {}
18
19 MRUCacheNSObjectDelegate(const MRUCacheNSObjectDelegate& other) = default;
20
21 void operator()(const base::scoped_nsprotocol<id<NSObject>>& payload) const {
22 [delegate_ lruCacheWillEvictObject:payload.get()];
23 }
24
25 private:
26 id<LRUCacheDelegate> delegate_; // weak.
sdefresne 2015/11/03 16:58:08 nit, optional: s/weak/Weak/
jbbegue 2015/11/05 17:14:40 Done.
27 };
28
29 struct NSObjectEqualTo {
30 bool operator()(const base::scoped_nsprotocol<id<NSObject>>& obj1,
31 const base::scoped_nsprotocol<id<NSObject>>& obj2) const {
32 return [obj1 isEqual:obj2];
33 }
34 };
35
36 struct NSObjectHash {
37 std::size_t operator()(
38 const base::scoped_nsprotocol<id<NSObject>>& obj) const {
39 return [obj hash];
40 }
41 };
42
43 template <class KeyType, class ValueType>
44 struct MRUCacheNSObjectHashMap {
45 typedef base::hash_map<KeyType, ValueType, NSObjectHash, NSObjectEqualTo>
46 Type;
47 };
48
49 class NSObjectMRUCache
50 : public base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>,
51 base::scoped_nsprotocol<id<NSObject>>,
52 MRUCacheNSObjectDelegate,
53 MRUCacheNSObjectHashMap> {
54 private:
55 typedef base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>,
56 base::scoped_nsprotocol<id<NSObject>>,
57 MRUCacheNSObjectDelegate,
58 MRUCacheNSObjectHashMap> ParentType;
59
60 public:
61 NSObjectMRUCache(typename ParentType::size_type max_size,
62 const MRUCacheNSObjectDelegate& deletor)
63 : ParentType(max_size, deletor) {}
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(NSObjectMRUCache);
67 };
68 }
sdefresne 2015/11/03 16:58:08 }; } // namespace
jbbegue 2015/11/05 17:14:40 Done.
69
70 @interface LRUCache ()<LRUCacheDelegate>
71 @end
72
73 @implementation LRUCache {
74 scoped_ptr<NSObjectMRUCache> _cache;
75 }
76
77 @synthesize delegate = _delegate;
78 @synthesize maxCacheSize = _maxCacheSize;
79
80 - (instancetype)init {
81 NOTREACHED(); // Use initWithCacheSize instead.
sdefresne 2015/11/03 16:58:08 nit: s/initWithCacheSize/initWithCacheSize:/
jbbegue 2015/11/05 17:14:40 Done.
82 return nil;
83 }
84
85 - (instancetype)initWithCacheSize:(NSUInteger)maxCacheSize {
86 self = [super init];
87 if (self) {
88 _maxCacheSize = maxCacheSize;
89 MRUCacheNSObjectDelegate cacheDelegateDeletor(self);
90 _cache.reset(new NSObjectMRUCache(self.maxCacheSize, cacheDelegateDeletor));
91 }
92 return self;
93 }
94
95 - (id)objectForKey:(id<NSObject>)key {
96 base::scoped_nsprotocol<id<NSObject>> keyObj([key retain]);
97 auto it = _cache->Get(keyObj);
98 if (it == _cache->end())
99 return nil;
100 return it->second.get();
101 }
102
103 - (void)setObject:(id<NSObject>)value forKey:(NSObject*)key {
104 base::scoped_nsprotocol<id<NSObject>> keyObj([key copy]);
105 base::scoped_nsprotocol<id<NSObject>> valueObj([value retain]);
106 _cache->Put(keyObj, valueObj);
107 }
108
109 - (void)removeObjectForKey:(id<NSObject>)key {
110 base::scoped_nsprotocol<id<NSObject>> keyObj([key retain]);
111 auto it = _cache->Peek(keyObj);
112 if (it != _cache->end())
113 _cache->Erase(it);
114 }
115
116 - (void)removeAllObjects {
117 _cache->Clear();
118 }
119
120 - (NSUInteger)count {
121 return _cache->size();
122 }
123
124 - (BOOL)isEmpty {
125 return _cache->empty();
126 }
127
128 #pragma mark - Private
129
130 - (void)lruCacheWillEvictObject:(id<NSObject>)obj {
131 [self.delegate lruCacheWillEvictObject:obj];
132 }
133
134 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698