Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Efficient JavaScript based implementation of a linked hash map used as a | |
| 6 // backing map for constant maps and the [LinkedHashMap] patch | |
| 7 | |
| 8 part of dart._js_helper; | |
| 9 | |
| 10 // DDC-specific, never use ES6 maps | |
|
sra1
2016/05/09 23:16:40
Why not always?
ES6 maps are used in many places i
| |
| 11 //const _USE_ES6_MAPS = const bool.fromEnvironment("dart2js.use.es6.maps"); | |
| 12 | |
| 13 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap { | |
| 14 int _length = 0; | |
| 15 | |
| 16 // The hash map contents are divided into three parts: one part for | |
| 17 // string keys, one for numeric keys, and one for the rest. String | |
| 18 // and numeric keys map directly to their linked cells, but the rest | |
| 19 // of the entries are stored in bucket lists of the form: | |
| 20 // | |
| 21 // [cell-0, cell-1, ...] | |
| 22 // | |
| 23 // where all keys in the same bucket share the same hash code. | |
| 24 var _strings; | |
| 25 var _nums; | |
| 26 var _rest; | |
| 27 | |
| 28 // The keys and values are stored in cells that are linked together | |
| 29 // to form a double linked list. | |
| 30 LinkedHashMapCell _first; | |
| 31 LinkedHashMapCell _last; | |
| 32 | |
| 33 // We track the number of modifications done to the key set of the | |
| 34 // hash map to be able to throw when the map is modified while being | |
| 35 // iterated over. | |
| 36 int _modifications = 0; | |
| 37 | |
| 38 // static bool get _supportsEs6Maps { | |
| 39 // return JS('returns:bool;depends:none;effects:none;throws:never;gvn:true', | |
| 40 // 'typeof Map != "undefined"'); | |
| 41 // } | |
| 42 | |
| 43 JsLinkedHashMap(); | |
| 44 | |
| 45 /// If ES6 Maps are available returns a linked hash-map backed by an ES6 Map. | |
| 46 // @ForceInline() | |
| 47 factory JsLinkedHashMap.es6() { | |
| 48 // return (_USE_ES6_MAPS && JsLinkedHashMap._supportsEs6Maps) | |
| 49 // ? new Es6LinkedHashMap<K, V>() | |
| 50 // : new JsLinkedHashMap<K, V>(); | |
| 51 return new JsLinkedHashMap<K, V>(); | |
| 52 } | |
| 53 | |
| 54 int get length => _length; | |
| 55 bool get isEmpty => _length == 0; | |
| 56 bool get isNotEmpty => !isEmpty; | |
| 57 | |
| 58 Iterable<K> get keys { | |
| 59 return new LinkedHashMapKeyIterable<K>(this); | |
| 60 } | |
| 61 | |
| 62 Iterable<V> get values { | |
| 63 return new MappedIterable<K, V>(keys, (each) => this[each]); | |
| 64 } | |
| 65 | |
| 66 bool containsKey(Object key) { | |
| 67 if (_isStringKey(key)) { | |
| 68 var strings = _strings; | |
| 69 if (strings == null) return false; | |
| 70 return _containsTableEntry(strings, key); | |
| 71 } else if (_isNumericKey(key)) { | |
| 72 var nums = _nums; | |
| 73 if (nums == null) return false; | |
| 74 return _containsTableEntry(nums, key); | |
| 75 } else { | |
| 76 return internalContainsKey(key); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 bool internalContainsKey(Object key) { | |
| 81 var rest = _rest; | |
| 82 if (rest == null) return false; | |
| 83 var bucket = _getBucket(rest, key); | |
| 84 return internalFindBucketIndex(bucket, key) >= 0; | |
| 85 } | |
| 86 | |
| 87 bool containsValue(Object value) { | |
| 88 return keys.any((each) => this[each] == value); | |
| 89 } | |
| 90 | |
| 91 void addAll(Map<K, V> other) { | |
| 92 other.forEach((K key, V value) { | |
| 93 this[key] = value; | |
| 94 }); | |
| 95 } | |
| 96 | |
| 97 V operator[](Object key) { | |
| 98 if (_isStringKey(key)) { | |
| 99 var strings = _strings; | |
| 100 if (strings == null) return null; | |
| 101 LinkedHashMapCell cell = _getTableEntry(strings, key); | |
|
sra1
2016/05/09 23:16:40
[B] since _getTableEntry returns dynamic, there is
| |
| 102 return (cell == null) ? null : cell.hashMapCellValue; | |
|
sra1
2016/05/09 23:16:40
[A] this is a cast from dynamic to V, which genera
| |
| 103 } else if (_isNumericKey(key)) { | |
| 104 var nums = _nums; | |
| 105 if (nums == null) return null; | |
| 106 LinkedHashMapCell cell = _getTableEntry(nums, key); | |
| 107 return (cell == null) ? null : cell.hashMapCellValue; | |
| 108 } else { | |
| 109 return internalGet(key); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 V internalGet(Object key) { | |
| 114 var rest = _rest; | |
| 115 if (rest == null) return null; | |
| 116 var bucket = _getBucket(rest, key); | |
| 117 int index = internalFindBucketIndex(bucket, key); | |
| 118 if (index < 0) return null; | |
| 119 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); | |
| 120 return cell.hashMapCellValue; | |
| 121 } | |
| 122 | |
| 123 void operator[]=(K key, V value) { | |
| 124 if (_isStringKey(key)) { | |
| 125 var strings = _strings; | |
| 126 if (strings == null) _strings = strings = _newHashTable(); | |
| 127 _addHashTableEntry(strings, key, value); | |
| 128 } else if (_isNumericKey(key)) { | |
| 129 var nums = _nums; | |
| 130 if (nums == null) _nums = nums = _newHashTable(); | |
| 131 _addHashTableEntry(nums, key, value); | |
| 132 } else { | |
| 133 internalSet(key, value); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void internalSet(K key, V value) { | |
| 138 var rest = _rest; | |
| 139 if (rest == null) _rest = rest = _newHashTable(); | |
| 140 var hash = internalComputeHashCode(key); | |
| 141 var bucket = _getTableEntry(rest, hash); | |
| 142 if (bucket == null) { | |
| 143 LinkedHashMapCell cell = _newLinkedCell(key, value); | |
| 144 _setTableEntry(rest, hash, JS('var', '[#]', cell)); | |
| 145 } else { | |
| 146 int index = internalFindBucketIndex(bucket, key); | |
| 147 if (index >= 0) { | |
| 148 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); | |
| 149 cell.hashMapCellValue = value; | |
| 150 } else { | |
| 151 LinkedHashMapCell cell = _newLinkedCell(key, value); | |
| 152 JS('void', '#.push(#)', bucket, cell); | |
| 153 } | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 V putIfAbsent(K key, V ifAbsent()) { | |
| 158 if (containsKey(key)) return this[key]; | |
| 159 V value = ifAbsent(); | |
| 160 this[key] = value; | |
| 161 return value; | |
| 162 } | |
| 163 | |
| 164 V remove(Object key) { | |
| 165 if (_isStringKey(key)) { | |
| 166 return _removeHashTableEntry(_strings, key); | |
| 167 } else if (_isNumericKey(key)) { | |
| 168 return _removeHashTableEntry(_nums, key); | |
| 169 } else { | |
| 170 return internalRemove(key); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 V internalRemove(Object key) { | |
| 175 var rest = _rest; | |
| 176 if (rest == null) return null; | |
| 177 var bucket = _getBucket(rest, key); | |
| 178 int index = internalFindBucketIndex(bucket, key); | |
| 179 if (index < 0) return null; | |
| 180 // Use splice to remove the [cell] element at the index and | |
| 181 // unlink the cell before returning its value. | |
| 182 LinkedHashMapCell cell = JS('var', '#.splice(#, 1)[0]', bucket, index); | |
| 183 _unlinkCell(cell); | |
| 184 // TODO(kasperl): Consider getting rid of the bucket list when | |
| 185 // the length reaches zero. | |
| 186 return cell.hashMapCellValue; | |
| 187 } | |
| 188 | |
| 189 void clear() { | |
| 190 if (_length > 0) { | |
| 191 _strings = _nums = _rest = _first = _last = null; | |
| 192 _length = 0; | |
| 193 _modified(); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 void forEach(void action(K key, V value)) { | |
| 198 LinkedHashMapCell cell = _first; | |
| 199 int modifications = _modifications; | |
| 200 while (cell != null) { | |
| 201 action(cell.hashMapCellKey, cell.hashMapCellValue); | |
| 202 if (modifications != _modifications) { | |
| 203 throw new ConcurrentModificationError(this); | |
| 204 } | |
| 205 cell = cell._next; | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void _addHashTableEntry(var table, K key, V value) { | |
| 210 LinkedHashMapCell cell = _getTableEntry(table, key); | |
| 211 if (cell == null) { | |
| 212 _setTableEntry(table, key, _newLinkedCell(key, value)); | |
| 213 } else { | |
| 214 cell.hashMapCellValue = value; | |
| 215 } | |
| 216 } | |
| 217 | |
| 218 V _removeHashTableEntry(var table, Object key) { | |
| 219 if (table == null) return null; | |
| 220 LinkedHashMapCell cell = _getTableEntry(table, key); | |
| 221 if (cell == null) return null; | |
| 222 _unlinkCell(cell); | |
| 223 _deleteTableEntry(table, key); | |
| 224 return cell.hashMapCellValue; | |
| 225 } | |
| 226 | |
| 227 void _modified() { | |
| 228 // Value cycles after 2^30 modifications so that modification counts are | |
| 229 // always unboxed (Smi) values. Modification detection will be missed if you | |
| 230 // make exactly some multiple of 2^30 modifications between advances of an | |
| 231 // iterator. | |
| 232 _modifications = (_modifications + 1) & 0x3ffffff; | |
| 233 } | |
| 234 | |
| 235 // Create a new cell and link it in as the last one in the list. | |
| 236 LinkedHashMapCell _newLinkedCell(K key, V value) { | |
| 237 LinkedHashMapCell cell = new LinkedHashMapCell(key, value); | |
| 238 if (_first == null) { | |
| 239 _first = _last = cell; | |
| 240 } else { | |
| 241 LinkedHashMapCell last = _last; | |
| 242 cell._previous = last; | |
| 243 _last = last._next = cell; | |
| 244 } | |
| 245 _length++; | |
| 246 _modified(); | |
| 247 return cell; | |
| 248 } | |
| 249 | |
| 250 // Unlink the given cell from the linked list of cells. | |
| 251 void _unlinkCell(LinkedHashMapCell cell) { | |
| 252 LinkedHashMapCell previous = cell._previous; | |
| 253 LinkedHashMapCell next = cell._next; | |
| 254 if (previous == null) { | |
| 255 assert(cell == _first); | |
| 256 _first = next; | |
| 257 } else { | |
| 258 previous._next = next; | |
| 259 } | |
| 260 if (next == null) { | |
| 261 assert(cell == _last); | |
| 262 _last = previous; | |
| 263 } else { | |
| 264 next._previous = previous; | |
| 265 } | |
| 266 _length--; | |
| 267 _modified(); | |
| 268 } | |
| 269 | |
| 270 static bool _isStringKey(var key) { | |
| 271 return key is String; | |
| 272 } | |
| 273 | |
| 274 static bool _isNumericKey(var key) { | |
| 275 // Only treat unsigned 30-bit integers as numeric keys. This way, | |
| 276 // we avoid converting them to strings when we use them as keys in | |
| 277 // the JavaScript hash table object. | |
| 278 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key); | |
| 279 } | |
| 280 | |
| 281 int internalComputeHashCode(var key) { | |
| 282 // We force the hash codes to be unsigned 30-bit integers to avoid | |
| 283 // issues with problematic keys like '__proto__'. Another option | |
| 284 // would be to throw an exception if the hash code isn't a number. | |
| 285 return JS('int', '# & 0x3ffffff', key.hashCode); | |
| 286 } | |
| 287 | |
| 288 List _getBucket(var table, var key) { | |
| 289 var hash = internalComputeHashCode(key); | |
| 290 return _getTableEntry(table, hash); | |
| 291 } | |
| 292 | |
| 293 int internalFindBucketIndex(var bucket, var key) { | |
| 294 if (bucket == null) return -1; | |
| 295 int length = JS('int', '#.length', bucket); | |
| 296 for (int i = 0; i < length; i++) { | |
| 297 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); | |
| 298 if (cell.hashMapCellKey == key) return i; | |
| 299 } | |
| 300 return -1; | |
| 301 } | |
| 302 | |
| 303 String toString() => Maps.mapToString(this); | |
| 304 | |
| 305 _getTableEntry(var table, var key) { | |
|
sra1
2016/05/09 23:16:40
[B] There is no return type, so it is dynamic.
| |
| 306 return JS('var', '#[#]', table, key); | |
| 307 } | |
| 308 | |
| 309 void _setTableEntry(var table, var key, var value) { | |
| 310 assert(value != null); | |
| 311 JS('void', '#[#] = #', table, key, value); | |
| 312 } | |
| 313 | |
| 314 void _deleteTableEntry(var table, var key) { | |
| 315 JS('void', 'delete #[#]', table, key); | |
| 316 } | |
| 317 | |
| 318 bool _containsTableEntry(var table, var key) { | |
| 319 LinkedHashMapCell cell = _getTableEntry(table, key); | |
| 320 return cell != null; | |
| 321 } | |
| 322 | |
| 323 _newHashTable() { | |
| 324 // Create a new JavaScript object to be used as a hash table. Use | |
| 325 // Object.create to avoid the properties on Object.prototype | |
| 326 // showing up as entries. | |
| 327 var table = JS('var', 'Object.create(null)'); | |
| 328 // Attempt to force the hash table into 'dictionary' mode by | |
| 329 // adding a property to it and deleting it again. | |
| 330 var temporaryKey = '<non-identifier-key>'; | |
| 331 _setTableEntry(table, temporaryKey, table); | |
| 332 _deleteTableEntry(table, temporaryKey); | |
| 333 return table; | |
| 334 } | |
| 335 } | |
| 336 | |
| 337 class Es6LinkedHashMap<K, V> extends JsLinkedHashMap<K, V> { | |
| 338 | |
| 339 @override | |
| 340 _getTableEntry(var table, var key) { | |
| 341 return JS('var', '#.get(#)', table, key); | |
| 342 } | |
| 343 | |
| 344 @override | |
| 345 void _setTableEntry(var table, var key, var value) { | |
| 346 JS('void', '#.set(#, #)', table, key, value); | |
| 347 } | |
| 348 | |
| 349 @override | |
| 350 void _deleteTableEntry(var table, var key) { | |
| 351 JS('void', '#.delete(#)', table, key); | |
| 352 } | |
| 353 | |
| 354 @override | |
| 355 bool _containsTableEntry(var table, var key) { | |
| 356 return JS('bool', '#.has(#)', table, key); | |
| 357 } | |
| 358 | |
| 359 @override | |
| 360 _newHashTable() { | |
| 361 return JS('var', 'new Map()'); | |
| 362 } | |
| 363 } | |
| 364 | |
| 365 class LinkedHashMapCell { | |
|
sra1
2016/05/09 23:16:40
This class needs DDC comment-style type parameters
| |
| 366 final hashMapCellKey; | |
| 367 var hashMapCellValue; | |
| 368 | |
| 369 LinkedHashMapCell _next; | |
| 370 LinkedHashMapCell _previous; | |
| 371 | |
| 372 LinkedHashMapCell(this.hashMapCellKey, this.hashMapCellValue); | |
| 373 } | |
| 374 | |
| 375 class LinkedHashMapKeyIterable<E> extends Iterable<E> | |
| 376 implements EfficientLength { | |
| 377 final _map; | |
| 378 LinkedHashMapKeyIterable(this._map); | |
| 379 | |
| 380 int get length => _map._length; | |
| 381 bool get isEmpty => _map._length == 0; | |
| 382 | |
| 383 Iterator<E> get iterator { | |
| 384 return new LinkedHashMapKeyIterator<E>(_map, _map._modifications); | |
| 385 } | |
| 386 | |
| 387 bool contains(Object element) { | |
| 388 return _map.containsKey(element); | |
| 389 } | |
| 390 | |
| 391 void forEach(void f(E element)) { | |
| 392 LinkedHashMapCell cell = _map._first; | |
| 393 int modifications = _map._modifications; | |
| 394 while (cell != null) { | |
| 395 f(cell.hashMapCellKey); | |
| 396 if (modifications != _map._modifications) { | |
| 397 throw new ConcurrentModificationError(_map); | |
| 398 } | |
| 399 cell = cell._next; | |
| 400 } | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 class LinkedHashMapKeyIterator<E> implements Iterator<E> { | |
| 405 final _map; | |
| 406 final int _modifications; | |
| 407 LinkedHashMapCell _cell; | |
| 408 E _current; | |
| 409 | |
| 410 LinkedHashMapKeyIterator(this._map, this._modifications) { | |
| 411 _cell = _map._first; | |
| 412 } | |
| 413 | |
| 414 E get current => _current; | |
| 415 | |
| 416 bool moveNext() { | |
| 417 if (_modifications != _map._modifications) { | |
| 418 throw new ConcurrentModificationError(_map); | |
| 419 } else if (_cell == null) { | |
| 420 _current = null; | |
| 421 return false; | |
| 422 } else { | |
| 423 _current = _cell.hashMapCellKey; | |
| 424 _cell = _cell._next; | |
| 425 return true; | |
| 426 } | |
| 427 } | |
| 428 } | |
| OLD | NEW |