Skip to content

Commit cbd4ffd

Browse files
committed
Access arrays from larger index to smaller to allow omitting bounds checks
1 parent 440e67b commit cbd4ffd

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

pkgs/fixnum/lib/src/int32.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ class Int32 implements IntX {
435435
@override
436436
List<int> toBytes() {
437437
var result = List<int>.filled(4, 0);
438-
result[0] = _i & 0xff;
439-
result[1] = (_i >> 8) & 0xff;
440-
result[2] = (_i >> 16) & 0xff;
441438
result[3] = (_i >> 24) & 0xff;
439+
result[2] = (_i >> 16) & 0xff;
440+
result[1] = (_i >> 8) & 0xff;
441+
result[0] = _i & 0xff;
442442
return result;
443443
}
444444

pkgs/fixnum/lib/src/int64_native.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ class Int64 implements IntX {
4848
(bytes[0] & 0xFF));
4949

5050
/// Constructs an [Int64] from the first 8 bytes in [bytes], as big endian.
51-
factory Int64.fromBytesBigEndian(List<int> bytes) =>
52-
Int64(((bytes[0] & 0xFF) << 56) |
53-
((bytes[1] & 0xFF) << 48) |
54-
((bytes[2] & 0xFF) << 40) |
55-
((bytes[3] & 0xFF) << 32) |
56-
((bytes[4] & 0xFF) << 24) |
57-
((bytes[5] & 0xFF) << 16) |
58-
((bytes[6] & 0xFF) << 8) |
59-
(bytes[7] & 0xFF));
51+
factory Int64.fromBytesBigEndian(List<int> bytes) => Int64((bytes[7] & 0xFF) |
52+
((bytes[6] & 0xFF) << 8) |
53+
((bytes[5] & 0xFF) << 16) |
54+
((bytes[4] & 0xFF) << 24) |
55+
((bytes[3] & 0xFF) << 32) |
56+
((bytes[2] & 0xFF) << 40) |
57+
((bytes[1] & 0xFF) << 48) |
58+
((bytes[0] & 0xFF) << 56));
6059

6160
/// Parses [source] as a decimal numeral.
6261
///
@@ -310,14 +309,14 @@ class Int64 implements IntX {
310309
@override
311310
List<int> toBytes() {
312311
final result = List<int>.filled(8, 0);
313-
result[0] = _i & 0xff;
314-
result[1] = (_i >> 8) & 0xff;
315-
result[2] = (_i >> 16) & 0xff;
316-
result[3] = (_i >> 24) & 0xff;
317-
result[4] = (_i >> 32) & 0xff;
318-
result[5] = (_i >> 40) & 0xff;
319-
result[6] = (_i >> 48) & 0xff;
320312
result[7] = (_i >> 56) & 0xff;
313+
result[6] = (_i >> 48) & 0xff;
314+
result[5] = (_i >> 40) & 0xff;
315+
result[4] = (_i >> 32) & 0xff;
316+
result[3] = (_i >> 24) & 0xff;
317+
result[2] = (_i >> 16) & 0xff;
318+
result[1] = (_i >> 8) & 0xff;
319+
result[0] = _i & 0xff;
321320
return result;
322321
}
323322

0 commit comments

Comments
 (0)