Skip to content

perf(csharp): add csharp benchmarks and optimize hot serialization paths#3396

Merged
chaokunyang merged 6 commits intoapache:mainfrom
chaokunyang:csharp_benchmarks
Feb 24, 2026
Merged

perf(csharp): add csharp benchmarks and optimize hot serialization paths#3396
chaokunyang merged 6 commits intoapache:mainfrom
chaokunyang:csharp_benchmarks

Conversation

@chaokunyang
Copy link
Copy Markdown
Collaborator

@chaokunyang chaokunyang commented Feb 23, 2026

Why?

  • Add a first-class C# benchmark suite aligned with existing benchmark datasets and serializers.
  • Improve C# runtime hot paths while keeping benchmark comparisons fair.
  • Fix enum behavior so undefined numeric enum values can round-trip instead of failing.

What does this PR do?

  • Adds benchmarks/csharp benchmark harness (Program.cs, models, serializer adapters, run.sh, and report generator) for fory, protobuf-net, and MessagePack.
  • Benchmarks cover Struct, Sample, MediaContent and list variants for both serialize/deserialize.
  • Uses directly serializable benchmark models for all three serializers (removed protobuf conversion adapter overhead from timed path).
  • Updates benchmark report generation to keep throughput and size reporting separate, and adds a C++-style Serialized Data Sizes (bytes) matrix.
  • Optimizes C# runtime hot paths: reusable ByteWriter/ByteReader, span-based string encode/decode path, and cached allowed wire-type sets.
  • Improves generated/runtime schema handling: CheckStructVersion is wired through contexts/generated serializers with cached no-trackRef schema hash.
  • Optimizes collection and enum hot paths: sealed collection element types avoid redundant type-info writes; enum serializer uses cached maps and preserves unknown numeric enum values on read.
  • Simplifies config surface by removing skip-root/reflection-fallback flags and setting default MaxDepth to 20.
  • Adds/updates tests for schema version behavior and undefined enum round-trip.

Related issues

#1017 #3387
#3397

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Benchmark

  • Command:
    • cd benchmarks/csharp && ./run.sh --duration 3 --warmup 1
  • Selected results (ops/sec):
Datatype Operation fory protobuf msgpack
MediaContent Serialize 2,787,853 2,241,239 2,111,882
MediaContent Deserialize 2,601,783 1,343,196 1,252,873
MediaContentList Serialize 588,552 443,250 505,927
MediaContentList Deserialize 570,945 289,274 279,718
  • Aggregate throughput (mean ops/sec across all benchmark cases):
    • fory: 5,278,631
    • protobuf: 2,020,540
    • msgpack: 1,982,782

@chaokunyang chaokunyang merged commit 1cdcb42 into apache:main Feb 24, 2026
58 checks passed
@chaokunyang chaokunyang mentioned this pull request Feb 24, 2026
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants