Skip to content

Commit 4e35e0b

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 11dde6e commit 4e35e0b

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

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 SessionLogResult
@@ -321,7 +321,7 @@ public class SessionWorkspaceListFilesResult
321321
{
322322
/// <summary>Relative file paths in the workspace files directory</summary>
323323
[JsonPropertyName("files")]
324-
public List<string> Files { get; set; } = [];
324+
public List<string> Files { get => field ??= []; set; }
325325
}
326326

327327
internal class SessionWorkspaceListFilesRequest
@@ -397,7 +397,7 @@ public class SessionAgentListResult
397397
{
398398
/// <summary>Available custom agents</summary>
399399
[JsonPropertyName("agents")]
400-
public List<Agent> Agents { get; set; } = [];
400+
public List<Agent> Agents { get => field ??= []; set; }
401401
}
402402

403403
internal class SessionAgentListRequest
@@ -454,7 +454,7 @@ public class SessionAgentSelectResult
454454
{
455455
/// <summary>The newly selected custom agent</summary>
456456
[JsonPropertyName("agent")]
457-
public SessionAgentSelectResultAgent Agent { get; set; } = new();
457+
public SessionAgentSelectResultAgent Agent { get => field ??= new(); set; }
458458
}
459459

460460
internal class SessionAgentSelectRequest

scripts/codegen/csharp.ts

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

567567
let defaultVal = "";
568+
let propAccessors = "{ get; set; }";
568569
if (isReq && !csharpType.endsWith("?")) {
569570
if (csharpType === "string") defaultVal = " = string.Empty;";
570571
else if (csharpType === "object") defaultVal = " = null!;";
571-
else if (csharpType.startsWith("List<") || csharpType.startsWith("Dictionary<")) defaultVal = " = [];";
572-
else if (emittedRpcClasses.has(csharpType)) defaultVal = " = new();";
572+
else if (csharpType.startsWith("List<") || csharpType.startsWith("Dictionary<")) {
573+
propAccessors = "{ get => field ??= []; set; }";
574+
} else if (emittedRpcClasses.has(csharpType)) {
575+
propAccessors = "{ get => field ??= new(); set; }";
576+
}
573577
}
574-
lines.push(` public ${csharpType} ${csharpName} { get; set; }${defaultVal}`);
578+
lines.push(` public ${csharpType} ${csharpName} ${propAccessors}${defaultVal}`);
575579
if (i < props.length - 1) lines.push("");
576580
}
577581
lines.push(`}`);

0 commit comments

Comments
 (0)