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

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/linked_hash_map.dart

Issue 1032783003: dart2js: use Es6 maps when available. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor cleanups Created 5 years, 8 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 | « sdk/lib/_internal/compiler/js_lib/collection_patch.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 // Efficient JavaScript based implementation of a linked hash map used as a 5 // Efficient JavaScript based implementation of a linked hash map used as a
6 // backing map for constant maps and the [LinkedHashMap] patch 6 // backing map for constant maps and the [LinkedHashMap] patch
7 7
8 part of _js_helper; 8 part of _js_helper;
9 9
10 const _USE_ES6_MAPS = const bool.fromEnvironment("dart2js.use.es6.maps");
11
10 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap { 12 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap {
11 int _length = 0; 13 int _length = 0;
12 14
13 // The hash map contents are divided into three parts: one part for 15 // The hash map contents are divided into three parts: one part for
14 // string keys, one for numeric keys, and one for the rest. String 16 // string keys, one for numeric keys, and one for the rest. String
15 // and numeric keys map directly to their linked cells, but the rest 17 // and numeric keys map directly to their linked cells, but the rest
16 // of the entries are stored in bucket lists of the form: 18 // of the entries are stored in bucket lists of the form:
17 // 19 //
18 // [cell-0, cell-1, ...] 20 // [cell-0, cell-1, ...]
19 // 21 //
20 // where all keys in the same bucket share the same hash code. 22 // where all keys in the same bucket share the same hash code.
21 var _strings; 23 var _strings;
22 var _nums; 24 var _nums;
23 var _rest; 25 var _rest;
24 26
25 // The keys and values are stored in cells that are linked together 27 // The keys and values are stored in cells that are linked together
26 // to form a double linked list. 28 // to form a double linked list.
27 LinkedHashMapCell _first; 29 LinkedHashMapCell _first;
28 LinkedHashMapCell _last; 30 LinkedHashMapCell _last;
29 31
30 // We track the number of modifications done to the key set of the 32 // We track the number of modifications done to the key set of the
31 // hash map to be able to throw when the map is modified while being 33 // hash map to be able to throw when the map is modified while being
32 // iterated over. 34 // iterated over.
33 int _modifications = 0; 35 int _modifications = 0;
34 36
37 static bool get _supportsEs6Maps {
38 return JS('returns:bool;depends:none;effects:none;',
39 'typeof Map != "undefined"');
40 }
41
35 JsLinkedHashMap(); 42 JsLinkedHashMap();
36 43
44 /// If ES6 Maps are available returns a linked hash-map backed by an ES6 Map.
45 factory JsLinkedHashMap.es6() {
46 if (_USE_ES6_MAPS && JsLinkedHashMap._supportsEs6Maps) {
herhut 2015/04/13 12:58:33 Type inference will not currently understand this
47 return new Es6LinkedHashMap<K, V>();
48 } else {
49 return new JsLinkedHashMap<K, V>();
50 }
51 }
37 52
38 int get length => _length; 53 int get length => _length;
39 bool get isEmpty => _length == 0; 54 bool get isEmpty => _length == 0;
40 bool get isNotEmpty => !isEmpty; 55 bool get isNotEmpty => !isEmpty;
41 56
42 Iterable<K> get keys { 57 Iterable<K> get keys {
43 return new LinkedHashMapKeyIterable<K>(this); 58 return new LinkedHashMapKeyIterable<K>(this);
44 } 59 }
45 60
46 Iterable<V> get values { 61 Iterable<V> get values {
47 return new MappedIterable<K, V>(keys, (each) => this[each]); 62 return new MappedIterable<K, V>(keys, (each) => this[each]);
48 } 63 }
49 64
50 bool containsKey(Object key) { 65 bool containsKey(Object key) {
51 if (_isStringKey(key)) { 66 if (_isStringKey(key)) {
52 var strings = _strings; 67 var strings = _strings;
53 if (strings == null) return false; 68 if (strings == null) return false;
54 LinkedHashMapCell cell = _getTableEntry(strings, key); 69 return _containsTableEntry(strings, key);
55 return cell != null;
56 } else if (_isNumericKey(key)) { 70 } else if (_isNumericKey(key)) {
57 var nums = _nums; 71 var nums = _nums;
58 if (nums == null) return false; 72 if (nums == null) return false;
59 LinkedHashMapCell cell = _getTableEntry(nums, key); 73 return _containsTableEntry(nums, key);
60 return cell != null;
61 } else { 74 } else {
62 return internalContainsKey(key); 75 return internalContainsKey(key);
63 } 76 }
64 } 77 }
65 78
66 bool internalContainsKey(Object key) { 79 bool internalContainsKey(Object key) {
67 var rest = _rest; 80 var rest = _rest;
68 if (rest == null) return false; 81 if (rest == null) return false;
69 var bucket = _getBucket(rest, key); 82 var bucket = _getBucket(rest, key);
70 return internalFindBucketIndex(bucket, key) >= 0; 83 return internalFindBucketIndex(bucket, key) >= 0;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 _addHashTableEntry(nums, key, value); 130 _addHashTableEntry(nums, key, value);
118 } else { 131 } else {
119 internalSet(key, value); 132 internalSet(key, value);
120 } 133 }
121 } 134 }
122 135
123 void internalSet(K key, V value) { 136 void internalSet(K key, V value) {
124 var rest = _rest; 137 var rest = _rest;
125 if (rest == null) _rest = rest = _newHashTable(); 138 if (rest == null) _rest = rest = _newHashTable();
126 var hash = internalComputeHashCode(key); 139 var hash = internalComputeHashCode(key);
127 var bucket = JS('var', '#[#]', rest, hash); 140 var bucket = _getTableEntry(rest, hash);
128 if (bucket == null) { 141 if (bucket == null) {
129 LinkedHashMapCell cell = _newLinkedCell(key, value); 142 LinkedHashMapCell cell = _newLinkedCell(key, value);
130 _setTableEntry(rest, hash, JS('var', '[#]', cell)); 143 _setTableEntry(rest, hash, JS('var', '[#]', cell));
131 } else { 144 } else {
132 int index = internalFindBucketIndex(bucket, key); 145 int index = internalFindBucketIndex(bucket, key);
133 if (index >= 0) { 146 if (index >= 0) {
134 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); 147 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index);
135 cell.hashMapCellValue = value; 148 cell.hashMapCellValue = value;
136 } else { 149 } else {
137 LinkedHashMapCell cell = _newLinkedCell(key, value); 150 LinkedHashMapCell cell = _newLinkedCell(key, value);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 assert(cell == _last); 259 assert(cell == _last);
247 _last = previous; 260 _last = previous;
248 } else { 261 } else {
249 next._previous = previous; 262 next._previous = previous;
250 } 263 }
251 _length--; 264 _length--;
252 _modified(); 265 _modified();
253 } 266 }
254 267
255 static bool _isStringKey(var key) { 268 static bool _isStringKey(var key) {
256 return key is String && key != '__proto__'; 269 return key is String;
herhut 2015/04/13 12:58:33 Nice!
257 } 270 }
258 271
259 static bool _isNumericKey(var key) { 272 static bool _isNumericKey(var key) {
260 // Only treat unsigned 30-bit integers as numeric keys. This way, 273 // Only treat unsigned 30-bit integers as numeric keys. This way,
261 // we avoid converting them to strings when we use them as keys in 274 // we avoid converting them to strings when we use them as keys in
262 // the JavaScript hash table object. 275 // the JavaScript hash table object.
263 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key); 276 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key);
264 } 277 }
265 278
266 int internalComputeHashCode(var key) { 279 int internalComputeHashCode(var key) {
267 // We force the hash codes to be unsigned 30-bit integers to avoid 280 // We force the hash codes to be unsigned 30-bit integers to avoid
268 // issues with problematic keys like '__proto__'. Another option 281 // issues with problematic keys like '__proto__'. Another option
269 // would be to throw an exception if the hash code isn't a number. 282 // would be to throw an exception if the hash code isn't a number.
270 return JS('int', '# & 0x3ffffff', key.hashCode); 283 return JS('int', '# & 0x3ffffff', key.hashCode);
271 } 284 }
272 285
273 static _getTableEntry(var table, var key) {
274 return JS('var', '#[#]', table, key);
275 }
276
277 static void _setTableEntry(var table, var key, var value) {
278 assert(value != null);
279 JS('void', '#[#] = #', table, key, value);
280 }
281
282 static void _deleteTableEntry(var table, var key) {
283 JS('void', 'delete #[#]', table, key);
284 }
285
286 List _getBucket(var table, var key) { 286 List _getBucket(var table, var key) {
287 var hash = internalComputeHashCode(key); 287 var hash = internalComputeHashCode(key);
288 return JS('var', '#[#]', table, hash); 288 return _getTableEntry(table, hash);
289 } 289 }
290 290
291 int internalFindBucketIndex(var bucket, var key) { 291 int internalFindBucketIndex(var bucket, var key) {
292 if (bucket == null) return -1; 292 if (bucket == null) return -1;
293 int length = JS('int', '#.length', bucket); 293 int length = JS('int', '#.length', bucket);
294 for (int i = 0; i < length; i++) { 294 for (int i = 0; i < length; i++) {
295 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); 295 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i);
296 if (cell.hashMapCellKey == key) return i; 296 if (cell.hashMapCellKey == key) return i;
297 } 297 }
298 return -1; 298 return -1;
299 } 299 }
300 300
301 static _newHashTable() { 301 String toString() => Maps.mapToString(this);
302
303 _getTableEntry(var table, var key) {
304 return JS('var', '#[#]', table, key);
305 }
306
307 void _setTableEntry(var table, var key, var value) {
308 assert(value != null);
309 JS('void', '#[#] = #', table, key, value);
310 }
311
312 void _deleteTableEntry(var table, var key) {
313 JS('void', 'delete #[#]', table, key);
314 }
315
316 bool _containsTableEntry(var table, var key) {
317 LinkedHashMapCell cell = _getTableEntry(table, key);
318 return cell != null;
319 }
320
321 _newHashTable() {
302 // Create a new JavaScript object to be used as a hash table. Use 322 // Create a new JavaScript object to be used as a hash table. Use
303 // Object.create to avoid the properties on Object.prototype 323 // Object.create to avoid the properties on Object.prototype
304 // showing up as entries. 324 // showing up as entries.
305 var table = JS('var', 'Object.create(null)'); 325 var table = JS('var', 'Object.create(null)');
306 // Attempt to force the hash table into 'dictionary' mode by 326 // Attempt to force the hash table into 'dictionary' mode by
307 // adding a property to it and deleting it again. 327 // adding a property to it and deleting it again.
308 var temporaryKey = '<non-identifier-key>'; 328 var temporaryKey = '<non-identifier-key>';
309 _setTableEntry(table, temporaryKey, table); 329 _setTableEntry(table, temporaryKey, table);
310 _deleteTableEntry(table, temporaryKey); 330 _deleteTableEntry(table, temporaryKey);
311 return table; 331 return table;
312 } 332 }
333 }
313 334
314 String toString() => Maps.mapToString(this); 335 class Es6LinkedHashMap<K, V> extends JsLinkedHashMap<K, V> {
336
337 @override
338 _getTableEntry(var table, var key) {
339 return JS('var', '#.get(#)', table, key);
340 }
341
342 @override
343 void _setTableEntry(var table, var key, var value) {
344 JS('void', '#.set(#, #)', table, key, value);
345 }
346
347 @override
348 void _deleteTableEntry(var table, var key) {
349 JS('void', '#.delete(#)', table, key);
350 }
351
352 @override
353 bool _containsTableEntry(var table, var key) {
354 return JS('bool', '#.has(#)', table, key);
355 }
356
357 @override
358 _newHashTable() {
359 return JS('var', 'new Map()');
360 }
315 } 361 }
316 362
317 class LinkedHashMapCell { 363 class LinkedHashMapCell {
318 final hashMapCellKey; 364 final hashMapCellKey;
319 var hashMapCellValue; 365 var hashMapCellValue;
320 366
321 LinkedHashMapCell _next; 367 LinkedHashMapCell _next;
322 LinkedHashMapCell _previous; 368 LinkedHashMapCell _previous;
323 369
324 LinkedHashMapCell(this.hashMapCellKey, this.hashMapCellValue); 370 LinkedHashMapCell(this.hashMapCellKey, this.hashMapCellValue);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 } else if (_cell == null) { 417 } else if (_cell == null) {
372 _current = null; 418 _current = null;
373 return false; 419 return false;
374 } else { 420 } else {
375 _current = _cell.hashMapCellKey; 421 _current = _cell.hashMapCellKey;
376 _cell = _cell._next; 422 _cell = _cell._next;
377 return true; 423 return true;
378 } 424 }
379 } 425 }
380 } 426 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/collection_patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698