| 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 library analyzer.src.context.cache; | 5 library analyzer.src.context.cache; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/generated/engine.dart' | 9 import 'package:analyzer/src/generated/engine.dart' |
| 10 show AnalysisEngine, CacheState, InternalAnalysisContext, RetentionPriority; | 10 show AnalysisEngine, CacheState, InternalAnalysisContext, RetentionPriority; |
| 11 import 'package:analyzer/src/generated/java_engine.dart'; | 11 import 'package:analyzer/src/generated/java_engine.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/generated/utilities_collection.dart'; | 13 import 'package:analyzer/src/generated/utilities_collection.dart'; |
| 14 import 'package:analyzer/src/generated/utilities_general.dart'; | 14 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 15 import 'package:analyzer/src/task/model.dart'; |
| 15 import 'package:analyzer/task/model.dart'; | 16 import 'package:analyzer/task/model.dart'; |
| 16 | 17 |
| 17 /** | 18 /** |
| 18 * Return `true` if the given [target] is a priority one. | 19 * Return `true` if the given [target] is a priority one. |
| 19 */ | 20 */ |
| 20 typedef bool IsPriorityAnalysisTarget(AnalysisTarget target); | 21 typedef bool IsPriorityAnalysisTarget(AnalysisTarget target); |
| 21 | 22 |
| 22 /** | 23 /** |
| 23 * An LRU cache of results produced by analysis. | 24 * An LRU cache of results produced by analysis. |
| 24 */ | 25 */ |
| (...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 /** | 610 /** |
| 610 * If [currentSize] is already less than [maxSize], returns an empty list. | 611 * If [currentSize] is already less than [maxSize], returns an empty list. |
| 611 * Otherwise returns [TargetedResult]s to flush from the cache to make | 612 * Otherwise returns [TargetedResult]s to flush from the cache to make |
| 612 * [currentSize] less or equal to [maxSize]. | 613 * [currentSize] less or equal to [maxSize]. |
| 613 * | 614 * |
| 614 * Results for priority files are never flushed, so this method might leave | 615 * Results for priority files are never flushed, so this method might leave |
| 615 * [currentSize] greater than [maxSize]. | 616 * [currentSize] greater than [maxSize]. |
| 616 */ | 617 */ |
| 617 List<TargetedResult> flushToSize() { | 618 List<TargetedResult> flushToSize() { |
| 618 // If still under the cap, done. | 619 // If still under the cap, done. |
| 619 if (maxSize == -1 || currentSize <= maxSize) { | 620 if (currentSize <= maxSize) { |
| 620 return TargetedResult.EMPTY_LIST; | 621 return TargetedResult.EMPTY_LIST; |
| 621 } | 622 } |
| 622 // Flush results until we are under the cap. | 623 // Flush results until we are under the cap. |
| 623 List<TargetedResult> resultsToFlush = <TargetedResult>[]; | 624 List<TargetedResult> resultsToFlush = <TargetedResult>[]; |
| 624 for (TargetedResult result in recentlyUsed) { | 625 for (TargetedResult result in recentlyUsed) { |
| 625 if (isPriorityAnalysisTarget(result.target)) { | 626 if (isPriorityAnalysisTarget(result.target)) { |
| 626 continue; | 627 continue; |
| 627 } | 628 } |
| 628 resultsToFlush.add(result); | 629 resultsToFlush.add(result); |
| 629 int size = resultSizeMap.remove(result); | 630 int size = resultSizeMap.remove(result); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 650 */ | 651 */ |
| 651 List<TargetedResult> madeIdle() { | 652 List<TargetedResult> madeIdle() { |
| 652 maxSize = maxIdleSize; | 653 maxSize = maxIdleSize; |
| 653 return flushToSize(); | 654 return flushToSize(); |
| 654 } | 655 } |
| 655 | 656 |
| 656 /** | 657 /** |
| 657 * Records that the given [result] was just read from the cache. | 658 * Records that the given [result] was just read from the cache. |
| 658 */ | 659 */ |
| 659 void resultAccessed(TargetedResult result) { | 660 void resultAccessed(TargetedResult result) { |
| 660 if (maxSize <= 0) { | |
| 661 return; | |
| 662 } | |
| 663 if (recentlyUsed.remove(result)) { | 661 if (recentlyUsed.remove(result)) { |
| 664 recentlyUsed.add(result); | 662 recentlyUsed.add(result); |
| 665 } | 663 } |
| 666 } | 664 } |
| 667 | 665 |
| 668 /** | 666 /** |
| 669 * Records that the given [newResult] and [newValue] were stored to the cache. | 667 * Records that the given [newResult] and [newValue] were stored to the cache. |
| 670 * Returns [TargetedResult]s that should be flushed from the cache. | 668 * Returns [TargetedResult]s that should be flushed from the cache. |
| 671 */ | 669 */ |
| 672 List<TargetedResult> resultStored(TargetedResult newResult, T newValue) { | 670 List<TargetedResult> resultStored(TargetedResult newResult, T newValue) { |
| 673 if (maxSize <= 0) { | |
| 674 return TargetedResult.EMPTY_LIST; | |
| 675 } | |
| 676 if (!recentlyUsed.remove(newResult)) { | 671 if (!recentlyUsed.remove(newResult)) { |
| 677 int size = policy.measure(newValue); | 672 int size = policy.measure(newValue); |
| 678 resultSizeMap[newResult] = size; | 673 resultSizeMap[newResult] = size; |
| 679 currentSize += size; | 674 currentSize += size; |
| 680 } | 675 } |
| 681 recentlyUsed.add(newResult); | 676 recentlyUsed.add(newResult); |
| 682 return flushToSize(); | 677 return flushToSize(); |
| 683 } | 678 } |
| 684 | 679 |
| 685 /** | 680 /** |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 } else { | 832 } else { |
| 838 return entry._getResultData(result.result); | 833 return entry._getResultData(result.result); |
| 839 } | 834 } |
| 840 } | 835 } |
| 841 | 836 |
| 842 /** | 837 /** |
| 843 * Return the [CacheFlushManager] for the given [descriptor], not `null`. | 838 * Return the [CacheFlushManager] for the given [descriptor], not `null`. |
| 844 */ | 839 */ |
| 845 CacheFlushManager _getFlushManager(ResultDescriptor descriptor) { | 840 CacheFlushManager _getFlushManager(ResultDescriptor descriptor) { |
| 846 ResultCachingPolicy policy = descriptor.cachingPolicy; | 841 ResultCachingPolicy policy = descriptor.cachingPolicy; |
| 847 return _flushManagerMap.putIfAbsent( | 842 if (identical(policy, DEFAULT_CACHING_POLICY)) { |
| 848 policy, () => new CacheFlushManager(policy, _isPriorityAnalysisTarget)); | 843 return UnlimitedCacheFlushManager.INSTANCE; |
| 844 } |
| 845 CacheFlushManager manager = _flushManagerMap[policy]; |
| 846 if (manager == null) { |
| 847 manager = new CacheFlushManager(policy, _isPriorityAnalysisTarget); |
| 848 _flushManagerMap[policy] = manager; |
| 849 } |
| 850 return manager; |
| 849 } | 851 } |
| 850 | 852 |
| 851 bool _isPriorityAnalysisTarget(AnalysisTarget target) { | 853 bool _isPriorityAnalysisTarget(AnalysisTarget target) { |
| 852 return context.priorityTargets.contains(target); | 854 return context.priorityTargets.contains(target); |
| 853 } | 855 } |
| 854 | 856 |
| 855 /** | 857 /** |
| 856 * If the given [target] is a [Source], removes it from [_sources]. | 858 * If the given [target] is a [Source], removes it from [_sources]. |
| 857 */ | 859 */ |
| 858 void _removeIfSource(AnalysisTarget target) { | 860 void _removeIfSource(AnalysisTarget target) { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 class UniversalCachePartition extends CachePartition { | 980 class UniversalCachePartition extends CachePartition { |
| 979 /** | 981 /** |
| 980 * Initialize a newly created cache partition, belonging to the given | 982 * Initialize a newly created cache partition, belonging to the given |
| 981 * [context]. | 983 * [context]. |
| 982 */ | 984 */ |
| 983 UniversalCachePartition(InternalAnalysisContext context) : super(context); | 985 UniversalCachePartition(InternalAnalysisContext context) : super(context); |
| 984 | 986 |
| 985 @override | 987 @override |
| 986 bool isResponsibleFor(AnalysisTarget target) => true; | 988 bool isResponsibleFor(AnalysisTarget target) => true; |
| 987 } | 989 } |
| 990 |
| 991 /** |
| 992 * [CacheFlushManager] that does nothing, results are never flushed. |
| 993 */ |
| 994 class UnlimitedCacheFlushManager extends CacheFlushManager { |
| 995 static final CacheFlushManager INSTANCE = new UnlimitedCacheFlushManager(); |
| 996 |
| 997 UnlimitedCacheFlushManager() : super(DEFAULT_CACHING_POLICY, (_) => false); |
| 998 |
| 999 @override |
| 1000 void resultAccessed(TargetedResult result) {} |
| 1001 |
| 1002 @override |
| 1003 List<TargetedResult> resultStored(TargetedResult newResult, newValue) { |
| 1004 return TargetedResult.EMPTY_LIST; |
| 1005 } |
| 1006 |
| 1007 @override |
| 1008 void targetRemoved(AnalysisTarget target) {} |
| 1009 } |
| OLD | NEW |