Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions benchmarks/bin/binary_decode_packed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ class PackedEnumDecodingBenchmark extends BenchmarkBase {
}
}

class PackedSparseEnumDecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedSparseEnumDecodingBenchmark() : super('PackedSparseEnumDecoding') {
final rand = Random(123);
final message = PackedFields();
final numEnums = SparseEnum.values.length;
for (var i = 0; i < 1000000; i += 1) {
message.sparseEnum.add(SparseEnum.values[rand.nextInt(numEnums)]);
}
encoded = message.writeToBuffer();
}

@override
void run() {
sink = PackedFields()..mergeFromBuffer(encoded);
}
}

void main() {
PackedInt32DecodingBenchmark().report();
PackedInt64DecodingBenchmark().report();
Expand All @@ -190,6 +209,7 @@ void main() {
PackedSint64DecodingBenchmark().report();
PackedBoolDecodingBenchmark().report();
PackedEnumDecodingBenchmark().report();
PackedSparseEnumDecodingBenchmark().report();

if (int.parse('1') == 0) print(sink);
}
21 changes: 21 additions & 0 deletions benchmarks/protos/packed_fields.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ message PackedFields {
repeated bool packedBool = 7 [packed = true];
repeated Enum1 packedEnum1 = 8 [packed = true];
repeated Enum2 packedEnum2 = 9 [packed = true];
repeated SparseEnum sparseEnum = 10 [packed = true];
}

enum Enum1 {
Expand All @@ -27,3 +28,23 @@ enum Enum2 {
ENUM_2_4 = 4;
ENUM_2_5 = 5;
}

// An enum with large gaps between the known values, and with negative values.
//
// This will be slower to decode as the varint to enum value mapping needs to be
// done with binary search, or map lookup etc.
enum SparseEnum {
ENUM_ZERO = 0;
ENUM_MIN_INT = -2147483648;
ENUM_1 = -1000000000;
ENUM_2 = -100000000;
ENUM_3 = -10000000;
ENUM_4 = -1000000;
ENUM_5 = -100000;
ENUM_6 = 100000;
ENUM_7 = 1000000;
ENUM_8 = 10000000;
ENUM_9 = 100000000;
ENUM_10 = 1000000000;
ENUM_MAX_INT = 2147483647;
}