Skip to content

Remove unneeded GetTypeInfo() calls#2206

Merged
AArnott merged 5 commits into
MessagePack-CSharp:masterfrom
Bykiev:RemoveGetTypeInfo
Aug 17, 2025
Merged

Remove unneeded GetTypeInfo() calls#2206
AArnott merged 5 commits into
MessagePack-CSharp:masterfrom
Bykiev:RemoveGetTypeInfo

Conversation

@Bykiev
Copy link
Copy Markdown
Contributor

@Bykiev Bykiev commented Jun 30, 2025

Improve performance by removing unneeded GetTypeInfo() calls

// * Summary *

BenchmarkDotNet v0.14.0, Windows 10 (10.0.19044.3086/21H2/November2021Update)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 8.0.17 (8.0.1725.26602), X64 RyuJIT AVX2
  ShortRun : .NET 8.0.17 (8.0.1725.26602), X64 RyuJIT AVX2

Job=ShortRun  EnvironmentVariables=DOTNET_TieredPGO=0  Jit=RyuJit
Platform=X64  Runtime=.NET 8.0  IterationCount=1
LaunchCount=1  WarmupCount=1

Before:

Method Serializer Mean Error DataSize Gen0 Allocated
AnswerDeserialize MessagePack_v1 3.320 us NA - 0.4234 1784 B
AnswerDeserialize MessagePack_v2 3.660 us NA - 0.4196 1784 B
AnswerDeserialize MessagePackLz4_v1 3.689 us NA - 0.4234 1784 B
AnswerDeserialize MessagePackLz4_v2 4.342 us NA - 0.4196 1784 B
AnswerDeserialize MsgPack_v1_str_lz4 4.327 us NA - 0.4234 1784 B
AnswerDeserialize MsgPack_v1_string 3.481 us NA - 0.4196 1784 B
AnswerDeserialize MsgPack_v2_opt 4.699 us NA - 0.4196 1784 B
AnswerDeserialize MsgPack_v2_str_lz4 7.532 us NA - 0.4196 1784 B
AnswerDeserialize MsgPack_v2_string 5.366 us NA - 0.4196 1784 B
AnswerDeserialize MsgPackCli 8.482 us NA - 0.6714 2864 B
AnswerSerialize MessagePack_v1 3.749 us NA 470B 0.1183 496 B
AnswerSerialize MessagePack_v2 3.608 us NA 470B 0.1183 496 B
AnswerSerialize MessagePackLz4_v1 4.451 us NA 454B 0.1144 480 B
AnswerSerialize MessagePackLz4_v2 3.685 us NA 455B 0.1144 480 B
AnswerSerialize MsgPack_v1_str_lz4 6.307 us NA 867B 0.2060 888 B
AnswerSerialize MsgPack_v1_string 4.310 us NA 1264B 0.3052 1288 B
AnswerSerialize MsgPack_v2_opt 2.945 us NA 434B 0.1106 464 B
AnswerSerialize MsgPack_v2_str_lz4 7.113 us NA 868B 0.2136 896 B
AnswerSerialize MsgPack_v2_string 4.062 us NA 1264B 0.3052 1288 B
AnswerSerialize MsgPackCli 5.745 us NA 521B 0.8774 3696 B

After:

Method Serializer Mean Error DataSize Gen0 Gen1 Allocated
AnswerDeserialize MessagePack_v1 2.986 us NA - 0.4234 - 1784 B
AnswerDeserialize MessagePack_v2 3.242 us NA - 0.4234 - 1784 B
AnswerDeserialize MessagePackLz4_v1 2.699 us NA - 0.4234 - 1784 B
AnswerDeserialize MessagePackLz4_v2 3.530 us NA - 0.4234 - 1784 B
AnswerDeserialize MsgPack_v1_str_lz4 3.607 us NA - 0.4196 - 1784 B
AnswerDeserialize MsgPack_v1_string 3.266 us NA - 0.4234 - 1784 B
AnswerDeserialize MsgPack_v2_opt 3.743 us NA - 0.4196 - 1784 B
AnswerDeserialize MsgPack_v2_str_lz4 5.544 us NA - 0.4196 - 1784 B
AnswerDeserialize MsgPack_v2_string 4.548 us NA - 0.4196 - 1784 B
AnswerDeserialize MsgPackCli 6.066 us NA - 0.6790 - 2856 B
AnswerSerialize MessagePack_v1 2.944 us NA 469B 0.1183 - 496 B
AnswerSerialize MessagePack_v2 3.868 us NA 469B 0.1144 - 496 B
AnswerSerialize MessagePackLz4_v1 4.695 us NA 450B 0.1144 - 480 B
AnswerSerialize MessagePackLz4_v2 5.065 us NA 451B 0.1144 - 480 B
AnswerSerialize MsgPack_v1_str_lz4 5.485 us NA 860B 0.2060 - 888 B
AnswerSerialize MsgPack_v1_string 4.105 us NA 1262B 0.3052 - 1288 B
AnswerSerialize MsgPack_v2_opt 3.234 us NA 433B 0.1106 - 464 B
AnswerSerialize MsgPack_v2_str_lz4 6.603 us NA 861B 0.2060 - 888 B
AnswerSerialize MsgPack_v2_string 4.034 us NA 1262B 0.3052 - 1288 B
AnswerSerialize MsgPackCli 4.528 us NA 521B 0.8774 - 3696 B

