Chromium Code Reviews| Index: pkg/analyzer/lib/src/dart/analysis/byte_store.dart |
| diff --git a/pkg/analyzer/lib/src/dart/analysis/byte_store.dart b/pkg/analyzer/lib/src/dart/analysis/byte_store.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b852380946efe46c3be754ac39cf03c3397d6b12 |
| --- /dev/null |
| +++ b/pkg/analyzer/lib/src/dart/analysis/byte_store.dart |
| @@ -0,0 +1,61 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'dart:collection'; |
| + |
| +/** |
| + * Store of bytes associated with string keys. |
| + * |
| + * Note that associations are not guaranteed to be persistent. The value |
| + * associated with a key can change or become `null` at any point of time. |
|
Paul Berry
2016/10/23 10:39:19
s/of/in/
scheglov
2016/10/23 20:54:34
Done.
|
| + */ |
| +abstract class ByteStore { |
|
Paul Berry
2016/10/23 10:39:19
Since this is a synchronous interface, it means th
scheglov
2016/10/23 20:54:33
I thought about this.
Maybe even global, LAN or c
Brian Wilkerson
2016/10/25 17:24:12
Those performance measures are consistent with Bob
|
| + /** |
| + * Return the bytes associated with the given [key]. |
| + * Return `null` if the association does not exist. |
| + */ |
| + List<int> get(String key); |
|
Paul Berry
2016/10/23 10:39:19
Since FileByteStore uses the key as a filename, we
scheglov
2016/10/23 20:54:33
Done.
In theory as long as keys are short enough,
|
| + |
| + /** |
| + * Associate the given [bytes] with the [key]. |
| + */ |
| + void put(String key, List<int> bytes); |
| +} |
| + |
| +/** |
| + * [ByteStore] with LRU cache. |
|
Paul Berry
2016/10/23 10:39:19
When I first read this comment it wasn't clear to
scheglov
2016/10/23 20:54:34
Done.
|
| + */ |
| +class MemoryCachingByteStore implements ByteStore { |
| + final ByteStore store; |
| + final int maxEntries; |
|
Paul Berry
2016/10/23 10:39:19
As a future improvement, consider measuring the li
scheglov
2016/10/23 20:54:34
Agreed.
We could look at Google Guava for inspirat
|
| + |
| + final _map = <String, List<int>>{}; |
| + final _keys = new LinkedHashSet<String>(); |
| + |
| + MemoryCachingByteStore(this.store, this.maxEntries); |
| + |
| + @override |
| + List<int> get(String key) { |
| + _keys.remove(key); |
| + _keys.add(key); |
| + _evict(); |
| + return _map.putIfAbsent(key, () => store.get(key)); |
| + } |
| + |
| + @override |
| + void put(String key, List<int> bytes) { |
|
Brian Wilkerson
2016/10/25 17:24:12
Is there a need to be able to remove items from th
scheglov
2016/10/25 18:38:45
AFAIK _evict() ensures that there is a limit on th
|
| + store.put(key, bytes); |
| + _map[key] = bytes; |
| + _keys.add(key); |
| + _evict(); |
| + } |
| + |
| + void _evict() { |
| + if (_keys.length > maxEntries) { |
| + String key = _keys.first; |
| + _keys.remove(key); |
| + _map.remove(key); |
| + } |
| + } |
| +} |