summaryrefslogtreecommitdiff
path: root/dart
diff options
context:
space:
mode:
authorIvan Dlugos <6349682+vaind@users.noreply.github.com>2021-07-16 00:50:12 +0200
committerGitHub <noreply@github.com>2021-07-15 15:50:12 -0700
commitdd5bb55cad8f75162a5a43dfb2eac8bcac3ccd55 (patch)
treee9feb18829e0dc7a39a796e7492f547a382f5080 /dart
parente8423da1b73c3efe5fcbe56b37efe18a5b162b90 (diff)
downloadflatbuffers-dd5bb55cad8f75162a5a43dfb2eac8bcac3ccd55.tar.gz
flatbuffers-dd5bb55cad8f75162a5a43dfb2eac8bcac3ccd55.tar.bz2
flatbuffers-dd5bb55cad8f75162a5a43dfb2eac8bcac3ccd55.zip
Dart - make vTable deduplication optional (#6734)
Diffstat (limited to 'dart')
-rw-r--r--dart/example/monster_my_game.sample_generated.dart6
-rw-r--r--dart/lib/flat_buffers.dart37
-rw-r--r--dart/test/monster_test_my_game.example2_generated.dart2
-rw-r--r--dart/test/monster_test_my_game.example_generated.dart18
-rw-r--r--dart/test/monster_test_my_game_generated.dart2
5 files changed, 36 insertions, 29 deletions
diff --git a/dart/example/monster_my_game.sample_generated.dart b/dart/example/monster_my_game.sample_generated.dart
index f5ab93ca..eae52b61 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 1ab410d3..ca635feb 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<int> _vTables = List<int>.filled(16, 0, growable: true)
- ..length = 0;
+ final List<int> _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<String, int>();
}
@@ -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;
+ 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<String, int>();
}
diff --git a/dart/test/monster_test_my_game.example2_generated.dart b/dart/test/monster_test_my_game.example2_generated.dart
index 60ac656a..82c3f4ba 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 c21d8516..5d11bb71 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 eef9e73d..7f0fcda5 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;