-
Notifications
You must be signed in to change notification settings - Fork 189
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
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1ed72f9
Add pragmas
osa1 5073a40
Inline _readPacked manually
osa1 8c41b3c
More inline pragmas
osa1 edfe97a
Remove redundant checkNotNull calls from pre-null-safe days
osa1 b7288f5
Merge branch 'redundant_checkNotNull' into vm_inlines
osa1 69562f6
More tweaks
osa1 899badb
More tweaks, not sure worth it
osa1 dc6e8fb
Fix warnings
osa1 b799c4c
Merge branch 'redundant_checkNotNull' into vm_inlines
osa1 f39ddce
Fix formatting
osa1 23b884d
Make _mergeFromCodedBufferReader parameters final
osa1 20155c5
Merge remote-tracking branch 'origin/master' into vm_inlines
osa1 f069031
Merge remote-tracking branch 'origin/master' into vm_inlines
osa1 f1d60d2
Add benchmarks
osa1 c5925a7
Fix warns
osa1 fa0a056
Add inlines for Wasm
osa1 addc0e0
Add a never-inline
osa1 2dc80da
Remove `final`s
osa1 5df3444
Keep sink alive
osa1 086c55c
Update changelog, package version
osa1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.