mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-20 10:35:05 +00:00
Dart - make vTable deduplication optional (#6734)
This commit is contained in:
@@ -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>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user