Improve efficiency at computing checksums
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index a6b7e13..ea445ea 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -92,25 +92,31 @@
}
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].toSigned(8);
+ }
+ for (var i = _checksumEnd; i < blockSize; i++) {
+ result += this[i].toSigned(8);
}
return result;
@@ -123,6 +129,14 @@
return true;
}
+
+ bool get isAllZeroes {
+ for (var i = 0; i < length; i++) {
+ if (this[i] != 0) return false;
+ }
+
+ return true;
+ }
}
bool isNotAscii(int i) => i > 128;
@@ -200,14 +214,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.