Skip to content

Commit 8a43653

Browse files
committed
flatbuffers - make deduplicateTables optional
1 parent 05d8d1e commit 8a43653

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

objectbox/lib/flatbuffers/flat_buffers.dart

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ class Builder {
132132
final int initialSize;
133133

134134
/// The list of existing VTable(s).
135-
final List<int> _vTables = List<int>.filled(16, 0, growable: true)
136-
..length = 0;
135+
final List<int> _vTables;
136+
137+
final bool deduplicateTables;
137138

138139
ByteData _buf;
139140

@@ -170,8 +171,10 @@ class Builder {
170171
this.initialSize: 1024,
171172
bool internStrings = false,
172173
Allocator allocator = const DefaultAllocator(),
174+
this.deduplicateTables = true,
173175
}) : _allocator = allocator,
174-
_buf = allocator.allocate(initialSize) {
176+
_buf = allocator.allocate(initialSize),
177+
_vTables = List.empty(growable: deduplicateTables) {
175178
if (internStrings) {
176179
_strings = new Map<String, int>();
177180
}
@@ -333,17 +336,20 @@ class Builder {
333336
int? vTableTail;
334337
{
335338
currentVTable.computeFieldOffsets(tableTail);
339+
336340
// Try to find an existing compatible VTable.
337-
// Search backward - more likely to have recently used one
338-
for (int i = _vTables.length - 1; i >= 0; i--) {
339-
final int vt2Offset = _vTables[i];
340-
final int vt2Start = _buf.lengthInBytes - vt2Offset;
341-
final int vt2Size = _buf.getUint16(vt2Start, Endian.little);
342-
343-
if (currentVTable._vTableSize == vt2Size &&
344-
currentVTable._offsetsMatch(vt2Start, _buf)) {
345-
vTableTail = vt2Offset;
346-
break;
341+
if (deduplicateTables) {
342+
// Search backward - more likely to have recently used one
343+
for (int i = _vTables.length - 1; i >= 0; i--) {
344+
final int vt2Offset = _vTables[i];
345+
final int vt2Start = _buf.lengthInBytes - vt2Offset;
346+
final int vt2Size = _buf.getUint16(vt2Start, Endian.little);
347+
348+
if (currentVTable._vTableSize == vt2Size &&
349+
currentVTable._offsetsMatch(vt2Start, _buf)) {
350+
vTableTail = vt2Offset;
351+
break;
352+
}
347353
}
348354
}
349355
// Write a new VTable.
@@ -352,7 +358,7 @@ class Builder {
352358
vTableTail = _tail;
353359
currentVTable.tail = vTableTail;
354360
currentVTable.output(_buf, _buf.lengthInBytes - _tail);
355-
_vTables.add(currentVTable.tail);
361+
if (deduplicateTables) _vTables.add(currentVTable.tail);
356362
}
357363
}
358364
// Set the VTable offset.
@@ -484,7 +490,7 @@ class Builder {
484490
_maxAlign = 1;
485491
_tail = 0;
486492
_currentVTable = null;
487-
_vTables.length = 0;
493+
if (deduplicateTables) _vTables.clear();
488494
if (_strings != null) {
489495
_strings = new Map<String, int>();
490496
}
@@ -1298,6 +1304,7 @@ class _VTable {
12981304
fieldOffsets[field] = offset + 1;
12991305
}
13001306

1307+
@pragma('vm:prefer-inline')
13011308
bool _offsetsMatch(int vt2Start, ByteData buf) {
13021309
assert(offsetsComputed);
13031310
for (int i = 0; i < fieldOffsets.length; i++) {
@@ -1310,6 +1317,7 @@ class _VTable {
13101317
}
13111318

13121319
/// Fill the [fieldOffsets] field.
1320+
@pragma('vm:prefer-inline')
13131321
void computeFieldOffsets(int tableTail) {
13141322
assert(!offsetsComputed);
13151323
offsetsComputed = true;
@@ -1321,6 +1329,7 @@ class _VTable {
13211329

13221330
/// Outputs this VTable to [buf], which is is expected to be aligned to 16-bit
13231331
/// and have at least [numOfUint16] 16-bit words available.
1332+
@pragma('vm:prefer-inline')
13241333
void output(ByteData buf, int bufOffset) {
13251334
assert(offsetsComputed);
13261335
// VTable size.

objectbox/lib/src/native/bindings/flatbuffers.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ class BuilderWithCBuffer {
2626
BuilderWithCBuffer({int initialSize = 256, int resetIfLargerThan = 64 * 1024})
2727
: _initialSize = initialSize,
2828
_resetIfLargerThan = resetIfLargerThan {
29-
_fbb = fb.Builder(initialSize: initialSize, allocator: _allocator);
29+
_fbb = fb.Builder(
30+
initialSize: initialSize,
31+
allocator: _allocator,
32+
deduplicateTables: false, // we always have exactly one table
33+
);
3034
}
3135

3236
@pragma('vm:prefer-inline')

0 commit comments

Comments
 (0)