Dart - make vTable deduplication optional (#6734)

This commit is contained in:
Ivan Dlugos 2021-07-16 00:50:12 +02:00 committed by GitHub
parent e8423da1b7
commit dd5bb55cad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 54 additions and 47 deletions

View File

@ -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;

View File

@ -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;
// 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<String, int>();
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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";

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;