Skip to content

Commit

Permalink
Reduce a bit of LINQ in M.E.AI (#5663)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Nov 18, 2024
1 parent 930af05 commit 5982f6a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public bool TryAdd(string key, object? value)
public void Clear() => _dictionary.Clear();

/// <inheritdoc />
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) => _dictionary.Contains(item);
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) =>
((ICollection<KeyValuePair<string, object?>>)_dictionary).Contains(item);

/// <inheritdoc />
public bool ContainsKey(string key) => _dictionary.ContainsKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.Json.Serialization;
using Microsoft.Shared.Diagnostics;

Expand Down Expand Up @@ -60,10 +59,10 @@ public string? AuthorName
[JsonIgnore]
public string? Text
{
get => Contents.OfType<TextContent>().FirstOrDefault()?.Text;
get => Contents.FindFirst<TextContent>()?.Text;
set
{
if (Contents.OfType<TextContent>().FirstOrDefault() is { } textContent)
if (Contents.FindFirst<TextContent>() is { } textContent)
{
textContent.Text = value;
}
Expand Down Expand Up @@ -95,6 +94,5 @@ public IList<AIContent> Contents
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }

/// <inheritdoc/>
public override string ToString() =>
string.Concat(Contents.OfType<TextContent>());
public override string ToString() => Contents.ConcatText();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.Json.Serialization;

namespace Microsoft.Extensions.AI;
Expand Down Expand Up @@ -66,10 +65,10 @@ public string? AuthorName
[JsonIgnore]
public string? Text
{
get => Contents.OfType<TextContent>().FirstOrDefault()?.Text;
get => Contents.FindFirst<TextContent>()?.Text;
set
{
if (Contents.OfType<TextContent>().FirstOrDefault() is { } textContent)
if (Contents.FindFirst<TextContent>() is { } textContent)
{
textContent.Text = value;
}
Expand Down Expand Up @@ -116,6 +115,5 @@ public IList<AIContent> Contents
public string? ModelId { get; set; }

/// <inheritdoc/>
public override string ToString() =>
string.Concat(Contents.OfType<TextContent>());
public override string ToString() => Contents.ConcatText();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
#if !NET
using System.Linq;
#else
using System.Runtime.CompilerServices;
#endif

namespace Microsoft.Extensions.AI;

/// <summary>Internal extensions for working with <see cref="AIContent"/>.</summary>
internal static class AIContentExtensions
{
/// <summary>Finds the first occurrence of a <typeparamref name="T"/> in the list.</summary>
public static T? FindFirst<T>(this IList<AIContent> contents)
where T : AIContent
{
int count = contents.Count;
for (int i = 0; i < count; i++)
{
if (contents[i] is T t)
{
return t;
}
}

return null;
}

/// <summary>Concatenates the text of all <see cref="TextContent"/> instances in the list.</summary>
public static string ConcatText(this IList<AIContent> contents)
{
int count = contents.Count;
switch (count)
{
case 0:
break;

case 1:
return contents[0] is TextContent tc ? tc.Text : string.Empty;

default:
#if NET
DefaultInterpolatedStringHandler builder = new(0, 0, null, stackalloc char[512]);
for (int i = 0; i < count; i++)
{
if (contents[i] is TextContent text)
{
builder.AppendLiteral(text.Text);
}
}

return builder.ToStringAndClear();
#else
return string.Concat(contents.OfType<TextContent>());
#endif
}

return string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private AssistantEvent CreateAssistantEvent(ChatMessage message)
{
if (EnableSensitiveData)
{
string content = string.Concat(message.Contents.OfType<TextContent>().Select(c => c.Text));
string content = string.Concat(message.Contents.OfType<TextContent>());
if (content.Length > 0)
{
return content;
Expand Down

0 comments on commit 5982f6a

Please sign in to comment.