diff options
author | Ivan Dlugos <6349682+vaind@users.noreply.github.com> | 2021-07-26 19:09:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-26 10:09:46 -0700 |
commit | a6eeeb3b9cce99b50e4b77695f72df30a00aef7a (patch) | |
tree | e001e3c337e302a81313d6193787a9d50b390bdb /dart | |
parent | 3dd02144d5d0b0b3cc1f95856ecd22c2516f45e3 (diff) | |
download | flatbuffers-a6eeeb3b9cce99b50e4b77695f72df30a00aef7a.tar.gz flatbuffers-a6eeeb3b9cce99b50e4b77695f72df30a00aef7a.tar.bz2 flatbuffers-a6eeeb3b9cce99b50e4b77695f72df30a00aef7a.zip |
Dart - inline small functions to increase performance (#6755)
~25 % for reading
~30-35 % for writing
Diffstat (limited to 'dart')
-rw-r--r-- | dart/lib/flat_buffers.dart | 306 |
1 files changed, 172 insertions, 134 deletions
diff --git a/dart/lib/flat_buffers.dart b/dart/lib/flat_buffers.dart index e136c12d..1dba6597 100644 --- a/dart/lib/flat_buffers.dart +++ b/dart/lib/flat_buffers.dart @@ -31,51 +31,50 @@ class BufferContext { ByteData get buffer => _buffer; /// Create from a FlatBuffer represented by a list of bytes (uint8). - factory BufferContext.fromBytes(List<int> byteList) { - Uint8List uint8List = _asUint8List(byteList); - ByteData buf = new ByteData.view(uint8List.buffer, uint8List.offsetInBytes); - return BufferContext(buf); - } + factory BufferContext.fromBytes(List<int> byteList) => + BufferContext(byteList is Uint8List + ? byteList.buffer.asByteData(byteList.offsetInBytes) + : ByteData.view(Uint8List.fromList(byteList).buffer)); /// Create from a FlatBuffer represented by ByteData. BufferContext(this._buffer); - int derefObject(int offset) { - return offset + _getUint32(offset); - } + @pragma('vm:prefer-inline') + int derefObject(int offset) => offset + _getUint32(offset); - Uint8List _asUint8LIst(int offset, int length) => + @pragma('vm:prefer-inline') + Uint8List _asUint8List(int offset, int length) => _buffer.buffer.asUint8List(_buffer.offsetInBytes + offset, length); + @pragma('vm:prefer-inline') double _getFloat64(int offset) => _buffer.getFloat64(offset, Endian.little); + @pragma('vm:prefer-inline') double _getFloat32(int offset) => _buffer.getFloat32(offset, Endian.little); + @pragma('vm:prefer-inline') int _getInt64(int offset) => _buffer.getInt64(offset, Endian.little); + @pragma('vm:prefer-inline') int _getInt32(int offset) => _buffer.getInt32(offset, Endian.little); + @pragma('vm:prefer-inline') int _getInt16(int offset) => _buffer.getInt16(offset, Endian.little); + @pragma('vm:prefer-inline') int _getInt8(int offset) => _buffer.getInt8(offset); + @pragma('vm:prefer-inline') int _getUint64(int offset) => _buffer.getUint64(offset, Endian.little); + @pragma('vm:prefer-inline') int _getUint32(int offset) => _buffer.getUint32(offset, Endian.little); + @pragma('vm:prefer-inline') int _getUint16(int offset) => _buffer.getUint16(offset, Endian.little); + @pragma('vm:prefer-inline') int _getUint8(int offset) => _buffer.getUint8(offset); - - /// If the [byteList] is already a [Uint8List] return it. - /// Otherwise return a [Uint8List] copy of the [byteList]. - static Uint8List _asUint8List(List<int> byteList) { - if (byteList is Uint8List) { - return byteList; - } else { - return new Uint8List.fromList(byteList); - } - } } /// Class implemented by typed builders generated by flatc. @@ -152,12 +151,13 @@ class Builder { _buf = allocator.allocate(initialSize), _vTables = deduplicateTables ? [] : const [] { if (internStrings) { - _strings = new Map<String, int>(); + _strings = <String, int>{}; } } /// Calculate the finished buffer size (aligned). - int size() => _tail + ((-_tail) % _maxAlign); + @pragma('vm:prefer-inline') + int size() => _tail + ((-_tail) & (_maxAlign - 1)); /// Add the [field] with the given boolean [value]. The field is not added if /// the [value] is equal to [def]. Booleans are stored as 8-bit fields with @@ -178,7 +178,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofInt32, 1); _trackField(field); - _setInt32AtTail(_buf, _tail, value); + _setInt32AtTail(_tail, value); } } @@ -189,7 +189,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofInt16, 1); _trackField(field); - _setInt16AtTail(_buf, _tail, value); + _setInt16AtTail(_tail, value); } } @@ -200,7 +200,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofInt8, 1); _trackField(field); - _setInt8AtTail(_buf, _tail, value); + _setInt8AtTail(_tail, value); } } @@ -216,7 +216,7 @@ class Builder { if (offset != null) { _prepare(_sizeofUint32, 1); _trackField(field); - _setUint32AtTail(_buf, _tail, _tail - offset); + _setUint32AtTail(_tail, _tail - offset); } } @@ -227,7 +227,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofUint32, 1); _trackField(field); - _setUint32AtTail(_buf, _tail, value); + _setUint32AtTail(_tail, value); } } @@ -238,7 +238,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofUint16, 1); _trackField(field); - _setUint16AtTail(_buf, _tail, value); + _setUint16AtTail(_tail, value); } } @@ -249,7 +249,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofUint8, 1); _trackField(field); - _setUint8AtTail(_buf, _tail, value); + _setUint8AtTail(_tail, value); } } @@ -260,7 +260,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofFloat32, 1); _trackField(field); - _setFloat32AtTail(_buf, _tail, value); + _setFloat32AtTail(_tail, value); } } @@ -271,7 +271,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofFloat64, 1); _trackField(field); - _setFloat64AtTail(_buf, _tail, value); + _setFloat64AtTail(_tail, value); } } @@ -282,7 +282,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofUint64, 1); _trackField(field); - _setUint64AtTail(_buf, _tail, value); + _setUint64AtTail(_tail, value); } } @@ -293,7 +293,7 @@ class Builder { if (value != null && value != def) { _prepare(_sizeofInt64, 1); _trackField(field); - _setInt64AtTail(_buf, _tail, value); + _setInt64AtTail(_tail, value); } } @@ -339,13 +339,14 @@ class Builder { } } // Set the VTable offset. - _setInt32AtTail(_buf, tableTail, vTableTail - tableTail); + _setInt32AtTail(tableTail, vTableTail - tableTail); // Done with this table. _currentVTable = null; return tableTail; } /// Returns the finished buffer. You must call [finish] before accessing this. + @pragma('vm:prefer-inline') Uint8List get buffer { assert(_finished); final finishedSize = size(); @@ -363,11 +364,11 @@ class Builder { final requiredBytes = _sizeofUint32 * (fileIdentifier == null ? 1 : 2); _prepare(max(requiredBytes, _maxAlign), 1); final finishedSize = size(); - _setUint32AtTail(_buf, finishedSize, finishedSize - offset); + _setUint32AtTail(finishedSize, finishedSize - offset); if (fileIdentifier != null) { for (int i = 0; i < 4; i++) { - _setUint8AtTail(_buf, finishedSize - _sizeofUint32 - i, - fileIdentifier.codeUnitAt(i)); + _setUint8AtTail( + finishedSize - _sizeofUint32 - i, fileIdentifier.codeUnitAt(i)); } } @@ -375,7 +376,7 @@ class Builder { for (var i = sizeBeforePadding + 1; i <= finishedSize - requiredBytes; i++) { - _setUint8AtTail(_buf, i, 0); + _setUint8AtTail(i, 0); } _finished = true; } @@ -385,7 +386,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putFloat64(double value) { _prepare(_sizeofFloat64, 1); - _setFloat32AtTail(_buf, _tail, value); + _setFloat32AtTail(_tail, value); } /// Writes a Float32 to the tail of the buffer after preparing space for it. @@ -393,7 +394,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putFloat32(double value) { _prepare(_sizeofFloat32, 1); - _setFloat32AtTail(_buf, _tail, value); + _setFloat32AtTail(_tail, value); } /// Writes a Int64 to the tail of the buffer after preparing space for it. @@ -401,7 +402,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putInt64(int value) { _prepare(_sizeofInt64, 1); - _setInt64AtTail(_buf, _tail, value); + _setInt64AtTail(_tail, value); } /// Writes a Uint32 to the tail of the buffer after preparing space for it. @@ -409,7 +410,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putInt32(int value) { _prepare(_sizeofInt32, 1); - _setInt32AtTail(_buf, _tail, value); + _setInt32AtTail(_tail, value); } /// Writes a Uint16 to the tail of the buffer after preparing space for it. @@ -417,7 +418,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putInt16(int value) { _prepare(_sizeofInt16, 1); - _setInt16AtTail(_buf, _tail, value); + _setInt16AtTail(_tail, value); } /// Writes a Uint8 to the tail of the buffer after preparing space for it. @@ -433,7 +434,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putUint64(int value) { _prepare(_sizeofUint64, 1); - _setUint64AtTail(_buf, _tail, value); + _setUint64AtTail(_tail, value); } /// Writes a Uint32 to the tail of the buffer after preparing space for it. @@ -441,7 +442,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putUint32(int value) { _prepare(_sizeofUint32, 1); - _setUint32AtTail(_buf, _tail, value); + _setUint32AtTail(_tail, value); } /// Writes a Uint16 to the tail of the buffer after preparing space for it. @@ -449,7 +450,7 @@ class Builder { /// Updates the [offset] pointer. This method is intended for use when writing structs to the buffer. void putUint16(int value) { _prepare(_sizeofUint16, 1); - _setUint16AtTail(_buf, _tail, value); + _setUint16AtTail(_tail, value); } /// Writes a Uint8 to the tail of the buffer after preparing space for it. @@ -468,7 +469,7 @@ class Builder { _currentVTable = null; if (deduplicateTables) _vTables.clear(); if (_strings != null) { - _strings = new Map<String, int>(); + _strings = <String, int>{}; } } @@ -477,7 +478,7 @@ class Builder { if (_currentVTable != null) { throw new StateError('Inline tables are not supported.'); } - _currentVTable = new _VTable(numFields); + _currentVTable = _VTable(numFields); _currentTableEndTail = _tail; } @@ -504,10 +505,10 @@ class Builder { _prepare(_sizeofUint32, 1 + values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setUint32AtTail(_buf, tail, tail - value); + _setUint32AtTail(tail, tail - value); tail -= _sizeofUint32; } return result; @@ -519,10 +520,10 @@ class Builder { _prepare(_sizeofFloat64, values.length, additionalBytes: _sizeofUint32); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (double value in values) { - _setFloat64AtTail(_buf, tail, value); + _setFloat64AtTail(tail, value); tail -= _sizeofFloat64; } return result; @@ -534,10 +535,10 @@ class Builder { _prepare(_sizeofFloat32, 1 + values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (double value in values) { - _setFloat32AtTail(_buf, tail, value); + _setFloat32AtTail(tail, value); tail -= _sizeofFloat32; } return result; @@ -549,10 +550,10 @@ class Builder { _prepare(_sizeofInt64, values.length, additionalBytes: _sizeofUint32); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setInt64AtTail(_buf, tail, value); + _setInt64AtTail(tail, value); tail -= _sizeofInt64; } return result; @@ -564,10 +565,10 @@ class Builder { _prepare(_sizeofUint64, values.length, additionalBytes: _sizeofUint32); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setUint64AtTail(_buf, tail, value); + _setUint64AtTail(tail, value); tail -= _sizeofUint64; } return result; @@ -579,10 +580,10 @@ class Builder { _prepare(_sizeofUint32, 1 + values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setInt32AtTail(_buf, tail, value); + _setInt32AtTail(tail, value); tail -= _sizeofInt32; } return result; @@ -594,10 +595,10 @@ class Builder { _prepare(_sizeofUint32, 1 + values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setUint32AtTail(_buf, tail, value); + _setUint32AtTail(tail, value); tail -= _sizeofUint32; } return result; @@ -609,10 +610,10 @@ class Builder { _prepare(_sizeofUint32, 1, additionalBytes: 2 * values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setInt16AtTail(_buf, tail, value); + _setInt16AtTail(tail, value); tail -= _sizeofInt16; } return result; @@ -624,10 +625,10 @@ class Builder { _prepare(_sizeofUint32, 1, additionalBytes: 2 * values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setUint16AtTail(_buf, tail, value); + _setUint16AtTail(tail, value); tail -= _sizeofUint16; } return result; @@ -644,10 +645,10 @@ class Builder { _prepare(_sizeofUint32, 1, additionalBytes: values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setInt8AtTail(_buf, tail, value); + _setInt8AtTail(tail, value); tail -= _sizeofUint8; } return result; @@ -659,10 +660,10 @@ class Builder { _prepare(_sizeofUint32, 1, additionalBytes: values.length); final int result = _tail; int tail = _tail; - _setUint32AtTail(_buf, tail, values.length); + _setUint32AtTail(tail, values.length); tail -= _sizeofUint32; for (int value in values) { - _setUint8AtTail(_buf, tail, value); + _setUint8AtTail(tail, value); tail -= _sizeofUint8; } return result; @@ -715,7 +716,7 @@ class Builder { _buf.setUint8(offset++, char); } _buf.setUint8(offset, 0); // trailing zero - _setUint32AtTail(_buf, _tail, value.length); + _setUint32AtTail(_tail, value.length); return true; } @@ -724,7 +725,7 @@ class Builder { final bytes = utf8.encode(value) as Uint8List; final length = bytes.length; _prepare(4, 1, additionalBytes: length + 1); - _setUint32AtTail(_buf, _tail, length); + _setUint32AtTail(_tail, length); var offset = _buf.lengthInBytes - _tail + 4; for (int i = 0; i < length; i++) { _buf.setUint8(offset++, bytes[i]); @@ -749,9 +750,11 @@ class Builder { /// The number of bytes that have been written to the buffer so far. The /// most recently written byte is this many bytes from the end of the buffer. + @pragma('vm:prefer-inline') int get offset => _tail; /// Zero-pads the buffer, which may be required for some struct layouts. + @pragma('vm:prefer-inline') void pad(int howManyBytes) { for (int i = 0; i < howManyBytes; i++) putUint8(0); } @@ -759,6 +762,7 @@ class Builder { /// Prepare for writing the given `count` of scalars of the given `size`. /// Additionally allocate the specified `additionalBytes`. Update the current /// tail pointer to point at the allocated space. + @pragma('vm:prefer-inline') void _prepare(int size, int count, {int additionalBytes = 0}) { assert(!_finished); // Update the alignment. @@ -767,7 +771,7 @@ class Builder { } // Prepare amount of required space. int dataSize = size * count + additionalBytes; - int alignDelta = (-(_tail + dataSize)) % size; + int alignDelta = (-(_tail + dataSize)) & (size - 1); int bufSize = alignDelta + dataSize; // Ensure that we have the required amount of space. { @@ -775,7 +779,7 @@ class Builder { if (_tail + bufSize > oldCapacity) { int desiredNewCapacity = (oldCapacity + bufSize) * 2; int deltaCapacity = desiredNewCapacity - oldCapacity; - deltaCapacity += (-deltaCapacity) % _maxAlign; + deltaCapacity += (-deltaCapacity) & (_maxAlign - 1); int newCapacity = oldCapacity + deltaCapacity; _buf = _allocator.resize(_buf, newCapacity, _tail, 0); } @@ -783,7 +787,7 @@ class Builder { // zero out the added padding for (var i = _tail + 1; i <= _tail + alignDelta; i++) { - _setUint8AtTail(_buf, i, 0); + _setUint8AtTail(i, 0); } // Update the tail pointer. @@ -791,49 +795,48 @@ class Builder { } /// Record the offset of the given [field]. - void _trackField(int field) { - _currentVTable!.addField(field, _tail); - } + @pragma('vm:prefer-inline') + void _trackField(int field) => _currentVTable!.addField(field, _tail); - static void _setFloat64AtTail(ByteData _buf, int tail, double x) { - _buf.setFloat64(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setFloat64AtTail(int tail, double x) => + _buf.setFloat64(_buf.lengthInBytes - tail, x, Endian.little); - static void _setFloat32AtTail(ByteData _buf, int tail, double x) { - _buf.setFloat32(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setFloat32AtTail(int tail, double x) => + _buf.setFloat32(_buf.lengthInBytes - tail, x, Endian.little); - static void _setUint64AtTail(ByteData _buf, int tail, int x) { - _buf.setUint64(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setUint64AtTail(int tail, int x) => + _buf.setUint64(_buf.lengthInBytes - tail, x, Endian.little); - static void _setInt64AtTail(ByteData _buf, int tail, int x) { - _buf.setInt64(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setInt64AtTail(int tail, int x) => + _buf.setInt64(_buf.lengthInBytes - tail, x, Endian.little); - static void _setInt32AtTail(ByteData _buf, int tail, int x) { - _buf.setInt32(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setInt32AtTail(int tail, int x) => + _buf.setInt32(_buf.lengthInBytes - tail, x, Endian.little); - static void _setUint32AtTail(ByteData _buf, int tail, int x) { - _buf.setUint32(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setUint32AtTail(int tail, int x) => + _buf.setUint32(_buf.lengthInBytes - tail, x, Endian.little); - static void _setInt16AtTail(ByteData _buf, int tail, int x) { - _buf.setInt16(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setInt16AtTail(int tail, int x) => + _buf.setInt16(_buf.lengthInBytes - tail, x, Endian.little); - static void _setUint16AtTail(ByteData _buf, int tail, int x) { - _buf.setUint16(_buf.lengthInBytes - tail, x, Endian.little); - } + @pragma('vm:prefer-inline') + void _setUint16AtTail(int tail, int x) => + _buf.setUint16(_buf.lengthInBytes - tail, x, Endian.little); - static void _setInt8AtTail(ByteData _buf, int tail, int x) { - _buf.setInt8(_buf.lengthInBytes - tail, x); - } + @pragma('vm:prefer-inline') + void _setInt8AtTail(int tail, int x) => + _buf.setInt8(_buf.lengthInBytes - tail, x); - static void _setUint8AtTail(ByteData _buf, int tail, int x) { - _buf.setUint8(_buf.lengthInBytes - tail, x); - } + @pragma('vm:prefer-inline') + void _setUint8AtTail(int tail, int x) => + _buf.setUint8(_buf.lengthInBytes - tail, x); } /// Reader of lists of boolean values. @@ -843,11 +846,13 @@ class BoolListReader extends Reader<List<bool>> { const BoolListReader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint32; @override + @pragma('vm:prefer-inline') List<bool> read(BufferContext bc, int offset) => - new _FbBoolList(bc, bc.derefObject(offset)); + _FbBoolList(bc, bc.derefObject(offset)); } /// The reader of booleans. @@ -855,9 +860,11 @@ class BoolReader extends Reader<bool> { const BoolReader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint8; @override + @pragma('vm:prefer-inline') bool read(BufferContext bc, int offset) => bc._getInt8(offset) != 0; } @@ -868,9 +875,11 @@ class Float64ListReader extends Reader<List<double>> { const Float64ListReader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofFloat64; @override + @pragma('vm:prefer-inline') List<double> read(BufferContext bc, int offset) => new _FbFloat64List(bc, bc.derefObject(offset)); } @@ -879,9 +888,11 @@ class Float32ListReader extends Reader<List<double>> { const Float32ListReader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofFloat32; @override + @pragma('vm:prefer-inline') List<double> read(BufferContext bc, int offset) => new _FbFloat32List(bc, bc.derefObject(offset)); } @@ -890,9 +901,11 @@ class Float64Reader extends Reader<double> { const Float64Reader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofFloat64; @override + @pragma('vm:prefer-inline') double read(BufferContext bc, int offset) => bc._getFloat64(offset); } @@ -900,9 +913,11 @@ class Float32Reader extends Reader<double> { const Float32Reader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofFloat32; @override + @pragma('vm:prefer-inline') double read(BufferContext bc, int offset) => bc._getFloat32(offset); } @@ -910,9 +925,11 @@ class Int64Reader extends Reader<int> { const Int64Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofInt64; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getInt64(offset); } @@ -921,9 +938,11 @@ class Int32Reader extends Reader<int> { const Int32Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofInt32; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getInt32(offset); } @@ -932,9 +951,11 @@ class Int16Reader extends Reader<int> { const Int16Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofInt16; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getInt16(offset); } @@ -943,9 +964,11 @@ class Int8Reader extends Reader<int> { const Int8Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofInt8; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getInt8(offset); } @@ -964,6 +987,7 @@ class ListReader<E> extends Reader<List<E>> { const ListReader(this._elementReader, {this.lazy = true}); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint32; @override @@ -990,17 +1014,20 @@ abstract class Reader<T> { T read(BufferContext bc, int offset); /// Read the value of the given [field] in the given [object]. + @pragma('vm:prefer-inline') T vTableGet(BufferContext object, int offset, int field, T defaultValue) { int fieldOffset = _vTableFieldOffset(object, offset, field); return fieldOffset == 0 ? defaultValue : read(object, offset + fieldOffset); } /// Read the value of the given [field] in the given [object]. + @pragma('vm:prefer-inline') T? vTableGetNullable(BufferContext object, int offset, int field) { int fieldOffset = _vTableFieldOffset(object, offset, field); return fieldOffset == 0 ? null : read(object, offset + fieldOffset); } + @pragma('vm:prefer-inline') int _vTableFieldOffset(BufferContext object, int offset, int field) { int vTableSOffset = object._getInt32(offset); int vTableOffset = offset - vTableSOffset; @@ -1015,19 +1042,22 @@ class StringReader extends Reader<String> { const StringReader() : super(); @override + @pragma('vm:prefer-inline') int get size => 4; @override + @pragma('vm:prefer-inline') String read(BufferContext bc, int offset) { int strOffset = bc.derefObject(offset); int length = bc._getUint32(strOffset); - Uint8List bytes = bc._asUint8LIst(strOffset + 4, length); + Uint8List bytes = bc._asUint8List(strOffset + 4, length); if (_isLatin(bytes)) { return new String.fromCharCodes(bytes); } return utf8.decode(bytes); } + @pragma('vm:prefer-inline') static bool _isLatin(Uint8List bytes) { int length = bytes.length; for (int i = 0; i < length; i++) { @@ -1056,6 +1086,7 @@ abstract class TableReader<T> extends Reader<T> { const TableReader(); @override + @pragma('vm:prefer-inline') int get size => 4; /// Return the object at [offset]. @@ -1075,11 +1106,13 @@ class Uint32ListReader extends Reader<List<int>> { const Uint32ListReader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint32; @override + @pragma('vm:prefer-inline') List<int> read(BufferContext bc, int offset) => - new _FbUint32List(bc, bc.derefObject(offset)); + _FbUint32List(bc, bc.derefObject(offset)); } /// The reader of unsigned 64-bit integers. @@ -1089,9 +1122,11 @@ class Uint64Reader extends Reader<int> { const Uint64Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint64; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getUint64(offset); } @@ -1100,9 +1135,11 @@ class Uint32Reader extends Reader<int> { const Uint32Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint32; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getUint32(offset); } @@ -1113,11 +1150,13 @@ class Uint16ListReader extends Reader<List<int>> { const Uint16ListReader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint32; @override + @pragma('vm:prefer-inline') List<int> read(BufferContext bc, int offset) => - new _FbUint16List(bc, bc.derefObject(offset)); + _FbUint16List(bc, bc.derefObject(offset)); } /// The reader of unsigned 32-bit integers. @@ -1125,9 +1164,11 @@ class Uint16Reader extends Reader<int> { const Uint16Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint16; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getUint16(offset); } @@ -1138,11 +1179,13 @@ class Uint8ListReader extends Reader<List<int>> { const Uint8ListReader(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint32; @override + @pragma('vm:prefer-inline') List<int> read(BufferContext bc, int offset) => - new _FbUint8List(bc, bc.derefObject(offset)); + _FbUint8List(bc, bc.derefObject(offset)); } /// The reader of unsigned 8-bit integers. @@ -1150,9 +1193,11 @@ class Uint8Reader extends Reader<int> { const Uint8Reader() : super(); @override + @pragma('vm:prefer-inline') int get size => _sizeofUint8; @override + @pragma('vm:prefer-inline') int read(BufferContext bc, int offset) => bc._getUint8(offset); } @@ -1161,9 +1206,8 @@ class _FbFloat64List extends _FbList<double> { _FbFloat64List(BufferContext bc, int offset) : super(bc, offset); @override - double operator [](int i) { - return bc._getFloat64(offset + 4 + 8 * i); - } + @pragma('vm:prefer-inline') + double operator [](int i) => bc._getFloat64(offset + 4 + 8 * i); } /// The list backed by 32-bit values - Float32. @@ -1171,9 +1215,8 @@ class _FbFloat32List extends _FbList<double> { _FbFloat32List(BufferContext bc, int offset) : super(bc, offset); @override - double operator [](int i) { - return bc._getFloat32(offset + 4 + 4 * i); - } + @pragma('vm:prefer-inline') + double operator [](int i) => bc._getFloat32(offset + 4 + 4 * i); } /// List backed by a generic object which may have any size. @@ -1186,6 +1229,7 @@ class _FbGenericList<E> extends _FbList<E> { : super(bp, offset); @override + @pragma('vm:prefer-inline') E operator [](int i) { _items ??= List<E?>.filled(length, null); E? item = _items![i]; @@ -1206,10 +1250,8 @@ abstract class _FbList<E> extends Object with ListMixin<E> implements List<E> { _FbList(this.bc, this.offset); @override - int get length { - _length ??= bc._getUint32(offset); - return _length!; - } + @pragma('vm:prefer-inline') + int get length => _length ??= bc._getUint32(offset); @override void set length(int i) => @@ -1225,9 +1267,8 @@ class _FbUint32List extends _FbList<int> { _FbUint32List(BufferContext bc, int offset) : super(bc, offset); @override - int operator [](int i) { - return bc._getUint32(offset + 4 + 4 * i); - } + @pragma('vm:prefer-inline') + int operator [](int i) => bc._getUint32(offset + 4 + 4 * i); } /// List backed by 16-bit unsigned integers. @@ -1235,9 +1276,8 @@ class _FbUint16List extends _FbList<int> { _FbUint16List(BufferContext bc, int offset) : super(bc, offset); @override - int operator [](int i) { - return bc._getUint16(offset + 4 + 2 * i); - } + @pragma('vm:prefer-inline') + int operator [](int i) => bc._getUint16(offset + 4 + 2 * i); } /// List backed by 8-bit unsigned integers. @@ -1245,9 +1285,8 @@ class _FbUint8List extends _FbList<int> { _FbUint8List(BufferContext bc, int offset) : super(bc, offset); @override - int operator [](int i) { - return bc._getUint8(offset + 4 + i); - } + @pragma('vm:prefer-inline') + int operator [](int i) => bc._getUint8(offset + 4 + i); } /// List backed by 8-bit unsigned integers. @@ -1255,9 +1294,8 @@ class _FbBoolList extends _FbList<bool> { _FbBoolList(BufferContext bc, int offset) : super(bc, offset); @override - bool operator [](int i) { - return bc._getUint8(offset + 4 + i) == 1 ? true : false; - } + @pragma('vm:prefer-inline') + bool operator [](int i) => bc._getUint8(offset + 4 + i) == 1 ? true : false; } /// Class that describes the structure of a table. |