Dart: binary lists (typed_data) (#6839)

* Dart - add eager mode to Uint8ListReader

* Dart - add Int8ListReader

* Dart - use binary reader where useful

* Dart - test binary list reader laziness

* Dart - update generated code
This commit is contained in:
Ivan Dlugos
2021-09-15 17:50:57 +02:00
committed by GitHub
parent 0a3b017f09
commit 90baa1444b
6 changed files with 108 additions and 33 deletions

View File

@@ -155,7 +155,8 @@ class Builder {
}
}
/// Calculate the finished buffer size (aligned).@pragma('vm:prefer-inline')
/// Calculate the finished buffer size (aligned).
@pragma('vm:prefer-inline')
int size() => _tail + ((-_tail) & (_maxAlign - 1));
/// Add the [field] with the given boolean [value]. The field is not added if
@@ -1170,11 +1171,17 @@ class Uint16Reader extends Reader<int> {
int read(BufferContext bc, int offset) => bc._getUint16(offset);
}
/// Reader of lists of unsigned 8-bit integer values.
///
/// The returned unmodifiable lists lazily read values on access.
/// Reader of unmodifiable binary data (a list of unsigned 8-bit integers).
class Uint8ListReader extends Reader<List<int>> {
const Uint8ListReader();
/// Enables lazy reading of the list
///
/// If true, the returned unmodifiable list lazily reads bytes on access.
/// Therefore, the underlying buffer must not change while accessing the list.
///
/// If false, reads the whole list immediately as an Uint8List.
final bool lazy;
const Uint8ListReader({this.lazy = true});
@override
@pragma('vm:prefer-inline')
@@ -1182,8 +1189,18 @@ class Uint8ListReader extends Reader<List<int>> {
@override
@pragma('vm:prefer-inline')
List<int> read(BufferContext bc, int offset) =>
_FbUint8List(bc, bc.derefObject(offset));
List<int> read(BufferContext bc, int offset) {
final listOffset = bc.derefObject(offset);
if (lazy) return _FbUint8List(bc, listOffset);
final length = bc._getUint32(listOffset);
final result = Uint8List(length);
var pos = listOffset + _sizeofUint32;
for (var i = 0; i < length; i++, pos++) {
result[i] = bc._getUint8(pos);
}
return result;
}
}
/// The reader of unsigned 8-bit integers.
@@ -1199,6 +1216,38 @@ class Uint8Reader extends Reader<int> {
int read(BufferContext bc, int offset) => bc._getUint8(offset);
}
/// Reader of unmodifiable binary data (a list of signed 8-bit integers).
class Int8ListReader extends Reader<List<int>> {
/// Enables lazy reading of the list
///
/// If true, the returned unmodifiable list lazily reads bytes on access.
/// Therefore, the underlying buffer must not change while accessing the list.
///
/// If false, reads the whole list immediately as an Uint8List.
final bool lazy;
const Int8ListReader({this.lazy = true});
@override
@pragma('vm:prefer-inline')
int get size => _sizeofUint32;
@override
@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);
final result = Int8List(length);
var pos = listOffset + _sizeofUint32;
for (var i = 0; i < length; i++, pos++) {
result[i] = bc._getInt8(pos);
}
return result;
}
}
/// The list backed by 64-bit values - Uint64 length and Float64.
class _FbFloat64List extends _FbList<double> {
_FbFloat64List(BufferContext bc, int offset) : super(bc, offset);
@@ -1286,6 +1335,15 @@ class _FbUint8List extends _FbList<int> {
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);

View File

@@ -373,8 +373,7 @@ class Reference {
return null;
}
int _diffKeys(
List<int> input, int index, int indirectOffset, int byteWidth) {
int _diffKeys(List<int> input, int index, int indirectOffset, int byteWidth) {
final keyOffset = indirectOffset + index * byteWidth;
final keyIndirectOffset =
keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth));