Improve performance of computing checksums
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index c55917b..8650c7c 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -93,30 +93,44 @@
}
int computeUnsignedHeaderChecksum() {
- var result = 0;
+ // Accessing the last element first helps the VM eliminate bounds checks in
+ // the loops below.
+ this[blockSize - 1];
+ var result = checksumLength * _checksumPlaceholder;
- for (var i = 0; i < length; i++) {
- result += (i < checksumOffset || i >= _checksumEnd)
- ? this[i] // Not in range of where the checksum is written
- : _checksumPlaceholder;
+ for (var i = 0; i < checksumOffset; i++) {
+ result += this[i];
+ }
+ for (var i = _checksumEnd; i < blockSize; i++) {
+ result += this[i];
}
return result;
}
int computeSignedHeaderChecksum() {
- var result = 0;
+ this[blockSize - 1];
+ // Note that _checksumPlaceholder.toSigned(8) == _checksumPlaceholder
+ var result = checksumLength * _checksumPlaceholder;
- for (var i = 0; i < length; i++) {
- // Note that _checksumPlaceholder.toSigned(8) == _checksumPlaceholder
- result += (i < checksumOffset || i >= _checksumEnd)
- ? this[i].toSigned(8)
- : _checksumPlaceholder;
+ for (var i = 0; i < checksumOffset; i++) {
+ result += this[i];
+ }
+ for (var i = _checksumEnd; i < blockSize; i++) {
+ result += this[i];
}
return result;
}
+ bool get isAllZeroes {
+ for (var i = 0; i < length; i++) {
+ if (this[i] != 0) return false;
+ }
+
+ return true;
+ }
+
bool matchesHeader(List<int> header, {int offset = magicOffset}) {
for (var i = 0; i < header.length; i++) {
if (this[offset + i] != header[i]) return false;
@@ -201,14 +215,6 @@
final $this = this;
return $this is Uint8List ? $this : Uint8List.fromList(this);
}
-
- bool get isAllZeroes {
- for (var i = 0; i < length; i++) {
- if (this[i] != 0) return false;
- }
-
- return true;
- }
}
/// Generates a chunked stream of [length] zeroes.