You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fory.apache.org by "chaokunyang (via GitHub)" <gi...@apache.org> on 2026/03/03 13:26:33 UTC
[PR] perf(c#): add csharp benchmark results [fory]
chaokunyang opened a new pull request, #3451:
URL: https://github.com/apache/fory/pull/3451
<!--
**Thanks for contributing to Apache Fory™.**
**If this is your first time opening a PR on fory, you can refer to [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fory™** community has requirements on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).
- Apache Fory™ has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here.
-->
## Why?
<!-- Describe the purpose of this PR. -->
## What does this PR do?
<!-- Describe the details of this PR. -->
## Related issues
<!--
Is there any related issue? If this PR closes them you say say fix/closes:
- #xxxx0
- #xxxx1
- Fixes #xxxx2
-->
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an issue](https://github.com/apache/fory/issues/new/choose) describing the need to do so and update the document if necessary.
Delete section if not applicable.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here.
Delete section if not applicable.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org
Re: [PR] perf(c#): add csharp benchmark results [fory]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3451:
URL: https://github.com/apache/fory/pull/3451#discussion_r2881905419
##########
csharp/src/Fory/Context.cs:
##########
@@ -268,6 +383,8 @@ public ReadContext(
public bool CheckStructVersion { get; }
+ public bool ShouldConsumeCompatibleTypeMeta => _shouldConsumeCompatibleTypeMeta;
Review Comment:
[P3] Unused compatible-meta expectation state is dead code
`ShouldConsumeCompatibleTypeMeta`, `ReplaceCompatibleTypeMetaExpectation`, and `RestoreCompatibleTypeMetaExpectation` are introduced but never read by any serializer/runtime path in this PR. Keeping this state machine unused adds maintenance overhead and makes compatible-read flow harder to reason about without delivering behavior.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org
Re: [PR] perf(c#): add csharp benchmark results [fory]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3451:
URL: https://github.com/apache/fory/pull/3451#discussion_r2881905176
##########
csharp/src/Fory/TypeResolver.cs:
##########
@@ -33,13 +33,14 @@ public sealed class TypeResolver
typeof(TypeResolver).GetMethod(
nameof(CreateNullableSerializerTypeInfo),
BindingFlags.NonPublic | BindingFlags.Instance)!;
- private static readonly HashSet<TypeId> CompatibleStructAllowedWireTypes =
- [
- TypeId.Struct,
- TypeId.NamedStruct,
- TypeId.CompatibleStruct,
- TypeId.NamedCompatibleStruct,
- ];
+
+ private static class GenericTypeCache<T>
Review Comment:
[P2] Static generic cache pins `TypeResolver` instances
`GenericTypeCache<T>` is static and stores a strong `TypeResolver` reference. That means the last resolver used for each `T` is rooted for the lifetime of the AppDomain, which can retain large registration graphs and serializer caches after runtimes should be collectable. This is a regression from the previous instance-only lookup path and can create long-lived memory retention in services that create/destroy runtimes.
##########
csharp/src/Fory/TypeMeta.cs:
##########
@@ -553,6 +569,125 @@ public static TypeMeta Decode(ByteReader reader)
header >> (int)(64 - TypeMetaConstants.TypeMetaNumHashBits));
}
+ /// <summary>
+ /// Assigns local sorted field indexes for a remote compatible type meta.
+ /// The result is written to each remote field's <see cref="TypeMetaFieldInfo.AssignedFieldId"/>:
+ /// - local sorted field index when a compatible local field is found
+ /// - -1 when no compatible local field is found and the field should be skipped
+ /// </summary>
+ public static void AssignFieldIds(
+ TypeMeta remoteTypeMeta,
+ IReadOnlyList<TypeMetaFieldInfo> localFieldInfos)
+ {
+ ArgumentNullException.ThrowIfNull(remoteTypeMeta);
+ ArgumentNullException.ThrowIfNull(localFieldInfos);
+
+ Dictionary<string, (int Index, TypeMetaFieldInfo Field)> localByName = new(localFieldInfos.Count, StringComparer.Ordinal);
+ Dictionary<short, (int Index, TypeMetaFieldInfo Field)> localById = new(localFieldInfos.Count);
+ for (int i = 0; i < localFieldInfos.Count; i++)
+ {
+ TypeMetaFieldInfo localField = localFieldInfos[i];
+ if (!string.IsNullOrEmpty(localField.FieldName))
+ {
+ localByName.TryAdd(localField.FieldName, (i, localField));
+ }
+
+ if (localField.FieldId.HasValue && localField.FieldId.Value >= 0)
Review Comment:
[P2] Duplicate `FieldAttribute.Id` values are not rejected
Compatible field-id mapping uses `localById.TryAdd(...)`, so duplicate ids are silently accepted and only the first field is kept in the lookup. With duplicate ids on a generated model, multiple remote fields can resolve to the same local field index and produce silent data loss/overwrite during compatible reads. The generator should emit a diagnostic (or runtime should throw) when duplicate non-negative field ids exist in a type.
##########
csharp/src/Fory/Context.cs:
##########
@@ -268,6 +383,8 @@ public ReadContext(
public bool CheckStructVersion { get; }
+ public bool ShouldConsumeCompatibleTypeMeta => _shouldConsumeCompatibleTypeMeta;
Review Comment:
[P3] Unused compatible-meta expectation state is dead code
`ShouldConsumeCompatibleTypeMeta`, `ReplaceCompatibleTypeMetaExpectation`, and `RestoreCompatibleTypeMetaExpectation` are introduced but never read by any serializer/runtime path in this PR. Keeping this state machine unused adds maintenance overhead and makes compatible-read flow harder to reason about without delivering behavior.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org
Re: [PR] perf(c#): add csharp benchmark results [fory]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3451:
URL: https://github.com/apache/fory/pull/3451#discussion_r2881905308
##########
csharp/src/Fory/TypeMeta.cs:
##########
@@ -553,6 +569,125 @@ public static TypeMeta Decode(ByteReader reader)
header >> (int)(64 - TypeMetaConstants.TypeMetaNumHashBits));
}
+ /// <summary>
+ /// Assigns local sorted field indexes for a remote compatible type meta.
+ /// The result is written to each remote field's <see cref="TypeMetaFieldInfo.AssignedFieldId"/>:
+ /// - local sorted field index when a compatible local field is found
+ /// - -1 when no compatible local field is found and the field should be skipped
+ /// </summary>
+ public static void AssignFieldIds(
+ TypeMeta remoteTypeMeta,
+ IReadOnlyList<TypeMetaFieldInfo> localFieldInfos)
+ {
+ ArgumentNullException.ThrowIfNull(remoteTypeMeta);
+ ArgumentNullException.ThrowIfNull(localFieldInfos);
+
+ Dictionary<string, (int Index, TypeMetaFieldInfo Field)> localByName = new(localFieldInfos.Count, StringComparer.Ordinal);
+ Dictionary<short, (int Index, TypeMetaFieldInfo Field)> localById = new(localFieldInfos.Count);
+ for (int i = 0; i < localFieldInfos.Count; i++)
+ {
+ TypeMetaFieldInfo localField = localFieldInfos[i];
+ if (!string.IsNullOrEmpty(localField.FieldName))
+ {
+ localByName.TryAdd(localField.FieldName, (i, localField));
+ }
+
+ if (localField.FieldId.HasValue && localField.FieldId.Value >= 0)
Review Comment:
[P2] Duplicate `FieldAttribute.Id` values are not rejected
Compatible field-id mapping uses `localById.TryAdd(...)`, so duplicate ids are silently accepted and only the first field is kept in the lookup. With duplicate ids on a generated model, multiple remote fields can resolve to the same local field index and produce silent data loss/overwrite during compatible reads. The generator should emit a diagnostic (or runtime should throw) when duplicate non-negative field ids exist in a type.
##########
csharp/src/Fory/TypeResolver.cs:
##########
@@ -33,13 +33,14 @@ public sealed class TypeResolver
typeof(TypeResolver).GetMethod(
nameof(CreateNullableSerializerTypeInfo),
BindingFlags.NonPublic | BindingFlags.Instance)!;
- private static readonly HashSet<TypeId> CompatibleStructAllowedWireTypes =
- [
- TypeId.Struct,
- TypeId.NamedStruct,
- TypeId.CompatibleStruct,
- TypeId.NamedCompatibleStruct,
- ];
+
+ private static class GenericTypeCache<T>
Review Comment:
[P2] Static generic cache pins `TypeResolver` instances
`GenericTypeCache<T>` is static and stores a strong `TypeResolver` reference. That means the last resolver used for each `T` is rooted for the lifetime of the AppDomain, which can retain large registration graphs and serializer caches after runtimes should be collectable. This is a regression from the previous instance-only lookup path and can create long-lived memory retention in services that create/destroy runtimes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org
Re: [PR] perf(c#): add csharp benchmark results [fory]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang merged PR #3451:
URL: https://github.com/apache/fory/pull/3451
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org