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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ios/chrome/browser/snapshots/lru_cache.mm
diff --git a/ios/chrome/browser/snapshots/lru_cache.mm b/ios/chrome/browser/snapshots/lru_cache.mm
new file mode 100644
index 0000000000000000000000000000000000000000..d1201135d95d88d9070914d54d0dc5eb5ad1eb74
--- /dev/null
+++ b/ios/chrome/browser/snapshots/lru_cache.mm
@@ -0,0 +1,134 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/chrome/browser/snapshots/lru_cache.h"
+
+#include "base/containers/hash_tables.h"
+#include "base/containers/mru_cache.h"
+#include "base/mac/scoped_nsobject.h"
+#include "base/memory/scoped_ptr.h"
+
+namespace {
+
+class MRUCacheNSObjectDelegate {
+ public:
+ MRUCacheNSObjectDelegate(id<LRUCacheDelegate> delegate)
+ : delegate_(delegate) {}
+
+ MRUCacheNSObjectDelegate(const MRUCacheNSObjectDelegate& other) = default;
+
+ void operator()(const base::scoped_nsprotocol<id<NSObject>>& payload) const {
+ [delegate_ lruCacheWillEvictObject:payload.get()];
+ }
+
+ private:
+ id<LRUCacheDelegate> delegate_; // weak.
sdefresne 2015/11/03 16:58:08 nit, optional: s/weak/Weak/
jbbegue 2015/11/05 17:14:40 Done.
+};
+
+struct NSObjectEqualTo {
+ bool operator()(const base::scoped_nsprotocol<id<NSObject>>& obj1,
+ const base::scoped_nsprotocol<id<NSObject>>& obj2) const {
+ return [obj1 isEqual:obj2];
+ }
+};
+
+struct NSObjectHash {
+ std::size_t operator()(
+ const base::scoped_nsprotocol<id<NSObject>>& obj) const {
+ return [obj hash];
+ }
+};
+
+template <class KeyType, class ValueType>
+struct MRUCacheNSObjectHashMap {
+ typedef base::hash_map<KeyType, ValueType, NSObjectHash, NSObjectEqualTo>
+ Type;
+};
+
+class NSObjectMRUCache
+ : public base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>,
+ base::scoped_nsprotocol<id<NSObject>>,
+ MRUCacheNSObjectDelegate,
+ MRUCacheNSObjectHashMap> {
+ private:
+ typedef base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>,
+ base::scoped_nsprotocol<id<NSObject>>,
+ MRUCacheNSObjectDelegate,
+ MRUCacheNSObjectHashMap> ParentType;
+
+ public:
+ NSObjectMRUCache(typename ParentType::size_type max_size,
+ const MRUCacheNSObjectDelegate& deletor)
+ : ParentType(max_size, deletor) {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(NSObjectMRUCache);
+};
+}
sdefresne 2015/11/03 16:58:08 }; } // namespace
jbbegue 2015/11/05 17:14:40 Done.
+
+@interface LRUCache ()<LRUCacheDelegate>
+@end
+
+@implementation LRUCache {
+ scoped_ptr<NSObjectMRUCache> _cache;
+}
+
+@synthesize delegate = _delegate;
+@synthesize maxCacheSize = _maxCacheSize;
+
+- (instancetype)init {
+ NOTREACHED(); // Use initWithCacheSize instead.
sdefresne 2015/11/03 16:58:08 nit: s/initWithCacheSize/initWithCacheSize:/
jbbegue 2015/11/05 17:14:40 Done.
+ return nil;
+}
+
+- (instancetype)initWithCacheSize:(NSUInteger)maxCacheSize {
+ self = [super init];
+ if (self) {
+ _maxCacheSize = maxCacheSize;
+ MRUCacheNSObjectDelegate cacheDelegateDeletor(self);
+ _cache.reset(new NSObjectMRUCache(self.maxCacheSize, cacheDelegateDeletor));
+ }
+ return self;
+}
+
+- (id)objectForKey:(id<NSObject>)key {
+ base::scoped_nsprotocol<id<NSObject>> keyObj([key retain]);
+ auto it = _cache->Get(keyObj);
+ if (it == _cache->end())
+ return nil;
+ return it->second.get();
+}
+
+- (void)setObject:(id<NSObject>)value forKey:(NSObject*)key {
+ base::scoped_nsprotocol<id<NSObject>> keyObj([key copy]);
+ base::scoped_nsprotocol<id<NSObject>> valueObj([value retain]);
+ _cache->Put(keyObj, valueObj);
+}
+
+- (void)removeObjectForKey:(id<NSObject>)key {
+ base::scoped_nsprotocol<id<NSObject>> keyObj([key retain]);
+ auto it = _cache->Peek(keyObj);
+ if (it != _cache->end())
+ _cache->Erase(it);
+}
+
+- (void)removeAllObjects {
+ _cache->Clear();
+}
+
+- (NSUInteger)count {
+ return _cache->size();
+}
+
+- (BOOL)isEmpty {
+ return _cache->empty();
+}
+
+#pragma mark - Private
+
+- (void)lruCacheWillEvictObject:(id<NSObject>)obj {
+ [self.delegate lruCacheWillEvictObject:obj];
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698