Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: pkg/analysis_server/lib/src/services/index/store/collection.dart

Issue 953913003: Optimize IntArrayToIntMap and ElementCodec.encodeHash(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/analysis_server/lib/src/services/index/store/codec.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 services.src.index.store.collection; 5 library services.src.index.store.collection;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:typed_data'; 8 import 'dart:typed_data' show Uint32List;
9
10 import 'package:analyzer/src/generated/utilities_general.dart';
9 11
10 12
11 /** 13 /**
12 * A hash map with `List<int>` keys and [int] values. 14 * A hash map with `List<int>` keys and [int] values.
13 */ 15 */
14 class IntArrayToIntMap { 16 class IntArrayToIntMap {
15 final Map<Int32List, int> map = new HashMap<Int32List, int>( 17 final Map<Uint32List, int> map = new HashMap<Uint32List, int>(
16 equals: _intArrayEquals, 18 equals: _intArrayEquals,
17 hashCode: _intArrayHashCode); 19 hashCode: _intArrayHashCode);
18 20
19 /** 21 /**
20 * Returns the value for the given [key] or null if [key] is not in the map. 22 * Returns the value for the given [key] or null if [key] is not in the map.
21 */ 23 */
22 int operator [](List<int> key) { 24 int operator [](List<int> key) {
23 Int32List typedKey = _getTypedKey(key); 25 Uint32List typedKey = _getTypedKey(key);
24 return map[typedKey]; 26 return map[typedKey];
25 } 27 }
26 28
27 /** 29 /**
28 * Associates the [key] with the given [value]. 30 * Associates the [key] with the given [value].
29 * 31 *
30 * If the key was already in the map, its associated value is changed. 32 * If the key was already in the map, its associated value is changed.
31 * Otherwise the key-value pair is added to the map. 33 * Otherwise the key-value pair is added to the map.
32 */ 34 */
33 void operator []=(List<int> key, int value) { 35 void operator []=(List<int> key, int value) {
34 Int32List typedKey = _getTypedKey(key); 36 Uint32List typedKey = _getTypedKey(key);
35 map[typedKey] = value; 37 map[typedKey] = value;
36 } 38 }
37 39
38 /** 40 /**
39 * Returns an [Int32List] version of the given `List<int>` key. 41 * Returns an [Uint32List] version of the given `List<int>` key.
40 */ 42 */
41 static Int32List _getTypedKey(List<int> key) { 43 static Uint32List _getTypedKey(List<int> key) {
42 if (key is Int32List) { 44 if (key is Uint32List) {
43 return key; 45 return key;
44 } 46 }
45 return new Int32List.fromList(key); 47 return new Uint32List.fromList(key);
46 } 48 }
47 49
48 static bool _intArrayEquals(List<int> a, List<int> b) { 50 static bool _intArrayEquals(List<int> a, List<int> b) {
49 int length = a.length; 51 int length = a.length;
50 if (length != b.length) { 52 if (length != b.length) {
51 return false; 53 return false;
52 } 54 }
53 for (int i = 0; i < length; i++) { 55 for (int i = 0; i < length; i++) {
54 if (a[i] != b[i]) { 56 if (a[i] != b[i]) {
55 return false; 57 return false;
56 } 58 }
57 } 59 }
58 return true; 60 return true;
59 } 61 }
60 62
61 static int _intArrayHashCode(List<int> key) { 63 static int _intArrayHashCode(List<int> key) {
62 return key.fold(0, (int result, int item) { 64 return key.fold(0, JenkinsSmiHash.combine);
63 return 31 * result + item;
64 });
65 } 65 }
66 } 66 }
67 67
68 68
69 /** 69 /**
70 * A table mapping [int] keys to sets of [int]s. 70 * A table mapping [int] keys to sets of [int]s.
71 */ 71 */
72 class IntToIntSetMap { 72 class IntToIntSetMap {
73 final Map<int, Int32List> _map = new HashMap<int, Int32List>(); 73 final Map<int, Uint32List> _map = new HashMap<int, Uint32List>();
74 74
75 /** 75 /**
76 * The number of key-value pairs in the map. 76 * The number of key-value pairs in the map.
77 */ 77 */
78 int get length => _map.length; 78 int get length => _map.length;
79 79
80 /** 80 /**
81 * Adds the [value] to the set associated with the given [value]. 81 * Adds the [value] to the set associated with the given [value].
82 */ 82 */
83 void add(int key, int value) { 83 void add(int key, int value) {
84 Int32List values = _map[key]; 84 Uint32List values = _map[key];
85 if (values == null) { 85 if (values == null) {
86 values = new Int32List(1); 86 values = new Uint32List(1);
87 values[0] = value; 87 values[0] = value;
88 _map[key] = values; 88 _map[key] = values;
89 } 89 }
90 if (values.indexOf(value) == -1) { 90 if (values.indexOf(value) == -1) {
91 int length = values.length; 91 int length = values.length;
92 Int32List newSet = new Int32List(length + 1); 92 Uint32List newSet = new Uint32List(length + 1);
93 newSet.setRange(0, length, values); 93 newSet.setRange(0, length, values);
94 newSet[length] = value; 94 newSet[length] = value;
95 _map[key] = newSet; 95 _map[key] = newSet;
96 } 96 }
97 } 97 }
98 98
99 /** 99 /**
100 * Removes all pairs from the map. 100 * Removes all pairs from the map.
101 */ 101 */
102 void clear() { 102 void clear() {
103 _map.clear(); 103 _map.clear();
104 } 104 }
105 105
106 /** 106 /**
107 * Returns the set of [int]s for the given [key] or an empty list if [key] is 107 * Returns the set of [int]s for the given [key] or an empty list if [key] is
108 * not in the map. 108 * not in the map.
109 */ 109 */
110 List<int> get(int key) { 110 List<int> get(int key) {
111 List<int> values = _map[key]; 111 List<int> values = _map[key];
112 if (values == null) { 112 if (values == null) {
113 values = <int>[]; 113 values = <int>[];
114 } 114 }
115 return values; 115 return values;
116 } 116 }
117 } 117 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/services/index/store/codec.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698