Use lazy property initialization in generated C# RPC classes#725
Use lazy property initialization in generated C# RPC classes#725stephentoub merged 1 commit intomainfrom
Conversation
Cross-SDK Consistency Review ✅This PR modifies the C# code generator to use lazy property initialization ( Language-Specific AnalysisTypeScript (
Python (
Go (
Conclusion✅ No cross-SDK changes needed. This optimization is C#-specific and addresses a performance concern unique to how C# handles property initialization during JSON deserialization. The eager initialization pattern ( The other languages handle property initialization fundamentally differently:
This is an appropriate language-specific optimization that correctly does not apply to the other SDK implementations.
|
There was a problem hiding this comment.
Pull request overview
Updates the C# code generator and regenerated RPC DTOs to avoid eager allocations for required collection and nested RPC-class properties by switching to lazy initialization using the field ??= pattern.
Changes:
- Adjusted
scripts/codegen/csharp.tsto emit lazy-initialized accessors for requiredList<>/Dictionary<>and nested RPC class properties. - Regenerated
dotnet/src/Generated/Rpc.csto apply the new accessor pattern to affected properties.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| scripts/codegen/csharp.ts | Changes RPC property emission to generate lazy-initialized accessors for certain required reference types. |
| dotnet/src/Generated/Rpc.cs | Regenerated RPC DTOs reflecting the new lazy-init accessor pattern. |
Switched property initializations to lazy accessors for lists, dictionaries, and custom types in C# RPC classes. Updated codegen in csharp.ts to emit these accessors, improving memory usage and consistency.
8547a85 to
4e35e0b
Compare
Cross-SDK Consistency Review ✅I've reviewed this PR for consistency across all four SDK implementations (Node.js/TypeScript, Python, Go, and .NET). SummaryNo consistency concerns. This PR implements a C#-specific performance optimization that doesn't require equivalent changes in other SDKs. AnalysisThe PR changes C# RPC class codegen from eager initialization: public List(Model) Models { get; set; } = [];
public ModelCapabilities Capabilities { get; set; } = new();To lazy initialization using C# 13's public List(Model) Models { get => field ??= []; set; }
public ModelCapabilities Capabilities { get => field ??= new(); set; }This avoids allocating empty collections/objects during construction that are immediately overwritten by the JSON deserializer—a C#-specific issue. Why Other SDKs Don't Need ThisThe other three SDKs don't have this allocation inefficiency:
Each language's codegen already handles deserialization efficiently given that language's idioms and runtime behavior. ConclusionThis optimization is appropriate and maintains cross-SDK semantic consistency (all SDKs correctly deserialize RPC types), while taking advantage of C#-specific features to improve performance. No changes needed in other SDKs. 🎯
|
The C# codegen was emitting eager initialization for collection and nested-class properties:
During deserialization, this allocates an empty collection/object only to immediately overwrite it. This PR changes the codegen to use lazy initialization via
fieldkeyword instead:This avoids unnecessary allocations when the property is set by the deserializer, while still ensuring a non-null default when accessed without prior assignment.
Changes