diff --git a/dart/example/monster_my_game.sample_generated.dart b/dart/example/monster_my_game.sample_generated.dart index f5ab93cae..eae52b61b 100644 --- a/dart/example/monster_my_game.sample_generated.dart +++ b/dart/example/monster_my_game.sample_generated.dart @@ -166,7 +166,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -334,7 +334,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -416,7 +416,7 @@ class WeaponObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/dart/lib/flat_buffers.dart b/dart/lib/flat_buffers.dart index 1ab410d39..ca635feb2 100644 --- a/dart/lib/flat_buffers.dart +++ b/dart/lib/flat_buffers.dart @@ -108,8 +108,9 @@ class Builder { final int initialSize; /// The list of existing VTable(s). - final List _vTables = List.filled(16, 0, growable: true) - ..length = 0; + final List _vTables; + + final bool deduplicateTables; ByteData _buf; @@ -146,8 +147,10 @@ class Builder { this.initialSize: 1024, bool internStrings = false, Allocator allocator = const DefaultAllocator(), + this.deduplicateTables = true, }) : _allocator = allocator, - _buf = allocator.allocate(initialSize) { + _buf = allocator.allocate(initialSize), + _vTables = deduplicateTables ? [] : const [] { if (internStrings) { _strings = new Map(); } @@ -309,26 +312,30 @@ class Builder { int? vTableTail; { currentVTable.computeFieldOffsets(tableTail); - // Try to find an existing compatible VTable. - // Search backward - more likely to have recently used one - for (int i = _vTables.length - 1; i >= 0; i--) { - final int vt2Offset = _vTables[i]; - final int vt2Start = _buf.lengthInBytes - vt2Offset; - final int vt2Size = _buf.getUint16(vt2Start, Endian.little); - if (currentVTable._vTableSize == vt2Size && - currentVTable._offsetsMatch(vt2Start, _buf)) { - vTableTail = vt2Offset; - break; + // Try to find an existing compatible VTable. + if (deduplicateTables) { + // Search backward - more likely to have recently used one + for (int i = _vTables.length - 1; i >= 0; i--) { + final int vt2Offset = _vTables[i]; + final int vt2Start = _buf.lengthInBytes - vt2Offset; + final int vt2Size = _buf.getUint16(vt2Start, Endian.little); + + if (currentVTable._vTableSize == vt2Size && + currentVTable._offsetsMatch(vt2Start, _buf)) { + vTableTail = vt2Offset; + break; + } } } + // Write a new VTable. if (vTableTail == null) { _prepare(_sizeofUint16, _currentVTable!.numOfUint16); vTableTail = _tail; currentVTable.tail = vTableTail; currentVTable.output(_buf, _buf.lengthInBytes - _tail); - _vTables.add(currentVTable.tail); + if (deduplicateTables) _vTables.add(currentVTable.tail); } } // Set the VTable offset. @@ -459,7 +466,7 @@ class Builder { _maxAlign = 1; _tail = 0; _currentVTable = null; - _vTables.length = 0; + if (deduplicateTables) _vTables.clear(); if (_strings != null) { _strings = new Map(); } diff --git a/dart/test/monster_test_my_game.example2_generated.dart b/dart/test/monster_test_my_game.example2_generated.dart index 60ac656a8..82c3f4ba1 100644 --- a/dart/test/monster_test_my_game.example2_generated.dart +++ b/dart/test/monster_test_my_game.example2_generated.dart @@ -69,7 +69,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/dart/test/monster_test_my_game.example_generated.dart b/dart/test/monster_test_my_game.example_generated.dart index c21d8516d..5d11bb71e 100644 --- a/dart/test/monster_test_my_game.example_generated.dart +++ b/dart/test/monster_test_my_game.example_generated.dart @@ -346,7 +346,7 @@ class TestObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -444,7 +444,7 @@ class TestSimpleTableWithEnumObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -591,7 +591,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -689,7 +689,7 @@ class AbilityObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -797,7 +797,7 @@ class StructOfStructsObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -923,7 +923,7 @@ class StatObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -1021,7 +1021,7 @@ class ReferrableObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -1890,7 +1890,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -2139,7 +2139,7 @@ class TypeAliasesObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/dart/test/monster_test_my_game_generated.dart b/dart/test/monster_test_my_game_generated.dart index eef9e73d7..7f0fcda50 100644 --- a/dart/test/monster_test_my_game_generated.dart +++ b/dart/test/monster_test_my_game_generated.dart @@ -69,7 +69,7 @@ class InParentNamespaceObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/src/idl_gen_dart.cpp b/src/idl_gen_dart.cpp index 18f1ea2cf..a788b20e1 100644 --- a/src/idl_gen_dart.cpp +++ b/src/idl_gen_dart.cpp @@ -953,7 +953,7 @@ class DartGenerator : public BaseGenerator { code += " @override\n"; code += " Uint8List toBytes([String? fileIdentifier]) {\n"; code += " " + _kFb + ".Builder fbBuilder = new "; - code += _kFb + ".Builder();\n"; + code += _kFb + ".Builder(deduplicateTables: false);\n"; code += " int offset = finish(fbBuilder);\n"; code += " fbBuilder.finish(offset, fileIdentifier);\n"; code += " return fbBuilder.buffer;\n"; diff --git a/tests/monster_extra_my_game_generated.dart b/tests/monster_extra_my_game_generated.dart index db5213085..115ba0cc2 100644 --- a/tests/monster_extra_my_game_generated.dart +++ b/tests/monster_extra_my_game_generated.dart @@ -224,7 +224,7 @@ class MonsterExtraObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/tests/monster_test_my_game.example2_generated.dart b/tests/monster_test_my_game.example2_generated.dart index 60ac656a8..82c3f4ba1 100644 --- a/tests/monster_test_my_game.example2_generated.dart +++ b/tests/monster_test_my_game.example2_generated.dart @@ -69,7 +69,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/tests/monster_test_my_game.example_generated.dart b/tests/monster_test_my_game.example_generated.dart index c21d8516d..5d11bb71e 100644 --- a/tests/monster_test_my_game.example_generated.dart +++ b/tests/monster_test_my_game.example_generated.dart @@ -346,7 +346,7 @@ class TestObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -444,7 +444,7 @@ class TestSimpleTableWithEnumObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -591,7 +591,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -689,7 +689,7 @@ class AbilityObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -797,7 +797,7 @@ class StructOfStructsObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -923,7 +923,7 @@ class StatObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -1021,7 +1021,7 @@ class ReferrableObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -1890,7 +1890,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -2139,7 +2139,7 @@ class TypeAliasesObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/tests/monster_test_my_game_generated.dart b/tests/monster_test_my_game_generated.dart index eef9e73d7..7f0fcda50 100644 --- a/tests/monster_test_my_game_generated.dart +++ b/tests/monster_test_my_game_generated.dart @@ -69,7 +69,7 @@ class InParentNamespaceObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart b/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart index b91848bfb..69ce98b14 100644 --- a/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart +++ b/tests/namespace_test/namespace_test1_namespace_a.namespace_b_generated.dart @@ -189,7 +189,7 @@ class TableInNestedNSObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -287,7 +287,7 @@ class StructInNestedNSObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/tests/namespace_test/namespace_test2_namespace_a_generated.dart b/tests/namespace_test/namespace_test2_namespace_a_generated.dart index d80d2050d..dd908bb40 100644 --- a/tests/namespace_test/namespace_test2_namespace_a_generated.dart +++ b/tests/namespace_test/namespace_test2_namespace_a_generated.dart @@ -165,7 +165,7 @@ class TableInFirstNSObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; @@ -265,7 +265,7 @@ class SecondTableInAObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer; diff --git a/tests/namespace_test/namespace_test2_namespace_c_generated.dart b/tests/namespace_test/namespace_test2_namespace_c_generated.dart index ba39b44e3..f94355391 100644 --- a/tests/namespace_test/namespace_test2_namespace_c_generated.dart +++ b/tests/namespace_test/namespace_test2_namespace_c_generated.dart @@ -117,7 +117,7 @@ class TableInCObjectBuilder extends fb.ObjectBuilder { /// Convenience method to serialize to byte list. @override Uint8List toBytes([String? fileIdentifier]) { - fb.Builder fbBuilder = new fb.Builder(); + fb.Builder fbBuilder = new fb.Builder(deduplicateTables: false); int offset = finish(fbBuilder); fbBuilder.finish(offset, fileIdentifier); return fbBuilder.buffer;