Skip to content

Commit 37c8c70

Browse files
committed
Move BytesBuilder class and implementation to dart:typed_data.
Change-Id: Ic4fe1944ef4af19732f23c389845b6a74af0bc32 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154682 Reviewed-by: Johnni Winther <[email protected]>
1 parent 7e8348f commit 37c8c70

File tree

17 files changed

+258
-249
lines changed

17 files changed

+258
-249
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777

7878
[#42714]: https://github.com/dart-lang/sdk/issues/42714
7979

80+
#### `dart:typed_data`
81+
82+
* Class `BytesBuilder` is moved from `dart:io` to `dart:typed_data`.
83+
It's temporarily being exported from `dart:io` as well.
84+
8085
### Tools
8186

8287
#### dartfmt

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ deps = {
539539
"packages": [
540540
{
541541
"package": "dart/cfe/dart2js_dills",
542-
"version": "binary_version:43_2",
542+
"version": "binary_version:44_2",
543543
}
544544
],
545545
"dep_type": "cipd",

pkg/kernel/binary.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ type CanonicalName {
143143

144144
type ComponentFile {
145145
UInt32 magic = 0x90ABCDEF;
146-
UInt32 formatVersion = 43;
146+
UInt32 formatVersion = 44;
147147
Byte[10] shortSdkHash;
148148
List<String> problemsAsJson; // Described in problems.md.
149149
Library[] libraries;
@@ -320,7 +320,7 @@ type Class extends Node {
320320
FileOffset fileOffset; // Offset of the name of the class.
321321
FileOffset fileEndOffset;
322322
Byte flags (levelBit0, levelBit1, isAbstract, isEnum, isAnonymousMixin,
323-
isEliminatedMixin, isMixinDeclaration,
323+
isEliminatedMixin, isMixinDeclaration,
324324
hasConstConstructor); // Where level is index into ClassLevel
325325
StringReference name;
326326
List<Expression> annotations;

pkg/kernel/lib/binary/tag.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class Tag {
149149
/// Internal version of kernel binary format.
150150
/// Bump it when making incompatible changes in kernel binaries.
151151
/// Keep in sync with runtime/vm/kernel_binary.h, pkg/kernel/binary.md.
152-
static const int BinaryFormatVersion = 43;
152+
static const int BinaryFormatVersion = 44;
153153
}
154154

155155
abstract class ConstantTag {

runtime/tests/vm/dart/sdk_hash_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'snapshot_test_helper.dart';
1414

1515
// Keep in sync with pkg/kernel/lib/binary/tag.dart:
1616
const tagComponentFile = [0x90, 0xAB, 0xCD, 0xEF];
17-
const tagBinaryFormatVersion = [0x00, 0x00, 0x00, 43];
1817

1918
Future<void> main(List<String> args) async {
2019
if (args.length == 1 && args[0] == '--child') {
@@ -53,7 +52,6 @@ Future<void> main(List<String> args) async {
5352
// The SDK Hash is located after the ComponentFile and BinaryFormatVersion
5453
// tags (both UInt32).
5554
Expect.listEquals(tagComponentFile, bytes.sublist(0, 4));
56-
Expect.listEquals(tagBinaryFormatVersion, bytes.sublist(4, 8));
5755
Expect.notEquals('0000000000', ascii.decode(bytes.sublist(8, 10)));
5856
// Flip the first byte in the hash:
5957
bytes[8] = ~bytes[8];

runtime/tests/vm/dart_2/sdk_hash_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'snapshot_test_helper.dart';
1414

1515
// Keep in sync with pkg/kernel/lib/binary/tag.dart:
1616
const tagComponentFile = [0x90, 0xAB, 0xCD, 0xEF];
17-
const tagBinaryFormatVersion = [0x00, 0x00, 0x00, 43];
1817

1918
Future<void> main(List<String> args) async {
2019
if (args.length == 1 && args[0] == '--child') {
@@ -53,7 +52,6 @@ Future<void> main(List<String> args) async {
5352
// The SDK Hash is located after the ComponentFile and BinaryFormatVersion
5453
// tags (both UInt32).
5554
Expect.listEquals(tagComponentFile, bytes.sublist(0, 4));
56-
Expect.listEquals(tagBinaryFormatVersion, bytes.sublist(4, 8));
5755
Expect.notEquals('0000000000', ascii.decode(bytes.sublist(8, 18)));
5856
// Flip the first byte in the hash:
5957
bytes[8] = ~bytes[8];

runtime/vm/kernel_binary.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace kernel {
2020
static const uint32_t kMagicProgramFile = 0x90ABCDEFu;
2121

2222
// Both version numbers are inclusive.
23-
static const uint32_t kMinSupportedKernelFormatVersion = 43;
24-
static const uint32_t kMaxSupportedKernelFormatVersion = 43;
23+
static const uint32_t kMinSupportedKernelFormatVersion = 44;
24+
static const uint32_t kMaxSupportedKernelFormatVersion = 44;
2525

2626
// Keep in sync with package:kernel/lib/binary/tag.dart
2727
#define KERNEL_TAG_LIST(V) \

sdk/lib/_internal/vm/bin/common_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import "dart:math" show min;
3333

3434
import "dart:nativewrappers" show NativeFieldWrapperClass1;
3535

36-
import "dart:typed_data" show Uint8List;
36+
import "dart:typed_data" show Uint8List, BytesBuilder;
3737

3838
/// These are the additional parts of this patch library:
3939
// part "directory_patch.dart";
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
// Copyright (c) 2013, 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+
part of dart._internal;
6+
7+
/// Builds a list of bytes, allowing bytes and lists of bytes to be added at the
8+
/// end.
9+
///
10+
/// Used to efficiently collect bytes and lists of bytes.
11+
abstract class BytesBuilder {
12+
/// Construct a new empty [BytesBuilder].
13+
///
14+
/// If [copy] is true (the default), the created builder is a *copying*
15+
/// builder. A copying builder maintains its own internal buffer and copies
16+
/// the bytes added to it eagerly.
17+
///
18+
/// If [copy] set to false, the created builder assumes that lists added
19+
/// to it will not change.
20+
/// Any [Uint8List] added using [add] is kept until
21+
/// [toBytes] or [takeBytes] is called,
22+
/// and only then are their contents copied.
23+
/// A non-[Uint8List] may be copied eagerly.
24+
/// If only a single [Uint8List] is added to the builder,
25+
/// that list is returned by [toBytes] or [takeBytes] directly, without any copying.
26+
/// A list added to a non-copying builder *should not* change its content
27+
/// after being added, and it *must not* change its length after being added.
28+
/// (Normal [Uint8List]s are fixed length lists, but growing lists implementing
29+
/// [Uint8List] exist.)
30+
factory BytesBuilder({bool copy = true}) =>
31+
copy ? _CopyingBytesBuilder() : _BytesBuilder();
32+
33+
/// Appends [bytes] to the current contents of this builder.
34+
///
35+
/// Each value of [bytes] will be truncated
36+
/// to an 8-bit value in the range 0 .. 255.
37+
void add(List<int> bytes);
38+
39+
/// Appends [byte] to the current contents of this builder.
40+
///
41+
/// The [byte] will be truncated to an 8-bit value in the range 0 .. 255.
42+
void addByte(int byte);
43+
44+
/// Returns the bytes currently contained in this builder and clears it.
45+
///
46+
/// The returned list may be a view of a larger buffer.
47+
Uint8List takeBytes();
48+
49+
/// Returns a copy of the current byte contents of this builder.
50+
///
51+
/// Leaves the contents of this builder intact.
52+
Uint8List toBytes();
53+
54+
/// The number of bytes in this builder.
55+
int get length;
56+
57+
/// Whether the buffer is empty.
58+
bool get isEmpty;
59+
60+
/// Whether the buffer is non-empty.
61+
bool get isNotEmpty;
62+
63+
/// Clears the contents of this builder.
64+
///
65+
/// The current contents are discarded and this builder becomes empty.
66+
void clear();
67+
}
68+
69+
/// A [BytesBuilder] which appends bytes to a growing internal buffer.
70+
class _CopyingBytesBuilder implements BytesBuilder {
71+
/// Initial size of internal buffer.
72+
static const int _initSize = 1024;
73+
74+
/// Reusable empty [Uint8List].
75+
///
76+
/// Safe for reuse because a fixed-length empty list is immutable.
77+
static final _emptyList = Uint8List(0);
78+
79+
/// Current count of bytes written to buffer.
80+
int _length = 0;
81+
82+
/// Internal buffer accumulating bytes.
83+
///
84+
/// Will grow as necessary
85+
Uint8List _buffer;
86+
87+
_CopyingBytesBuilder() : _buffer = _emptyList;
88+
89+
void add(List<int> bytes) {
90+
int byteCount = bytes.length;
91+
if (byteCount == 0) return;
92+
int required = _length + byteCount;
93+
if (_buffer.length < required) {
94+
_grow(required);
95+
}
96+
assert(_buffer.length >= required);
97+
if (bytes is Uint8List) {
98+
_buffer.setRange(_length, required, bytes);
99+
} else {
100+
for (int i = 0; i < byteCount; i++) {
101+
_buffer[_length + i] = bytes[i];
102+
}
103+
}
104+
_length = required;
105+
}
106+
107+
void addByte(int byte) {
108+
if (_buffer.length == _length) {
109+
// The grow algorithm always at least doubles.
110+
// If we added one to _length it would quadruple unnecessarily.
111+
_grow(_length);
112+
}
113+
assert(_buffer.length > _length);
114+
_buffer[_length] = byte;
115+
_length++;
116+
}
117+
118+
void _grow(int required) {
119+
// We will create a list in the range of 2-4 times larger than
120+
// required.
121+
int newSize = required * 2;
122+
if (newSize < _initSize) {
123+
newSize = _initSize;
124+
} else {
125+
newSize = _pow2roundup(newSize);
126+
}
127+
var newBuffer = Uint8List(newSize);
128+
newBuffer.setRange(0, _buffer.length, _buffer);
129+
_buffer = newBuffer;
130+
}
131+
132+
Uint8List takeBytes() {
133+
if (_length == 0) return _emptyList;
134+
var buffer = Uint8List.view(_buffer.buffer, _buffer.offsetInBytes, _length);
135+
_clear();
136+
return buffer;
137+
}
138+
139+
Uint8List toBytes() {
140+
if (_length == 0) return _emptyList;
141+
return Uint8List.fromList(
142+
Uint8List.view(_buffer.buffer, _buffer.offsetInBytes, _length));
143+
}
144+
145+
int get length => _length;
146+
147+
bool get isEmpty => _length == 0;
148+
149+
bool get isNotEmpty => _length != 0;
150+
151+
void clear() {
152+
_clear();
153+
}
154+
155+
void _clear() {
156+
_length = 0;
157+
_buffer = _emptyList;
158+
}
159+
160+
/// Rounds numbers <= 2^32 up to the nearest power of 2.
161+
static int _pow2roundup(int x) {
162+
assert(x > 0);
163+
--x;
164+
x |= x >> 1;
165+
x |= x >> 2;
166+
x |= x >> 4;
167+
x |= x >> 8;
168+
x |= x >> 16;
169+
return x + 1;
170+
}
171+
}
172+
173+
/// A non-copying [BytesBuilder].
174+
///
175+
/// Accumulates lists of integers and lazily builds
176+
/// a collected list with all the bytes when requested.
177+
class _BytesBuilder implements BytesBuilder {
178+
int _length = 0;
179+
final List<Uint8List> _chunks = [];
180+
181+
void add(List<int> bytes) {
182+
Uint8List typedBytes;
183+
if (bytes is Uint8List) {
184+
typedBytes = bytes;
185+
} else {
186+
typedBytes = Uint8List.fromList(bytes);
187+
}
188+
_chunks.add(typedBytes);
189+
_length += typedBytes.length;
190+
}
191+
192+
void addByte(int byte) {
193+
// TODO(lrn): Optimize repeated `addByte` calls.
194+
_chunks.add(Uint8List(1)..[0] = byte);
195+
_length++;
196+
}
197+
198+
Uint8List takeBytes() {
199+
if (_length == 0) return _CopyingBytesBuilder._emptyList;
200+
if (_chunks.length == 1) {
201+
var buffer = _chunks[0];
202+
_clear();
203+
return buffer;
204+
}
205+
var buffer = Uint8List(_length);
206+
int offset = 0;
207+
for (var chunk in _chunks) {
208+
buffer.setRange(offset, offset + chunk.length, chunk);
209+
offset += chunk.length;
210+
}
211+
_clear();
212+
return buffer;
213+
}
214+
215+
Uint8List toBytes() {
216+
if (_length == 0) return _CopyingBytesBuilder._emptyList;
217+
var buffer = Uint8List(_length);
218+
int offset = 0;
219+
for (var chunk in _chunks) {
220+
buffer.setRange(offset, offset + chunk.length, chunk);
221+
offset += chunk.length;
222+
}
223+
return buffer;
224+
}
225+
226+
int get length => _length;
227+
228+
bool get isEmpty => _length == 0;
229+
230+
bool get isNotEmpty => _length != 0;
231+
232+
void clear() {
233+
_clear();
234+
}
235+
236+
void _clear() {
237+
_length = 0;
238+
_chunks.clear();
239+
}
240+
}

sdk/lib/internal/internal.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'dart:math' show Random;
2121
import 'dart:typed_data' show Uint8List;
2222

2323
part 'async_cast.dart';
24+
part 'bytes_builder.dart';
2425
part 'cast.dart';
2526
part 'errors.dart';
2627
part 'iterable.dart';

0 commit comments

Comments
 (0)