summaryrefslogtreecommitdiff
path: root/src/com/android/bitmap/UnrefedPooledCache.java
blob: 7bc6d0dd0ea671042c53ebd73728b5f75647d3f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bitmap;

import android.util.Log;
import android.util.LruCache;

import com.android.bitmap.util.Trace;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * An alternative implementation of a pool+cache. This implementation only counts
 * unreferenced objects in its size calculation. Internally, it never evicts from
 * its cache, and instead {@link #poll()} is allowed to return unreferenced cache
 * entries.
 * <p>
 * You would only use this kind of cache if your objects are interchangeable and
 * have significant allocation cost, and if your memory footprint is somewhat
 * flexible.
 * <p>
 * Because this class only counts unreferenced objects toward targetSize,
 * it will have a total memory footprint of:
 * <code>(targetSize) + (# of threads concurrently writing to cache) +
 * (total size of still-referenced entries)</code>
 *
 */
public class UnrefedPooledCache<K, V extends Poolable> implements PooledCache<K, V> {

    private final LinkedHashMap<K, V> mCache;
    private final LinkedBlockingQueue<V> mPool;
    private final int mTargetSize;
    private final LruCache<K, V> mNonPooledCache;

    private static final boolean DEBUG = DecodeTask.DEBUG;
    private static final String TAG = UnrefedPooledCache.class.getSimpleName();

    /**
     * @param targetSize not exactly a max size in practice
     * @param nonPooledFraction the fractional portion in the range [0.0,1.0] of targetSize to
     * dedicate to non-poolable entries
     */
    public UnrefedPooledCache(int targetSize, float nonPooledFraction) {
        mCache = new LinkedHashMap<K, V>(0, 0.75f, true);
        mPool = new LinkedBlockingQueue<V>();
        final int nonPooledSize = Math.round(targetSize * nonPooledFraction);
        if (nonPooledSize > 0) {
            mNonPooledCache = new NonPooledCache(nonPooledSize);
        } else {
            mNonPooledCache = null;
        }
        mTargetSize = targetSize - nonPooledSize;
    }

    @Override
    public V get(K key, boolean incrementRefCount) {
        Trace.beginSection("cache get");
        synchronized (mCache) {
            V result = mCache.get(key);
            if (result == null && mNonPooledCache != null) {
                result = mNonPooledCache.get(key);
            }
            if (incrementRefCount && result != null) {
                result.acquireReference();
            }
            Trace.endSection();
            return result;
        }
    }

    @Override
    public V put(K key, V value) {
        Trace.beginSection("cache put");
        // Null values not supported.
        if (value == null) {
            Trace.endSection();
            return null;
        }
        synchronized (mCache) {
            final V prev;
            if (value.isEligibleForPooling()) {
                prev = mCache.put(key, value);
            } else if (mNonPooledCache != null) {
                prev = mNonPooledCache.put(key, value);
            } else {
                prev = null;
            }
            Trace.endSection();
            return prev;
        }
    }

    @Override
    public void offer(V value) {
        Trace.beginSection("pool offer");
        if (value.getRefCount() != 0 || !value.isEligibleForPooling()) {
            Trace.endSection();
            throw new IllegalArgumentException("unexpected offer of an invalid object: " + value);
        }
        mPool.offer(value);
        Trace.endSection();
    }

    @Override
    public V poll() {
        Trace.beginSection("pool poll");
        final V pooled = mPool.poll();
        if (pooled != null) {
            Trace.endSection();
            return pooled;
        }

        synchronized (mCache) {
            int unrefSize = 0;
            Map.Entry<K, V> eldestUnref = null;
            for (Map.Entry<K, V> entry : mCache.entrySet()) {
                final V value = entry.getValue();
                if (value.getRefCount() > 0 || !value.isEligibleForPooling()) {
                    continue;
                }
                if (eldestUnref == null) {
                    eldestUnref = entry;
                }
                unrefSize += sizeOf(value);
                if (unrefSize > mTargetSize) {
                    break;
                }
            }
            // only return a scavenged cache entry if the cache has enough
            // eligible (unreferenced) items
            if (unrefSize <= mTargetSize) {
                if (DEBUG) {
                    Log.e(TAG, "POOL SCAVENGE FAILED, cache not fully warm yet. szDelta="
                            + (mTargetSize-unrefSize));
                }
                Trace.endSection();
                return null;
            } else {
                mCache.remove(eldestUnref.getKey());
                if (DEBUG) {
                    Log.e(TAG, "POOL SCAVENGE SUCCESS, oldKey=" + eldestUnref.getKey());
                }
                Trace.endSection();
                return eldestUnref.getValue();
            }
        }
    }

    protected int sizeOf(V value) {
        return 1;
    }

    @Override
    public String toDebugString() {
        if (DEBUG) {
            final StringBuilder sb = new StringBuilder("[");
            sb.append(super.toString());
            int size = 0;
            synchronized (mCache) {
                sb.append(" poolCount=");
                sb.append(mPool.size());
                sb.append(" cacheSize=");
                sb.append(mCache.size());
                if (mNonPooledCache != null) {
                    sb.append(" nonPooledCacheSize=");
                    sb.append(mNonPooledCache.size());
                }
                sb.append("\n---------------------");
                for (V val : mPool) {
                    size += sizeOf(val);
                    sb.append("\n\tpool item: ");
                    sb.append(val);
                }
                sb.append("\n---------------------");
                for (Map.Entry<K, V> item : mCache.entrySet()) {
                    final V val = item.getValue();
                    sb.append("\n\tcache key=");
                    sb.append(item.getKey());
                    sb.append(" val=");
                    sb.append(val);
                    size += sizeOf(val);
                }
                sb.append("\n---------------------");
                if (mNonPooledCache != null) {
                    for (Map.Entry<K, V> item : mNonPooledCache.snapshot().entrySet()) {
                        final V val = item.getValue();
                        sb.append("\n\tnon-pooled cache key=");
                        sb.append(item.getKey());
                        sb.append(" val=");
                        sb.append(val);
                        size += sizeOf(val);
                    }
                    sb.append("\n---------------------");
                }
                sb.append("\nTOTAL SIZE=" + size);
            }
            sb.append("]");
            return sb.toString();
        } else {
            return null;
        }
    }

    private class NonPooledCache extends LruCache<K, V> {

        public NonPooledCache(int maxSize) {
            super(maxSize);
        }

        @Override
        protected int sizeOf(K key, V value) {
            return UnrefedPooledCache.this.sizeOf(value);
        }

    }

    @Override
    public void clear() {
        mCache.clear();
        mPool.clear();
    }
}