Skip to content

Commit aa52476

Browse files
committed
flatbuffers - optimize field offsets handling
1 parent 8a43653 commit aa52476

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

objectbox/lib/flatbuffers/flat_buffers.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,8 @@ class _FbBoolList extends _FbList<bool> {
12761276
class _VTable {
12771277
static const int _metadataLength = 4;
12781278

1279-
// Note: fieldOffsets start as "tail offsets"+1 and are then transformed to
1280-
// actual offsets when a table is finished, by calling [computeFieldOffsets].
1279+
// Note: fieldOffsets start as "tail offsets" and are then transformed by
1280+
// [computeFieldOffsets()] to actual offsets when a table is finished.
12811281
final Uint32List fieldOffsets;
12821282
static const uint32Max = 4294967295;
12831283
bool offsetsComputed = false;
@@ -1298,10 +1298,9 @@ class _VTable {
12981298
@pragma('vm:prefer-inline')
12991299
void addField(int field, int offset) {
13001300
assert(!offsetsComputed);
1301-
// We need to increase the offset by 1 to later (in [computeFieldOffsets])
1302-
// recognize fields that haven't been set (Uint32List initializes to 0s).
1301+
assert(offset > 0); // it's impossible for field to start at the buffer end
13031302
assert(offset < uint32Max);
1304-
fieldOffsets[field] = offset + 1;
1303+
fieldOffsets[field] = offset;
13051304
}
13061305

13071306
@pragma('vm:prefer-inline')
@@ -1322,8 +1321,9 @@ class _VTable {
13221321
assert(!offsetsComputed);
13231322
offsetsComputed = true;
13241323
for (var i = 0; i < fieldOffsets.length; i++) {
1325-
int fieldTail = fieldOffsets[i];
1326-
fieldOffsets[i] = fieldTail == 0 ? 0 : tableTail - fieldTail + 1;
1324+
if (fieldOffsets[i] != 0) {
1325+
fieldOffsets[i] = tableTail - fieldOffsets[i];
1326+
}
13271327
}
13281328
}
13291329

@@ -1340,8 +1340,7 @@ class _VTable {
13401340
bufOffset += 2;
13411341
// Field offsets.
13421342
for (int i = 0; i < fieldOffsets.length; i++) {
1343-
final fieldOffset = fieldOffsets[i];
1344-
buf.setUint16(bufOffset, fieldOffset, Endian.little);
1343+
buf.setUint16(bufOffset, fieldOffsets[i], Endian.little);
13451344
bufOffset += 2;
13461345
}
13471346
}

0 commit comments

Comments
 (0)