Improve performcance by removing unneeded GetTypeInfo() calls
@Bykiev
Copy link
Copy Markdown
Contributor Author

Bykiev commented Jun 30, 2025

@dotnet-policy-service agree

@Bykiev
Copy link
Copy Markdown
Contributor Author

Bykiev commented Jun 30, 2025

We can remove IsNullable, IsPublic, IsAnonymous, HasPrivateCtorForSerialization extension methods for TypeInfo if acceptable (a sort of breaking change, though it's in internal namespace)

@Bykiev
Copy link
Copy Markdown
Contributor Author

Bykiev commented Aug 12, 2025

@AArnott, can you please take a look?

Copy link
Copy Markdown
Collaborator

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

(a sort of breaking change, though it's in internal namespace)

We need to be very careful. Public APIs in an 'internal' namespace are likely called by source generated code and thus outside of our control and likely to break if the library changes them.

I do think one method should be reverted. But if you can prove me wrong with sharplab.io release build JIT output, I'll accept what you have here.

Comment thread src/MessagePack/MessagePackSecurity.cs
@Bykiev
Copy link
Copy Markdown
Contributor Author

Bykiev commented Aug 13, 2025

I do think one method should be reverted. But if you can prove me wrong with sharplab.io release build JIT output, I'll accept what you have here.

Sorry, what method are you talking about? I'm ready to revert it if can cause any breaking changes.

@AArnott AArnott requested a review from Copilot August 17, 2025 14:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes performance by removing unnecessary GetTypeInfo() calls throughout the MessagePack codebase. The optimization leverages the fact that many properties that previously required TypeInfo can now be accessed directly on Type objects.

  • Replaces type.GetTypeInfo() calls with direct Type property access
  • Removes intermediate TypeInfo variables where no longer needed
  • Adds new overloaded extension methods to support Type instead of TypeInfo

Reviewed Changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
SkipClrVisibilityChecks.cs Updates method parameter and all usages from TypeInfo to Type
ImmutableCollectionResolver.cs Removes TypeInfo usage and adds Type extension method for IsNullable
DynamicUnionResolver.cs Eliminates TypeInfo variables and uses Type properties directly
DynamicObjectResolver.cs Comprehensive removal of TypeInfo usage, updates method calls and property access
DynamicGenericResolver.cs Removes TypeInfo variables and converts interface checks to use Type directly
DynamicEnumResolver.cs Simplifies type checking by using Type instead of TypeInfo
DynamicEnumAsStringResolver.cs Updates nullable and enum type checking to use Type directly
DynamicEnumAsStringIgnoreCaseResolver.cs Similar TypeInfo to Type conversion for enum handling
AttributeFormatterResolver.cs Direct Type usage for custom attribute retrieval
MessagePackSerializer.NonGeneric.cs Updates value type checking in expression building
MessagePackSecurity.cs Refactors equality comparer selection to use Type directly
Sequence`1.cs Direct Type property access for primitive type checking
ReflectionExtensions.cs Adds Type overloads for existing TypeInfo extension methods
ILGeneratorExtensions.cs Updates IL generation code to use Type properties
DynamicAssemblyFactory.cs Updates visibility check method call to use Type
TypelessFormatter.cs Removes unnecessary TypeInfo variables in serialization logic
PrimitiveObjectFormatter.cs Adds Type overload for IsSupportedType method
EnumAsStringFormatter`1.cs Uses Type variable to reduce repeated typeof() calls
DynamicObjectTypeFallbackFormatter.cs Eliminates TypeInfo usage in fallback formatting

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Comment thread src/MessagePack/Resolvers/DynamicObjectResolver.cs Outdated
Comment thread src/MessagePack/Resolvers/DynamicGenericResolver.cs Outdated
Comment thread src/MessagePack/Resolvers/DynamicGenericResolver.cs Outdated
Comment thread src/MessagePack/Resolvers/DynamicGenericResolver.cs
Comment thread src/MessagePack/Resolvers/DynamicGenericResolver.cs Outdated
Comment thread src/MessagePack/Resolvers/DynamicGenericResolver.cs
Comment thread src/MessagePack/Resolvers/DynamicGenericResolver.cs Outdated
Copy link
Copy Markdown
Collaborator

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just a few redundant methods to consolidate and it will be good.

Comment thread src/MessagePack/Formatters/PrimitiveObjectFormatter.cs
Comment thread src/MessagePack/Internal/ReflectionExtensions.cs
Comment thread src/MessagePack/Internal/ReflectionExtensions.cs
Comment thread src/MessagePack/Resolvers/ImmutableCollectionResolver.cs
Comment thread src/MessagePack/Formatters/PrimitiveObjectFormatter.cs
@Bykiev Bykiev requested a review from AArnott August 17, 2025 17:32
Copy link
Copy Markdown
Collaborator

@AArnott AArnott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

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