Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:collection'; | 5 import 'dart:collection'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Store of bytes associated with string keys. | 8 * Store of bytes associated with string keys. |
| 9 * | 9 * |
| 10 * Each key must be not longer than 100 characters and consist of only `[a-z]`, | 10 * Each key must be not longer than 100 characters and consist of only `[a-z]`, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 List<int> get(String key); | 24 List<int> get(String key); |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Associate the given [bytes] with the [key]. | 27 * Associate the given [bytes] with the [key]. |
| 28 */ | 28 */ |
| 29 void put(String key, List<int> bytes); | 29 void put(String key, List<int> bytes); |
| 30 } | 30 } |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * A wrapper around [ByteStore] which adds an in-memory LRU cache to it. | 33 * A wrapper around [ByteStore] which adds an in-memory LRU cache to it. |
| 34 * | |
| 35 * TODO(scheglov) Consider implementing size and/or time eviction policies. | |
| 36 */ | 34 */ |
| 37 class MemoryCachingByteStore implements ByteStore { | 35 class MemoryCachingByteStore implements ByteStore { |
| 38 final ByteStore store; | 36 final ByteStore _store; |
| 39 final int maxEntries; | 37 final int _maxSizeBytes; |
| 40 | 38 |
| 41 final _map = <String, List<int>>{}; | 39 final HashMap<String, List<int>> _map = new HashMap<String, List<int>>(); |
|
Paul Berry
2016/11/09 20:17:36
Why change this to a HashMap? Don't we lose the "
scheglov
2016/11/09 20:21:33
Ouch.
Thanks.
That's a mistake.
Fixed.
Paul Berry
2016/11/09 20:22:15
Ok, with that fixed, lgtm.
| |
| 42 final _keys = new LinkedHashSet<String>(); | 40 int _currentSizeBytes = 0; |
| 43 | 41 |
| 44 MemoryCachingByteStore(this.store, this.maxEntries); | 42 MemoryCachingByteStore(this._store, this._maxSizeBytes); |
| 45 | 43 |
| 46 @override | 44 @override |
| 47 List<int> get(String key) { | 45 List<int> get(String key) { |
| 48 _keys.remove(key); | 46 List<int> bytes = _map[key]; |
| 49 _keys.add(key); | 47 if (bytes == null) { |
| 50 _evict(); | 48 bytes = _store.get(key); |
| 51 return _map.putIfAbsent(key, () => store.get(key)); | 49 _map[key] = bytes; |
| 50 _currentSizeBytes += bytes?.length ?? 0; | |
| 51 _evict(); | |
| 52 } | |
| 53 return bytes; | |
| 52 } | 54 } |
| 53 | 55 |
| 54 @override | 56 @override |
| 55 void put(String key, List<int> bytes) { | 57 void put(String key, List<int> bytes) { |
| 56 store.put(key, bytes); | 58 _store.put(key, bytes); |
| 59 _currentSizeBytes -= _map[key]?.length ?? 0; | |
| 57 _map[key] = bytes; | 60 _map[key] = bytes; |
| 58 _keys.add(key); | 61 _currentSizeBytes += bytes.length; |
| 59 _evict(); | 62 _evict(); |
| 60 } | 63 } |
| 61 | 64 |
| 62 void _evict() { | 65 void _evict() { |
| 63 if (_keys.length > maxEntries) { | 66 while (_currentSizeBytes > _maxSizeBytes) { |
| 64 String key = _keys.first; | 67 if (_map.isEmpty) { |
| 65 _keys.remove(key); | 68 break; |
| 66 _map.remove(key); | 69 } |
| 70 String key = _map.keys.first; | |
| 71 List<int> bytes = _map.remove(key); | |
| 72 _currentSizeBytes -= bytes.length; | |
| 67 } | 73 } |
| 68 } | 74 } |
| 69 } | 75 } |
| OLD | NEW |