OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.summary.flat_buffers; | 5 library analyzer.src.summary.flat_buffers; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:math'; | 9 import 'dart:math'; |
10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 int _tail; | 103 int _tail; |
104 | 104 |
105 /** | 105 /** |
106 * The location of the end of the current table, measured in bytes from the | 106 * The location of the end of the current table, measured in bytes from the |
107 * end of [_buf], or `null` if a table is not currently being built. | 107 * end of [_buf], or `null` if a table is not currently being built. |
108 */ | 108 */ |
109 int _currentTableEndTail; | 109 int _currentTableEndTail; |
110 | 110 |
111 _VTable _currentVTable; | 111 _VTable _currentVTable; |
112 | 112 |
| 113 /** |
| 114 * Map containing all strings that have been written so far. This allows us |
| 115 * to avoid duplicating strings. |
| 116 */ |
| 117 Map<String, Offset<String>> _strings = <String, Offset<String>>{}; |
| 118 |
113 Builder({this.initialSize: 1024}) { | 119 Builder({this.initialSize: 1024}) { |
114 reset(); | 120 reset(); |
115 } | 121 } |
116 | 122 |
117 /** | 123 /** |
118 * Add the [field] with the given boolean [value]. The field is not added if | 124 * Add the [field] with the given boolean [value]. The field is not added if |
119 * the [value] is equal to [def]. Booleans are stored as 8-bit fields with | 125 * the [value] is equal to [def]. Booleans are stored as 8-bit fields with |
120 * `0` for `false` and `1` for `true`. | 126 * `0` for `false` and `1` for `true`. |
121 */ | 127 */ |
122 void addBool(int field, bool value, [bool def]) { | 128 void addBool(int field, bool value, [bool def]) { |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 /** | 386 /** |
381 * Write the given string [value] and return its [Offset], or `null` if | 387 * Write the given string [value] and return its [Offset], or `null` if |
382 * the [value] is equal to [def]. | 388 * the [value] is equal to [def]. |
383 */ | 389 */ |
384 Offset<String> writeString(String value, [String def]) { | 390 Offset<String> writeString(String value, [String def]) { |
385 if (_currentVTable != null) { | 391 if (_currentVTable != null) { |
386 throw new StateError( | 392 throw new StateError( |
387 'Cannot write a non-scalar value while writing a table.'); | 393 'Cannot write a non-scalar value while writing a table.'); |
388 } | 394 } |
389 if (value != def) { | 395 if (value != def) { |
390 // TODO(scheglov) optimize for ASCII strings | 396 return _strings.putIfAbsent(value, () { |
391 List<int> bytes = UTF8.encode(value); | 397 // TODO(scheglov) optimize for ASCII strings |
392 int length = bytes.length; | 398 List<int> bytes = UTF8.encode(value); |
393 _prepare(4, 1, additionalBytes: length); | 399 int length = bytes.length; |
394 Offset<String> result = new Offset(_tail); | 400 _prepare(4, 1, additionalBytes: length); |
395 _setUint32AtTail(_buf, _tail, length); | 401 Offset<String> result = new Offset(_tail); |
396 int offset = _buf.lengthInBytes - _tail + 4; | 402 _setUint32AtTail(_buf, _tail, length); |
397 for (int i = 0; i < length; i++) { | 403 int offset = _buf.lengthInBytes - _tail + 4; |
398 _buf.setUint8(offset++, bytes[i]); | 404 for (int i = 0; i < length; i++) { |
399 } | 405 _buf.setUint8(offset++, bytes[i]); |
400 return result; | 406 } |
| 407 return result; |
| 408 }); |
401 } | 409 } |
402 return null; | 410 return null; |
403 } | 411 } |
404 | 412 |
405 /** | 413 /** |
406 * Prepare for writing the given [count] of scalars of the given [size]. | 414 * Prepare for writing the given [count] of scalars of the given [size]. |
407 * Additionally allocate the specified [additionalBytes]. Update the current | 415 * Additionally allocate the specified [additionalBytes]. Update the current |
408 * tail pointer to point at the allocated space. | 416 * tail pointer to point at the allocated space. |
409 */ | 417 */ |
410 void _prepare(int size, int count, {int additionalBytes: 0}) { | 418 void _prepare(int size, int count, {int additionalBytes: 0}) { |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 // Table size. | 759 // Table size. |
752 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN); | 760 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN); |
753 bufOffset += 2; | 761 bufOffset += 2; |
754 // Field offsets. | 762 // Field offsets. |
755 for (int fieldOffset in fieldOffsets) { | 763 for (int fieldOffset in fieldOffsets) { |
756 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN); | 764 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN); |
757 bufOffset += 2; | 765 bufOffset += 2; |
758 } | 766 } |
759 } | 767 } |
760 } | 768 } |
OLD | NEW |