google / google/flatbuffers

Optimize Dart Builder.writeListFloat32() for Float32List

Open
#8,916 5 comments 0 reactions 0 assignees View on GitHub
dart pr-requested
Dominant language
C++
Stars
26.5k
Forks
3.7k
PR merge metrics
No merged PRs in 30d

Description

The Dart flat_buffer Builder is slow when writing large Float32Lists because it iterates the elements one by one instead of copying the underlying memory buffer. The method can be optimized to special case when the given list is Float32List and then use faster memory copy operations. This aligns with the intentions of using typed data for cpu efficient code. Suggested code to change from:

```dart
int writeListFloat32(List values) {
assert(!_inVTable);
_prepare(_sizeofFloat32, 1 + values.length);
final result = _tail;
var tail = _tail;
_setUint32AtTail(tail, values.length);
tail -= _sizeofUint32;
for (final value in values) {
_setFloat32AtTail(tail, value);
tail -= _sizeofFloat32;
}
return result;
}
```

to:

```dart
int writeListFloat32(List values) {
assert(!_inVTable);
_prepare(_sizeofFloat32, 1 + values.length);
final result = _tail;
var tail = _tail;
_setUint32AtTail(tail, values.length);
tail -= _sizeofUint32;

if (values is Float32List && Endian.host == Endian.little) {
// Fast copying of Float32List
// Get the underlying bytes from Float32List (zero-copy view)
final floatBytes = values.buffer.asUint8List(values.offsetInBytes, values.lengthInBytes);

// Get the target location in our buffer
final targetOffset = _buf.lengthInBytes - tail;

// Bulk copy the float bytes directly to the buffer
final targetBytes = _buf.buffer.asUint8List(_buf.offsetInBytes);
targetBytes.setRange(targetOffset, targetOffset + floatBytes.length, floatBytes);

tail -= floatBytes.length;
} else {
// non-Float32List, iterate one element at a time
for (final value in values) {
_setFloat32AtTail(tail, value);
tail -= _sizeofFloat32;
}
}

return result;
}
```

If this makes sense I'd be happy to share a PR. Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.