Skip to content

Commit 8547a85

Browse files
committed
Use lazy property initialization in C# RPC classes
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.
1 parent 396e8b3 commit 8547a85

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

dotnet/src/Generated/Rpc.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public class ModelCapabilitiesLimits
6161
public class ModelCapabilities
6262
{
6363
[JsonPropertyName("supports")]
64-
public ModelCapabilitiesSupports Supports { get; set; } = new();
64+
public ModelCapabilitiesSupports Supports { get => field ??= new(); set; }
6565

6666
[JsonPropertyName("limits")]
67-
public ModelCapabilitiesLimits Limits { get; set; } = new();
67+
public ModelCapabilitiesLimits Limits { get => field ??= new(); set; }
6868
}
6969

7070
/// <summary>Policy state (if applicable)</summary>
@@ -96,7 +96,7 @@ public class Model
9696

9797
/// <summary>Model capabilities and limits</summary>
9898
[JsonPropertyName("capabilities")]
99-
public ModelCapabilities Capabilities { get; set; } = new();
99+
public ModelCapabilities Capabilities { get => field ??= new(); set; }
100100

101101
/// <summary>Policy state (if applicable)</summary>
102102
[JsonPropertyName("policy")]
@@ -119,7 +119,7 @@ public class ModelsListResult
119119
{
120120
/// <summary>List of available models with full metadata</summary>
121121
[JsonPropertyName("models")]
122-
public List<Model> Models { get; set; } = [];
122+
public List<Model> Models { get => field ??= []; set; }
123123
}
124124

125125
public class Tool
@@ -149,7 +149,7 @@ public class ToolsListResult
149149
{
150150
/// <summary>List of available built-in tools with metadata</summary>
151151
[JsonPropertyName("tools")]
152-
public List<Tool> Tools { get; set; } = [];
152+
public List<Tool> Tools { get => field ??= []; set; }
153153
}
154154

155155
internal class ToolsListRequest
@@ -189,7 +189,7 @@ public class AccountGetQuotaResult
189189
{
190190
/// <summary>Quota snapshots keyed by type (e.g., chat, completions, premium_interactions)</summary>
191191
[JsonPropertyName("quotaSnapshots")]
192-
public Dictionary<string, AccountGetQuotaResultQuotaSnapshotsValue> QuotaSnapshots { get; set; } = [];
192+
public Dictionary<string, AccountGetQuotaResultQuotaSnapshotsValue> QuotaSnapshots { get => field ??= []; set; }
193193
}
194194

195195
public class SessionModelGetCurrentResult
@@ -296,7 +296,7 @@ public class SessionWorkspaceListFilesResult
296296
{
297297
/// <summary>Relative file paths in the workspace files directory</summary>
298298
[JsonPropertyName("files")]
299-
public List<string> Files { get; set; } = [];
299+
public List<string> Files { get => field ??= []; set; }
300300
}
301301

302302
internal class SessionWorkspaceListFilesRequest
@@ -372,7 +372,7 @@ public class SessionAgentListResult
372372
{
373373
/// <summary>Available custom agents</summary>
374374
[JsonPropertyName("agents")]
375-
public List<Agent> Agents { get; set; } = [];
375+
public List<Agent> Agents { get => field ??= []; set; }
376376
}
377377

378378
internal class SessionAgentListRequest
@@ -429,7 +429,7 @@ public class SessionAgentSelectResult
429429
{
430430
/// <summary>The newly selected custom agent</summary>
431431
[JsonPropertyName("agent")]
432-
public SessionAgentSelectResultAgent Agent { get; set; } = new();
432+
public SessionAgentSelectResultAgent Agent { get => field ??= new(); set; }
433433
}
434434

435435
internal class SessionAgentSelectRequest

scripts/codegen/csharp.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,17 @@ function emitRpcClass(className: string, schema: JSONSchema7, visibility: "publi
533533
lines.push(` [JsonPropertyName("${propName}")]`);
534534

535535
let defaultVal = "";
536+
let propAccessors = "{ get; set; }";
536537
if (isReq && !csharpType.endsWith("?")) {
537538
if (csharpType === "string") defaultVal = " = string.Empty;";
538539
else if (csharpType === "object") defaultVal = " = null!;";
539-
else if (csharpType.startsWith("List<") || csharpType.startsWith("Dictionary<")) defaultVal = " = [];";
540-
else if (emittedRpcClasses.has(csharpType)) defaultVal = " = new();";
540+
else if (csharpType.startsWith("List<") || csharpType.startsWith("Dictionary<")) {
541+
propAccessors = "{ get => field ??= []; set; }";
542+
} else if (emittedRpcClasses.has(csharpType)) {
543+
propAccessors = "{ get => field ??= new(); set; }";
544+
}
541545
}
542-
lines.push(` public ${csharpType} ${csharpName} { get; set; }${defaultVal}`);
546+
lines.push(` public ${csharpType} ${csharpName} ${propAccessors}${defaultVal}`);
543547
if (i < props.length - 1) lines.push("");
544548
}
545549
lines.push(`}`);

0 commit comments

Comments
 (0)