Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dart: Return dart:typed_data equivalent where possible #8289

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 23 additions & 36 deletions dart/lib/flat_buffers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class BufferContext {
@pragma('vm:prefer-inline')
Uint8List _asUint8List(int offset, int length) =>
_buffer.buffer.asUint8List(_buffer.offsetInBytes + offset, length);

@pragma('vm:prefer-inline')
Int8List _asInt8List(int offset, int length) =>
_buffer.buffer.asInt8List(_buffer.offsetInBytes + offset, length);

@pragma('vm:prefer-inline')
Uint16List _asUint16List(int offset, int length) =>
_buffer.buffer.asUint16List(_buffer.offsetInBytes + offset, length);
NotTsunami marked this conversation as resolved.
Show resolved Hide resolved

@pragma('vm:prefer-inline')
double _getFloat64(int offset) => _buffer.getFloat64(offset, Endian.little);
Expand Down Expand Up @@ -729,7 +737,7 @@ class Builder {

@pragma('vm:prefer-inline')
void _writeUTFString(String value) {
final bytes = utf8.encode(value) as Uint8List;
final bytes = utf8.encode(value);
final length = bytes.length;
_prepare(4, 1, additionalBytes: length + 1);
_setUint32AtTail(_tail, length);
Expand Down Expand Up @@ -953,7 +961,7 @@ class Int32Reader extends Reader<int> {
int read(BufferContext bc, int offset) => bc._getInt32(offset);
}

/// The reader of signed 32-bit integers.
/// The reader of signed 16-bit integers.
NotTsunami marked this conversation as resolved.
Show resolved Hide resolved
class Int16Reader extends Reader<int> {
const Int16Reader() : super();

Expand All @@ -966,7 +974,7 @@ class Int16Reader extends Reader<int> {
int read(BufferContext bc, int offset) => bc._getInt16(offset);
}

/// The reader of 8-bit signed integers.
/// The reader of signed 8-bit integers.
class Int8Reader extends Reader<int> {
const Int8Reader() : super();

Expand Down Expand Up @@ -1165,8 +1173,12 @@ class Uint16ListReader extends Reader<List<int>> {

@override
@pragma('vm:prefer-inline')
List<int> read(BufferContext bc, int offset) =>
_FbUint16List(bc, bc.derefObject(offset));
List<int> read(BufferContext bc, int offset) {
final listOffset = bc.derefObject(offset);
final length = bc._getUint32(listOffset);

return bc._asUint16List(listOffset + _sizeofUint32, length);
NotTsunami marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// The reader of unsigned 32-bit integers.
Expand Down Expand Up @@ -1202,9 +1214,10 @@ class Uint8ListReader extends Reader<List<int>> {
@pragma('vm:prefer-inline')
List<int> read(BufferContext bc, int offset) {
final listOffset = bc.derefObject(offset);
if (lazy) return _FbUint8List(bc, listOffset);

final length = bc._getUint32(listOffset);

if (lazy) return bc._asUint8List(listOffset + _sizeofUint32, length);

final result = Uint8List(length);
var pos = listOffset + _sizeofUint32;
for (var i = 0; i < length; i++, pos++) {
Expand Down Expand Up @@ -1247,9 +1260,10 @@ class Int8ListReader extends Reader<List<int>> {
@pragma('vm:prefer-inline')
List<int> read(BufferContext bc, int offset) {
final listOffset = bc.derefObject(offset);
if (lazy) return _FbUint8List(bc, listOffset);

final length = bc._getUint32(listOffset);

if (lazy) return bc._asInt8List(listOffset + _sizeofUint32, length);

final result = Int8List(length);
var pos = listOffset + _sizeofUint32;
for (var i = 0; i < length; i++, pos++) {
Expand Down Expand Up @@ -1328,33 +1342,6 @@ class _FbUint32List extends _FbList<int> {
int operator [](int i) => bc._getUint32(offset + 4 + 4 * i);
}

/// List backed by 16-bit unsigned integers.
class _FbUint16List extends _FbList<int> {
_FbUint16List(BufferContext bc, int offset) : super(bc, offset);

@override
@pragma('vm:prefer-inline')
int operator [](int i) => bc._getUint16(offset + 4 + 2 * i);
}

/// List backed by 8-bit unsigned integers.
class _FbUint8List extends _FbList<int> {
_FbUint8List(BufferContext bc, int offset) : super(bc, offset);

@override
@pragma('vm:prefer-inline')
int operator [](int i) => bc._getUint8(offset + 4 + i);
}

/// List backed by 8-bit signed integers.
class _FbInt8List extends _FbList<int> {
_FbInt8List(BufferContext bc, int offset) : super(bc, offset);

@override
@pragma('vm:prefer-inline')
int operator [](int i) => bc._getInt8(offset + 4 + i);
}

/// List backed by 8-bit unsigned integers.
class _FbBoolList extends _FbList<bool> {
_FbBoolList(BufferContext bc, int offset) : super(bc, offset);
Expand Down
32 changes: 30 additions & 2 deletions dart/test/flat_buffers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ class BuilderTest {
// read and verify
BufferContext buf = BufferContext.fromBytes(byteList);
List<int> items = const Uint16ListReader().read(buf, 0);
expect(items is Uint16List, true);
expect(items, hasLength(3));
expect(items, orderedEquals(<int>[1, 2, 60000]));
}
Expand All @@ -683,18 +684,45 @@ class BuilderTest {
const buffOffset = 8; // 32-bit offset to the list, + 32-bit length
for (final lazy in [true, false]) {
List<int> items = Uint8ListReader(lazy: lazy).read(buf, 0);
expect(items is Uint8List, true);
expect(items, hasLength(6));
expect(items, orderedEquals(<int>[1, 2, 3, 4, 0x9A, 0xFA]));
expect(items, orderedEquals(<int>[1, 2, 3, 4, 154, 250]));

// overwrite the buffer to verify the laziness
buf.buffer.setUint8(buffOffset + 1, 99);
expect(items, orderedEquals(<int>[1, lazy ? 99 : 2, 3, 4, 0x9A, 0xFA]));
expect(items, orderedEquals(<int>[1, lazy ? 99 : 2, 3, 4, 154, 250]));

// restore the previous value for the next loop
buf.buffer.setUint8(buffOffset + 1, 2);
}
}

void test_writeList_ofInt8() {
List<int> byteList;
{
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListInt8(<int>[1, 2, 3, 4, 0x9A, 0xFA]);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = BufferContext.fromBytes(byteList);
const buffOffset = 8; // 32-bit offset to the list, + 32-bit length
for (final lazy in [true, false]) {
List<int> items = Int8ListReader(lazy: lazy).read(buf, 0);
expect(items is Int8List, true);
expect(items, hasLength(6));
expect(items, orderedEquals(<int>[1, 2, 3, 4, -102, -6]));

// overwrite the buffer to verify the laziness
buf.buffer.setInt8(buffOffset + 1, 99);
expect(items, orderedEquals(<int>[1, lazy ? 99 : 2, 3, 4, -102, -6]));

// restore the previous value for the next loop
buf.buffer.setInt8(buffOffset + 1, 2);
}
}

void test_reset() {
// We'll run a selection of tests , reusing the builder between them.
final testCases = <void Function(Builder?)>[
Expand Down
Loading