Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Avoid a few unnecessary Dictionary allocations in Abstractions #3188

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Microsoft.SemanticKernel.AI;
/// </summary>
public class AIRequestSettings
{
private Dictionary<string, object>? _extensionData;

/// <summary>
/// Service identifier.
/// This identifies a service and is set when the AI service is registered.
Expand All @@ -33,5 +35,9 @@ public class AIRequestSettings
/// Extra properties
/// </summary>
[JsonExtensionData]
public Dictionary<string, object> ExtensionData { get; set; } = new Dictionary<string, object>();
public Dictionary<string, object> ExtensionData
{
get => this._extensionData ??= new();
set => this._extensionData = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Microsoft.SemanticKernel.Events;
/// </summary>
public class FunctionInvokedEventArgs : SKCancelEventArgs
{
private Dictionary<string, object>? _metadata;

/// <summary>
/// Indicates if the function execution should repeat.
/// </summary>
Expand All @@ -18,7 +20,7 @@ public class FunctionInvokedEventArgs : SKCancelEventArgs
/// <summary>
/// Metadata for storing additional information about function execution result.
/// </summary>
public Dictionary<string, object> Metadata { get; private set; } = new Dictionary<string, object>();
public Dictionary<string, object> Metadata => this._metadata ??= new();

/// <summary>
/// Initializes a new instance of the <see cref="FunctionInvokedEventArgs"/> class.
Expand All @@ -27,7 +29,7 @@ public class FunctionInvokedEventArgs : SKCancelEventArgs
/// <param name="result">Function result</param>
public FunctionInvokedEventArgs(FunctionView functionView, FunctionResult result) : base(functionView, result.Context)
{
this.Metadata = result.Metadata;
this._metadata = result._metadata;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Microsoft.SemanticKernel.Orchestration;
/// </summary>
public sealed class FunctionResult
{
internal Dictionary<string, object>? _metadata;

/// <summary>
/// Name of executed function.
/// </summary>
Expand All @@ -23,7 +25,11 @@ public sealed class FunctionResult
/// <summary>
/// Metadata for storing additional information about function execution result.
/// </summary>
public Dictionary<string, object> Metadata { get; internal set; } = new Dictionary<string, object>();
public Dictionary<string, object> Metadata
{
get => this._metadata ??= new();
internal set => this._metadata = value;
}

/// <summary>
/// Function result object.
Expand Down Expand Up @@ -86,7 +92,8 @@ public FunctionResult(string functionName, string pluginName, SKContext context,
/// </summary>
public bool TryGetMetadataValue<T>(string key, out T value)
{
if (this.Metadata.TryGetValue(key, out object? valueObject) &&
if (this._metadata is { } metadata &&
metadata.TryGetValue(key, out object? valueObject) &&
valueObject is T typedValue)
{
value = typedValue;
Expand Down
Loading