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 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; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 // for (int i = 0; i < count; i++) { | 53 // for (int i = 0; i < count; i++) { |
| 54 // CachePartition partition = _partitions[i]; | 54 // CachePartition partition = _partitions[i]; |
| 55 // data[i] = new AnalysisContextStatisticsImpl_PartitionDataImpl( | 55 // data[i] = new AnalysisContextStatisticsImpl_PartitionDataImpl( |
| 56 // partition.astSize, | 56 // partition.astSize, |
| 57 // partition.map.length); | 57 // partition.map.length); |
| 58 // } | 58 // } |
| 59 // return data; | 59 // return data; |
| 60 // } | 60 // } |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * Return an iterator returning all of the [Source] targets. | |
| 64 */ | |
| 65 Iterable<Source> get sources { | |
| 66 return _partitions | |
| 67 .map((CachePartition partition) => partition._sources) | |
| 68 .expand((Iterable<Source> sources) => sources); | |
| 69 } | |
| 70 | |
| 71 /** | |
| 63 * Return the entry associated with the given [target]. | 72 * Return the entry associated with the given [target]. |
| 64 */ | 73 */ |
| 65 CacheEntry get(AnalysisTarget target) { | 74 CacheEntry get(AnalysisTarget target) { |
| 66 int count = _partitions.length; | 75 int count = _partitions.length; |
| 67 for (int i = 0; i < count; i++) { | 76 for (int i = 0; i < count; i++) { |
| 68 CachePartition partition = _partitions[i]; | 77 CachePartition partition = _partitions[i]; |
| 69 if (partition.isResponsibleFor(target)) { | 78 if (partition.isResponsibleFor(target)) { |
| 70 return partition.get(target); | 79 return partition.get(target); |
| 71 } | 80 } |
| 72 } | 81 } |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 thisData.dependentResults = <TargetedResult>[]; | 457 thisData.dependentResults = <TargetedResult>[]; |
| 449 dependentResults.forEach((TargetedResult dependentResult) { | 458 dependentResults.forEach((TargetedResult dependentResult) { |
| 450 CacheEntry entry = _partition.get(dependentResult.target); | 459 CacheEntry entry = _partition.get(dependentResult.target); |
| 451 if (entry != null) { | 460 if (entry != null) { |
| 452 entry._invalidate(dependentResult.result); | 461 entry._invalidate(dependentResult.result); |
| 453 } | 462 } |
| 454 }); | 463 }); |
| 455 // If empty, remove the entry altogether. | 464 // If empty, remove the entry altogether. |
| 456 if (_resultMap.isEmpty) { | 465 if (_resultMap.isEmpty) { |
| 457 _partition._targetMap.remove(target); | 466 _partition._targetMap.remove(target); |
| 467 _partition._removeSource(target); | |
| 458 } | 468 } |
| 459 } | 469 } |
| 460 | 470 |
| 461 /** | 471 /** |
| 462 * Invalidates all the results of this entry, with propagation. | 472 * Invalidates all the results of this entry, with propagation. |
| 463 */ | 473 */ |
| 464 void _invalidateAll() { | 474 void _invalidateAll() { |
| 465 List<ResultDescriptor> results = _resultMap.keys.toList(); | 475 List<ResultDescriptor> results = _resultMap.keys.toList(); |
| 466 for (ResultDescriptor result in results) { | 476 for (ResultDescriptor result in results) { |
| 467 _invalidate(result); | 477 _invalidate(result); |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 700 new HashMap<ResultCachingPolicy, CacheFlushManager>(); | 710 new HashMap<ResultCachingPolicy, CacheFlushManager>(); |
| 701 | 711 |
| 702 /** | 712 /** |
| 703 * A table mapping the targets belonging to this partition to the information | 713 * A table mapping the targets belonging to this partition to the information |
| 704 * known about those targets. | 714 * known about those targets. |
| 705 */ | 715 */ |
| 706 HashMap<AnalysisTarget, CacheEntry> _targetMap = | 716 HashMap<AnalysisTarget, CacheEntry> _targetMap = |
| 707 new HashMap<AnalysisTarget, CacheEntry>(); | 717 new HashMap<AnalysisTarget, CacheEntry>(); |
| 708 | 718 |
| 709 /** | 719 /** |
| 720 * A set of the [Source] targets. | |
| 721 */ | |
| 722 final HashSet<Source> _sources = new HashSet<Source>(); | |
| 723 | |
| 724 /** | |
| 710 * Initialize a newly created cache partition, belonging to the given | 725 * Initialize a newly created cache partition, belonging to the given |
| 711 * [context]. | 726 * [context]. |
| 712 */ | 727 */ |
| 713 CachePartition(this.context); | 728 CachePartition(this.context); |
| 714 | 729 |
| 715 /** | 730 /** |
| 716 * Return a table mapping the targets known to the context to the information | 731 * Return a table mapping the targets known to the context to the information |
| 717 * known about the target. | 732 * known about the target. |
| 718 * | 733 * |
| 719 * <b>Note:</b> This method is only visible for use by [AnalysisCache] and | 734 * <b>Note:</b> This method is only visible for use by [AnalysisCache] and |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 743 */ | 758 */ |
| 744 void put(CacheEntry entry) { | 759 void put(CacheEntry entry) { |
| 745 AnalysisTarget target = entry.target; | 760 AnalysisTarget target = entry.target; |
| 746 if (entry._partition != null) { | 761 if (entry._partition != null) { |
| 747 throw new StateError( | 762 throw new StateError( |
| 748 'The entry for $target is already in ${entry._partition}'); | 763 'The entry for $target is already in ${entry._partition}'); |
| 749 } | 764 } |
| 750 entry._partition = this; | 765 entry._partition = this; |
| 751 entry.fixExceptionState(); | 766 entry.fixExceptionState(); |
| 752 _targetMap[target] = entry; | 767 _targetMap[target] = entry; |
| 768 _addSource(target); | |
| 753 } | 769 } |
| 754 | 770 |
| 755 /** | 771 /** |
| 756 * Remove all information related to the given [target] from this cache. | 772 * Remove all information related to the given [target] from this cache. |
| 757 */ | 773 */ |
| 758 void remove(AnalysisTarget target) { | 774 void remove(AnalysisTarget target) { |
| 759 for (CacheFlushManager flushManager in _flushManagerMap.values) { | 775 for (CacheFlushManager flushManager in _flushManagerMap.values) { |
| 760 flushManager.targetRemoved(target); | 776 flushManager.targetRemoved(target); |
| 761 } | 777 } |
| 762 CacheEntry entry = _targetMap.remove(target); | 778 CacheEntry entry = _targetMap.remove(target); |
| 763 if (entry != null) { | 779 if (entry != null) { |
| 764 entry._invalidateAll(); | 780 entry._invalidateAll(); |
| 765 } | 781 } |
| 782 _removeSource(target); | |
| 766 } | 783 } |
| 767 | 784 |
| 768 /** | 785 /** |
| 769 * Records that a value of the result described by the given [descriptor] | 786 * Records that a value of the result described by the given [descriptor] |
| 770 * for the given [target] was just read from the cache. | 787 * for the given [target] was just read from the cache. |
| 771 */ | 788 */ |
| 772 void resultAccessed(AnalysisTarget target, ResultDescriptor descriptor) { | 789 void resultAccessed(AnalysisTarget target, ResultDescriptor descriptor) { |
| 773 CacheFlushManager flushManager = _getFlushManager(descriptor); | 790 CacheFlushManager flushManager = _getFlushManager(descriptor); |
| 774 TargetedResult result = new TargetedResult(target, descriptor); | 791 TargetedResult result = new TargetedResult(target, descriptor); |
| 775 flushManager.resultAccessed(result); | 792 flushManager.resultAccessed(result); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 791 } | 808 } |
| 792 } | 809 } |
| 793 } | 810 } |
| 794 } | 811 } |
| 795 | 812 |
| 796 /** | 813 /** |
| 797 * Return the number of targets that are mapped to cache entries. | 814 * Return the number of targets that are mapped to cache entries. |
| 798 */ | 815 */ |
| 799 int size() => _targetMap.length; | 816 int size() => _targetMap.length; |
| 800 | 817 |
| 818 /** | |
| 819 * If the given [target] is a [Source], adds it to [_sources]. | |
| 820 */ | |
| 821 void _addSource(AnalysisTarget target) { | |
|
Brian Wilkerson
2015/05/21 13:52:31
I was confused when I first saw this method being
| |
| 822 if (target is Source) { | |
| 823 _sources.add(target); | |
| 824 } | |
| 825 } | |
| 826 | |
| 801 ResultData _getDataFor(TargetedResult result, {bool orNull: false}) { | 827 ResultData _getDataFor(TargetedResult result, {bool orNull: false}) { |
| 802 CacheEntry entry = context.analysisCache.get(result.target); | 828 CacheEntry entry = context.analysisCache.get(result.target); |
| 803 if (orNull) { | 829 if (orNull) { |
| 804 return entry != null ? entry._resultMap[result.result] : null; | 830 return entry != null ? entry._resultMap[result.result] : null; |
| 805 } else { | 831 } else { |
| 806 return entry._getResultData(result.result); | 832 return entry._getResultData(result.result); |
| 807 } | 833 } |
| 808 } | 834 } |
| 809 | 835 |
| 810 /** | 836 /** |
| 811 * Return the [CacheFlushManager] for the given [descriptor], not `null`. | 837 * Return the [CacheFlushManager] for the given [descriptor], not `null`. |
| 812 */ | 838 */ |
| 813 CacheFlushManager _getFlushManager(ResultDescriptor descriptor) { | 839 CacheFlushManager _getFlushManager(ResultDescriptor descriptor) { |
| 814 ResultCachingPolicy policy = descriptor.cachingPolicy; | 840 ResultCachingPolicy policy = descriptor.cachingPolicy; |
| 815 return _flushManagerMap.putIfAbsent( | 841 return _flushManagerMap.putIfAbsent( |
| 816 policy, () => new CacheFlushManager(policy, _isPriorityAnalysisTarget)); | 842 policy, () => new CacheFlushManager(policy, _isPriorityAnalysisTarget)); |
| 817 } | 843 } |
| 818 | 844 |
| 819 bool _isPriorityAnalysisTarget(AnalysisTarget target) { | 845 bool _isPriorityAnalysisTarget(AnalysisTarget target) { |
| 820 return context.priorityTargets.contains(target); | 846 return context.priorityTargets.contains(target); |
| 821 } | 847 } |
| 848 | |
| 849 /** | |
| 850 * If the given [target] is a [Source], removes it from [_sources]. | |
| 851 */ | |
| 852 void _removeSource(AnalysisTarget target) { | |
| 853 if (target is Source) { | |
| 854 _sources.remove(target); | |
| 855 } | |
| 856 } | |
| 822 } | 857 } |
| 823 | 858 |
| 824 /** | 859 /** |
| 825 * The data about a single analysis result that is stored in a [CacheEntry]. | 860 * The data about a single analysis result that is stored in a [CacheEntry]. |
| 826 */ | 861 */ |
| 827 // TODO(brianwilkerson) Consider making this a generic class so that the value | 862 // TODO(brianwilkerson) Consider making this a generic class so that the value |
| 828 // can be typed. | 863 // can be typed. |
| 829 class ResultData { | 864 class ResultData { |
| 830 /** | 865 /** |
| 831 * The [ResultDescriptor] this result is for. | 866 * The [ResultDescriptor] this result is for. |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 937 class UniversalCachePartition extends CachePartition { | 972 class UniversalCachePartition extends CachePartition { |
| 938 /** | 973 /** |
| 939 * Initialize a newly created cache partition, belonging to the given | 974 * Initialize a newly created cache partition, belonging to the given |
| 940 * [context]. | 975 * [context]. |
| 941 */ | 976 */ |
| 942 UniversalCachePartition(InternalAnalysisContext context) : super(context); | 977 UniversalCachePartition(InternalAnalysisContext context) : super(context); |
| 943 | 978 |
| 944 @override | 979 @override |
| 945 bool isResponsibleFor(AnalysisTarget target) => true; | 980 bool isResponsibleFor(AnalysisTarget target) => true; |
| 946 } | 981 } |
| OLD | NEW |