Skip to content

Improve packed field decoding #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 17 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
171 changes: 171 additions & 0 deletions benchmarks/bin/binary_decode_packed.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:math';
import 'dart:typed_data';

import 'package:fixnum/fixnum.dart';
import 'package:protobuf_benchmarks/benchmark_base.dart';
import 'package:protobuf_benchmarks/generated/packed_fields.pb.dart';

PackedFields? sink;

class PackedInt32DecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedInt32DecodingBenchmark() : super('PackedInt32Decoding') {
final rand = Random(123);
final message = PackedFields();
for (var i = 0; i < 1000000; i += 1) {
message.packedInt32.add(rand.nextInt(2147483647));
}
encoded = message.writeToBuffer();
}

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

class PackedInt64DecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedInt64DecodingBenchmark() : super('PackedInt64Decoding') {
final rand = Random(123);
final message = PackedFields();
for (var i = 0; i < 1000000; i += 1) {
// Note: `Random` cannot generate more than the number below.
message.packedInt64.add(Int64(rand.nextInt(4294967296)));
}
encoded = message.writeToBuffer();
}

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

class PackedUint32DecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedUint32DecodingBenchmark() : super('PackedUint32Decoding') {
final rand = Random(123);
final message = PackedFields();
for (var i = 0; i < 1000000; i += 1) {
message.packedUint32.add(rand.nextInt(4294967295));
}
encoded = message.writeToBuffer();
}

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

class PackedUint64DecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedUint64DecodingBenchmark() : super('PackedUint64Decoding') {
final rand = Random(123);
final message = PackedFields();
for (var i = 0; i < 1000000; i += 1) {
// Note: `Random` cannot generate more than the number below.
message.packedUint64.add(Int64(rand.nextInt(4294967296)));
}
encoded = message.writeToBuffer();
}

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

class PackedSint32DecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedSint32DecodingBenchmark() : super('PackedSint32Decoding') {
final rand = Random(123);
final message = PackedFields();
for (var i = 0; i < 1000000; i += 1) {
message.packedSint32.add(rand.nextInt(2147483647));
}
encoded = message.writeToBuffer();
}

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

class PackedSint64DecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedSint64DecodingBenchmark() : super('PackedSint64Decoding') {
final rand = Random(123);
final message = PackedFields();
for (var i = 0; i < 1000000; i += 1) {
// Note: `Random` cannot generate more than the number below.
message.packedSint64.add(Int64(rand.nextInt(4294967296)));
}
encoded = message.writeToBuffer();
}

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

class PackedBoolDecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

PackedBoolDecodingBenchmark() : super('PackedBoolDecoding') {
final rand = Random(123);
final message = PackedFields();
for (var i = 0; i < 1000000; i += 1) {
message.packedBool.add(rand.nextBool());
}
encoded = message.writeToBuffer();
}

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

class PackedEnumDecodingBenchmark extends BenchmarkBase {
late final Uint8List encoded;

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

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

void main() {
PackedInt32DecodingBenchmark().report();
PackedInt64DecodingBenchmark().report();
PackedUint32DecodingBenchmark().report();
PackedUint64DecodingBenchmark().report();
PackedSint32DecodingBenchmark().report();
PackedSint64DecodingBenchmark().report();
PackedBoolDecodingBenchmark().report();
PackedEnumDecodingBenchmark().report();
}
20 changes: 20 additions & 0 deletions benchmarks/protos/packed_fields.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";

message PackedFields {
repeated int32 packedInt32 = 1 [packed = true];
repeated int64 packedInt64 = 2 [packed = true];
repeated uint32 packedUint32 = 3 [packed = true];
repeated uint64 packedUint64 = 4 [packed = true];
repeated sint32 packedSint32 = 5 [packed = true];
repeated sint64 packedSint64 = 6 [packed = true];
repeated bool packedBool = 7 [packed = true];
repeated Enum packedEnum = 8 [packed = true];
}

enum Enum {
ENUM_1 = 0;
ENUM_2 = 1;
ENUM_3 = 2;
ENUM_4 = 4;
ENUM_5 = 5;
}
1 change: 1 addition & 0 deletions benchmarks/tool/compile_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SIMPLE_PROTOS=(
"protos/google_message1_proto2.proto"
"protos/google_message1_proto3.proto"
"protos/google_message2.proto"
"protos/packed_fields.proto"
)

set -x
Expand Down
Loading